22from unittest .mock import MagicMock
33
44from pytest import fixture , mark
5- from scanapi .console import write_report_path , write_results
5+
6+ from scanapi .console import (
7+ write_report_path ,
8+ write_result ,
9+ write_results ,
10+ write_summary ,
11+ )
612
713
814@fixture
@@ -11,49 +17,58 @@ def mocked__console(mocker):
1117
1218
1319@mark .describe ("console" )
14- @mark .describe ("write_results " )
20+ @mark .describe ("write_result " )
1521class TestWriteResults :
1622 @fixture
17- def mocked__session (self , mocker ):
18- session = MagicMock ()
19- session .errors = 0
20- session .elapsed_time .return_value = timedelta (seconds = 3 )
21- return mocker .patch ("scanapi.console.session" , session )
23+ def mocked__write_result (self , mocker ):
24+ return mocker .patch ("scanapi.console.write_result" )
2225
23- @mark .context ("when session has successes and no failures" )
24- @mark .it ("should write results and write summary" )
25- def test_write_success (self , mocked__console , mocked__session ):
26+ @mark .context ("when results is empty" )
27+ @mark .it ("should not call write_result" )
28+ def test_should_not_call (self , mocked__write_result ):
29+ write_results ([])
2630
27- test_success = {
28- "name" : "should_be_success" ,
29- "status" : "passed" ,
30- "failure" : None ,
31- "error" : None ,
32- }
33- fake_results_passed = [
31+ assert not mocked__write_result .called
32+
33+ @mark .context ("when results has size 3" )
34+ @mark .it ("should call write_result 3 times" )
35+ def test_should_call_3_times (self , mocked__write_result ):
36+ write_results ([1 , 2 , 3 ])
37+
38+ assert mocked__write_result .call_count == 3
39+
40+
41+ @mark .describe ("console" )
42+ @mark .describe ("write_result" )
43+ class TestWriteResult :
44+ @mark .context ("when tests results contains one success" )
45+ @mark .it ("should print the success result" )
46+ def test_write_success (self , mocked__console ):
47+
48+ tests_results = [
3449 {
35- "response" : "foo" ,
36- "tests_results" : [test_success ],
37- "no_failure" : True ,
38- },
50+ "name" : "should_be_success" ,
51+ "status" : "passed" ,
52+ "failure" : None ,
53+ "error" : None ,
54+ }
3955 ]
4056
41- mocked__session .failures = 0
42- mocked__session .successes = 1
57+ fake_result_passed = {
58+ "tests_results" : tests_results ,
59+ }
4360
44- write_results ( fake_results_passed )
61+ write_result ( fake_result_passed )
4562
4663 mocked__console .print .assert_called_once_with (
4764 "[bright_green] [PASSED] [white]should_be_success"
4865 )
4966
50- mocked__console .rule .assert_called_once_with (
51- "[bright_green]1 passed in 3.0s" , characters = "=" ,
52- )
53-
54- @mark .context ("when session has failures" )
55- @mark .it ("should write results and write summary" )
56- def test_write_failures (self , mocker , mocked__console , mocked__session ):
67+ @mark .context ("when tests results contains one success and one failure" )
68+ @mark .it (
69+ "should print two lines, one for the success and one for the failure"
70+ )
71+ def test_write_failures (self , mocker , mocked__console ):
5772 tests = [
5873 {
5974 "name" : "failed_test" ,
@@ -68,14 +83,12 @@ def test_write_failures(self, mocker, mocked__console, mocked__session):
6883 "error" : None ,
6984 },
7085 ]
71- fake_results_failed = [
72- {"response" : "foo" , "tests_results" : tests , "no_failure" : True ,},
73- ]
7486
75- mocked__session .failures = 1
76- mocked__session .successes = 1
87+ fake_result_failed = {
88+ "tests_results" : tests ,
89+ }
7790
78- write_results ( fake_results_failed )
91+ write_result ( fake_result_failed )
7992
8093 calls = [
8194 mocker .call (
@@ -86,33 +99,17 @@ def test_write_failures(self, mocker, mocked__console, mocked__session):
8699
87100 mocked__console .print .assert_has_calls (calls )
88101
89- mocked__console .rule .assert_called_once_with (
90- "[bright_green]1 passed, [bright_red]1 failed, [bright_red]0 errors in 3.0s" ,
91- characters = "=" ,
92- style = "bright_red" ,
93- )
94-
95102 @mark .context ("when session has no tests" )
96- @mark .it ("should just write summary" )
97- def test_write_without_tests (
98- self , mocker , mocked__console , mocked__session
99- ):
100-
101- fake_results_failed = [
102- {"response" : "foo" , "tests_results" : [], "no_failure" : True ,},
103- ]
104-
105- mocked__session .failures = 0
106- mocked__session .successes = 0
103+ @mark .it ("should print nothing" )
104+ def test_write_without_tests (self , mocked__console ):
105+ fake_result_failed = {
106+ "tests_results" : [],
107+ }
107108
108- write_results ( fake_results_failed )
109+ write_result ( fake_result_failed )
109110
110111 assert not mocked__console .print .called
111112
112- mocked__console .rule .assert_called_once_with (
113- "[bright_green]0 passed in 3.0s" , characters = "=" ,
114- )
115-
116113
117114@mark .describe ("console" )
118115@mark .describe ("write_report_path" )
@@ -126,3 +123,53 @@ def test(self, mocked__console):
126123 "The documentation was generated successfully.\n "
127124 "It is available at -> [deep_sky_blue1 underline]http://localhost:8080\n "
128125 )
126+
127+
128+ @mark .describe ("console" )
129+ @mark .describe ("write_summary" )
130+ class TestWriteSummary :
131+ @fixture
132+ def mocked__session (self , mocker ):
133+ session = MagicMock ()
134+ session .errors = 0
135+ session .elapsed_time .return_value = timedelta (seconds = 3 )
136+ return mocker .patch ("scanapi.console.session" , session )
137+
138+ @mark .context ("when session has successes and no failures" )
139+ @mark .it ("should print the success summary" )
140+ def test_write_success (self , mocked__console , mocked__session ):
141+ mocked__session .failures = 0
142+ mocked__session .successes = 1
143+
144+ write_summary ()
145+
146+ mocked__console .rule .assert_called_once_with (
147+ "[bright_green]1 passed in 3.0s" , characters = "=" ,
148+ )
149+
150+ @mark .context ("when session has failures" )
151+ @mark .it ("should print the failure summary" )
152+ def test_write_failures (self , mocked__console , mocked__session ):
153+ mocked__session .failures = 1
154+ mocked__session .successes = 1
155+
156+ write_summary ()
157+
158+ mocked__console .rule .assert_called_once_with (
159+ "[bright_green]1 passed, [bright_red]1 failed, [bright_red]0 errors in 3.0s" ,
160+ characters = "=" ,
161+ style = "bright_red" ,
162+ )
163+
164+ @mark .context ("when session has no tests" )
165+ @mark .it ("should print the success summary" )
166+ def test_write_without_tests (self , mocked__console , mocked__session ):
167+
168+ mocked__session .failures = 0
169+ mocked__session .successes = 0
170+
171+ write_summary ()
172+
173+ mocked__console .rule .assert_called_once_with (
174+ "[bright_green]0 passed in 3.0s" , characters = "=" ,
175+ )
0 commit comments