1- from unittest import mock
2- from unittest .mock import patch , MagicMock
3-
41import pytest
52
63from 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
6663from unittest import mock
6764
6865def 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-
9085def 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+
108105def 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\n recipient_email=you@example.com\n " )
0 commit comments