Skip to content

Commit faa704b

Browse files
authored
Fix bug screenshot not attached with email and update tests (#113)
* Fix bug screenshot not attached with email and update tests * remove report path from email env and automatically fetch it
1 parent 7771658 commit faa704b

4 files changed

Lines changed: 35 additions & 31 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ Create an emailenv file in your project folder that has the following
155155
```commandline
156156
sender_email=you@example.com
157157
recipient_email=team@example.com
158-
report_path=report.html
159158
subject=Your Test Report
160159
smtp_server=smtp.sendgrid.net
161160
smtp_port=587

pytest_reporter_plus/plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def pytest_sessionfinish(session, exitstatus):
132132
print("📬 --send-email enabled. Sending report...")
133133
try:
134134
config = load_email_env()
135-
config["report_path"] = f"{html_output}/report.html"
135+
config["report_path"] = f"{html_output}"
136136
send_email_from_env(config)
137137
except Exception as e:
138138
raise RuntimeError(f"Failed to send email: {e}") from e

pytest_reporter_plus/send_email_report.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
from email.message import EmailMessage
44
from email.utils import make_msgid
55

6+
import shutil
7+
8+
def zip_report_folder(folder_path: str, output_zip: str):
9+
shutil.make_archive(output_zip.replace(".zip", ""), 'zip', folder_path)
10+
return output_zip
11+
612
def load_email_env(filepath="emailenv"):
713
if not os.path.exists(filepath):
814
raise FileNotFoundError("emailenv file not found!")
@@ -25,17 +31,19 @@ def send_email_from_env(config: dict):
2531
password = config["email_password"]
2632
use_tls = True
2733

34+
zip_path = zip_report_folder(report_path, f"{report_path}_zipped" + ".zip")
35+
2836
msg = EmailMessage()
2937
msg["Subject"] = subject
3038
msg["From"] = sender
3139
msg["To"] = recipient
3240

3341
msg.set_content("Your test report is attached as an HTML file.")
3442

35-
filename = os.path.basename(report_path)
36-
with open(report_path, "rb") as f:
43+
filename = os.path.basename(zip_path)
44+
with open(zip_path, "rb") as f:
3745
report_data = f.read()
38-
msg.add_attachment(report_data, maintype="text", subtype="html", filename=filename)
46+
msg.add_attachment(report_data, maintype="application", subtype="zip", filename=filename)
3947

4048
try:
4149
if "sendgrid" in smtp_server.lower():

tests/e2e/test_email.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
from unittest import mock
2-
from unittest.mock import patch, MagicMock
3-
41
import pytest
52

63
from pytest_reporter_plus.send_email_report import send_email_from_env, load_email_env
7-
4+
from unittest.mock import patch, MagicMock
85

96
@patch("smtplib.SMTP")
10-
def test_send_email_success(mock_smtp):
11-
# Prepare mock environment
7+
def test_send_email_success(mock_smtp, tmp_path):
8+
report_dir = tmp_path / "report_output"
9+
report_dir.mkdir()
10+
report_file = report_dir / "report.html"
11+
report_file.write_text("<h1>Test Report</h1>")
12+
1213
config = {
1314
"sender_email": "sender@example.com",
1415
"recipient_email": "recipient@example.com",
15-
"report_path": "sample_report.html",
16+
"report_path": str(report_dir),
1617
"subject": "Test Subject",
1718
"smtp_server": "smtp.sendgrid.net",
1819
"smtp_port": "587",
1920
"email_password": "SG.fakekeyforunittesting"
2021
}
2122

22-
with open(config["report_path"], "w") as f:
23-
f.write("<h1>Test Report</h1>")
24-
2523
mock_server = MagicMock()
2624
mock_smtp.return_value.__enter__.return_value = mock_server
2725

@@ -34,25 +32,25 @@ def test_send_email_success(mock_smtp):
3432

3533

3634
@patch("smtplib.SMTP")
37-
def test_invalid_sendgrid_key_warning(mock_smtp, capsys):
35+
def test_invalid_sendgrid_key_warning(mock_smtp, capsys, tmp_path):
36+
report_dir = tmp_path / "report_output"
37+
report_dir.mkdir()
38+
(report_dir / "report.html").write_text("test")
39+
3840
config = {
3941
"sender_email": "sender@example.com",
4042
"recipient_email": "recipient@example.com",
41-
"report_path": "sample_report.html",
43+
"report_path": str(report_dir),
4244
"subject": "Invalid Key Test",
4345
"smtp_server": "smtp.sendgrid.net",
4446
"smtp_port": "587",
4547
"email_password": "not_sendgrid_key"
4648
}
4749

48-
with open(config["report_path"], "w") as f:
49-
f.write("test")
50-
5150
mock_server = MagicMock()
5251
mock_smtp.return_value.__enter__.return_value = mock_server
5352

5453
send_email_from_env(config)
55-
5654
captured = capsys.readouterr()
5755
assert "SendGrid API key looks invalid" in captured.out
5856

@@ -62,17 +60,17 @@ def test_load_email_env_file_not_found():
6260
load_email_env("non_existent_file.env")
6361

6462

65-
import pytest
6663
from unittest import mock
6764

6865
def test_send_email_handles_exception(tmp_path):
69-
report_path = tmp_path / "report.html"
70-
report_path.write_text("<html><body>Fake report</body></html>")
66+
report_dir = tmp_path / "report_output"
67+
report_dir.mkdir()
68+
(report_dir / "report.html").write_text("<html><body>Fake report</body></html>")
7169

7270
config = {
7371
"sender_email": "test@example.com",
7472
"recipient_email": "recipient@example.com",
75-
"report_path": str(report_path),
73+
"report_path": str(report_dir),
7674
"subject": "Test Report",
7775
"smtp_server": "smtp.sendgrid.net",
7876
"smtp_port": "587",
@@ -84,17 +82,15 @@ def test_send_email_handles_exception(tmp_path):
8482
send_email_from_env(config)
8583

8684

87-
88-
import pytest
89-
9085
def test_warns_on_invalid_sendgrid_key(tmp_path):
91-
report_path = tmp_path / "report.html"
92-
report_path.write_text("<html><body>Hi</body></html>")
86+
report_dir = tmp_path / "report_output"
87+
report_dir.mkdir()
88+
(report_dir / "report.html").write_text("<html><body>Hi</body></html>")
9389

9490
config = {
9591
"sender_email": "me@example.com",
9692
"recipient_email": "you@example.com",
97-
"report_path": str(report_path),
93+
"report_path": str(report_dir),
9894
"subject": "Fake Subject",
9995
"smtp_server": "smtp.sendgrid.net",
10096
"smtp_port": "587",
@@ -105,6 +101,7 @@ def test_warns_on_invalid_sendgrid_key(tmp_path):
105101
with pytest.raises(RuntimeError, match="Failed to send email: SMTP failed"):
106102
send_email_from_env(config)
107103

104+
108105
def test_load_email_env_parses_file_correctly(tmp_path):
109106
env_file = tmp_path / "emailenv"
110107
env_file.write_text("sender_email=me@example.com\nrecipient_email=you@example.com\n")

0 commit comments

Comments
 (0)