Skip to content

Commit 461203f

Browse files
committed
Refactoring
1 parent 3e153f2 commit 461203f

File tree

1 file changed

+52
-100
lines changed

1 file changed

+52
-100
lines changed

tests/integration/test_fixtures.py

Lines changed: 52 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,25 @@ def remove_last_item_id(*_, **__) -> str:
5656
return ITEM_ID_LIST.pop()
5757

5858

59+
def setup_mock(mock_client_init):
60+
mock_client = mock_client_init.return_value
61+
mock_client.step_reporter = StepReporter(mock_client)
62+
return mock_client
63+
64+
65+
def setup_mock_for_logging(mock_client_init):
66+
mock_client = setup_mock(mock_client_init)
67+
set_current(mock_client)
68+
mock_client.start_test_item.side_effect = generate_item_id
69+
mock_client.finish_test_item.side_effect = remove_last_item_id
70+
mock_client.current_item.side_effect = get_last_item_id
71+
return mock_client
72+
73+
5974
@pytest.mark.parametrize('switch', [True, False])
6075
@mock.patch(REPORT_PORTAL_SERVICE)
6176
def test_fixture_on_off(mock_client_init, switch):
62-
mock_client = mock_client_init.return_value
63-
mock_client.step_reporter = StepReporter(mock_client)
77+
mock_client = setup_mock(mock_client_init)
6478

6579
variables = dict(utils.DEFAULT_VARIABLES)
6680
variables['rp_report_fixtures'] = switch
@@ -74,20 +88,22 @@ def test_fixture_on_off(mock_client_init, switch):
7488
'Incorrect number of "start_test_item" or "finish_test_item" calls'
7589

7690

91+
def run_tests(test_path, should_fail=False):
92+
variables = dict(utils.DEFAULT_VARIABLES)
93+
variables['rp_report_fixtures'] = True
94+
result = utils.run_pytest_tests(tests=[test_path], variables=variables)
95+
if should_fail:
96+
assert int(result) == 1, 'Exit code should be 1 (test failure)'
97+
else:
98+
assert int(result) == 0, 'Exit code should be 0 (no errors)'
99+
100+
77101
@mock.patch(REPORT_PORTAL_SERVICE)
78102
def test_fixture_setup(mock_client_init):
79-
mock_client = mock_client_init.return_value
80-
mock_client.step_reporter = StepReporter(mock_client)
81-
set_current(mock_client)
82-
mock_client.start_test_item.side_effect = generate_item_id
83-
mock_client.finish_test_item.side_effect = remove_last_item_id
84-
mock_client.current_item.side_effect = get_last_item_id
103+
mock_client = setup_mock_for_logging(mock_client_init)
85104

86-
variables = dict(utils.DEFAULT_VARIABLES)
87-
variables['rp_report_fixtures'] = True
88105
test_path = 'examples/fixtures/test_fixture_setup'
89-
result = utils.run_pytest_tests(tests=[test_path], variables=variables)
90-
assert int(result) == 0, 'Exit code should be 0 (no errors)'
106+
run_tests(test_path)
91107

92108
start_count = mock_client.start_test_item.call_count
93109
finish_count = mock_client.finish_test_item.call_count
@@ -121,18 +137,10 @@ def test_fixture_setup(mock_client_init):
121137

122138
@mock.patch(REPORT_PORTAL_SERVICE)
123139
def test_fixture_teardown(mock_client_init):
124-
mock_client = mock_client_init.return_value
125-
mock_client.step_reporter = StepReporter(mock_client)
126-
set_current(mock_client)
127-
mock_client.start_test_item.side_effect = generate_item_id
128-
mock_client.finish_test_item.side_effect = remove_last_item_id
129-
mock_client.current_item.side_effect = get_last_item_id
140+
mock_client = setup_mock_for_logging(mock_client_init)
130141

131-
variables = dict(utils.DEFAULT_VARIABLES)
132-
variables['rp_report_fixtures'] = True
133142
test_path = 'examples/fixtures/test_fixture_teardown'
134-
result = utils.run_pytest_tests(tests=[test_path], variables=variables)
135-
assert int(result) == 0, 'Exit code should be 0 (no errors)'
143+
run_tests(test_path)
136144

137145
start_count = mock_client.start_test_item.call_count
138146
finish_count = mock_client.finish_test_item.call_count
@@ -172,27 +180,17 @@ def test_fixture_teardown(mock_client_init):
172180
'examples/fixtures/test_fixture_teardown/test_fixture_teardown.py::test_fixture_teardown_1'
173181

174182

183+
@pytest.mark.skipif(sys.version_info < (3, 8), reason='Python 3.8+ required due to bugs in older versions')
175184
@mock.patch(REPORT_PORTAL_SERVICE)
176185
def test_fixture_setup_failure(mock_client_init):
177-
mock_client = mock_client_init.return_value
178-
mock_client.step_reporter = StepReporter(mock_client)
179-
set_current(mock_client)
180-
mock_client.start_test_item.side_effect = generate_item_id
181-
mock_client.finish_test_item.side_effect = remove_last_item_id
182-
mock_client.current_item.side_effect = get_last_item_id
186+
mock_client = setup_mock_for_logging(mock_client_init)
183187

184-
variables = dict(utils.DEFAULT_VARIABLES)
185-
variables['rp_report_fixtures'] = True
186188
test_path = 'examples/fixtures/test_fixture_setup_failure'
187-
result = utils.run_pytest_tests(tests=[test_path], variables=variables)
188-
assert int(result) == 1, 'Exit code should be 1 (test failure)'
189+
run_tests(test_path, True)
189190

190191
start_count = mock_client.start_test_item.call_count
191192
finish_count = mock_client.finish_test_item.call_count
192-
if sys.version_info < (3, 8):
193-
assert start_count == finish_count == 3, 'Incorrect number of "start_test_item" or "finish_test_item" calls'
194-
else:
195-
assert start_count == finish_count == 2, 'Incorrect number of "start_test_item" or "finish_test_item" calls'
193+
assert start_count == finish_count == 2, 'Incorrect number of "start_test_item" or "finish_test_item" calls'
196194

197195
call_args = mock_client.start_test_item.call_args_list
198196
setup_call_args = call_args[1][0]
@@ -223,18 +221,10 @@ def test_fixture_setup_failure(mock_client_init):
223221

224222
@mock.patch(REPORT_PORTAL_SERVICE)
225223
def test_fixture_teardown_failure(mock_client_init):
226-
mock_client = mock_client_init.return_value
227-
mock_client.step_reporter = StepReporter(mock_client)
228-
set_current(mock_client)
229-
mock_client.start_test_item.side_effect = generate_item_id
230-
mock_client.finish_test_item.side_effect = remove_last_item_id
231-
mock_client.current_item.side_effect = get_last_item_id
224+
mock_client = setup_mock_for_logging(mock_client_init)
232225

233-
variables = dict(utils.DEFAULT_VARIABLES)
234-
variables['rp_report_fixtures'] = True
235226
test_path = 'examples/fixtures/test_fixture_teardown_failure'
236-
result = utils.run_pytest_tests(tests=[test_path], variables=variables)
237-
assert int(result) == 1, 'Exit code should be 1 (test failure)'
227+
run_tests(test_path, True)
238228

239229
start_count = mock_client.start_test_item.call_count
240230
finish_count = mock_client.finish_test_item.call_count
@@ -285,18 +275,10 @@ def test_fixture_teardown_failure(mock_client_init):
285275

286276
@mock.patch(REPORT_PORTAL_SERVICE)
287277
def test_fixture_yield_none(mock_client_init):
288-
mock_client = mock_client_init.return_value
289-
mock_client.step_reporter = StepReporter(mock_client)
290-
set_current(mock_client)
291-
mock_client.start_test_item.side_effect = generate_item_id
292-
mock_client.finish_test_item.side_effect = remove_last_item_id
293-
mock_client.current_item.side_effect = get_last_item_id
278+
mock_client = setup_mock_for_logging(mock_client_init)
294279

295-
variables = dict(utils.DEFAULT_VARIABLES)
296-
variables['rp_report_fixtures'] = True
297280
test_path = 'examples/fixtures/test_fixture_yield_none'
298-
result = utils.run_pytest_tests(tests=[test_path], variables=variables)
299-
assert int(result) == 0, 'Exit code should be 0 (no errors)'
281+
run_tests(test_path)
300282

301283
start_count = mock_client.start_test_item.call_count
302284
finish_count = mock_client.finish_test_item.call_count
@@ -331,18 +313,10 @@ def test_fixture_yield_none(mock_client_init):
331313

332314
@mock.patch(REPORT_PORTAL_SERVICE)
333315
def test_fixture_return_none(mock_client_init):
334-
mock_client = mock_client_init.return_value
335-
mock_client.step_reporter = StepReporter(mock_client)
336-
set_current(mock_client)
337-
mock_client.start_test_item.side_effect = generate_item_id
338-
mock_client.finish_test_item.side_effect = remove_last_item_id
339-
mock_client.current_item.side_effect = get_last_item_id
316+
mock_client = setup_mock_for_logging(mock_client_init)
340317

341-
variables = dict(utils.DEFAULT_VARIABLES)
342-
variables['rp_report_fixtures'] = True
343318
test_path = 'examples/fixtures/test_fixture_return_none'
344-
result = utils.run_pytest_tests(tests=[test_path], variables=variables)
345-
assert int(result) == 0, 'Exit code should be 0 (no errors)'
319+
run_tests(test_path)
346320

347321
start_count = mock_client.start_test_item.call_count
348322
finish_count = mock_client.finish_test_item.call_count
@@ -377,18 +351,10 @@ def test_fixture_return_none(mock_client_init):
377351

378352
@mock.patch(REPORT_PORTAL_SERVICE)
379353
def test_failure_fixture_teardown(mock_client_init):
380-
mock_client = mock_client_init.return_value
381-
mock_client.step_reporter = StepReporter(mock_client)
382-
set_current(mock_client)
383-
mock_client.start_test_item.side_effect = generate_item_id
384-
mock_client.finish_test_item.side_effect = remove_last_item_id
385-
mock_client.current_item.side_effect = get_last_item_id
354+
mock_client = setup_mock_for_logging(mock_client_init)
386355

387-
variables = dict(utils.DEFAULT_VARIABLES)
388-
variables['rp_report_fixtures'] = True
389356
test_path = 'examples/fixtures/test_failure_fixture_teardown'
390-
result = utils.run_pytest_tests(tests=[test_path], variables=variables)
391-
assert int(result) == 1, 'Exit code should be 1 (test failure)'
357+
run_tests(test_path, True)
392358

393359
start_count = mock_client.start_test_item.call_count
394360
finish_count = mock_client.finish_test_item.call_count
@@ -440,14 +406,10 @@ def test_failure_fixture_teardown(mock_client_init):
440406
@pytest.mark.skipif(sys.version_info < (3, 8), reason='Python 3.8+ required due to bugs in older versions')
441407
@mock.patch(REPORT_PORTAL_SERVICE)
442408
def test_session_fixture_setup(mock_client_init):
443-
mock_client = mock_client_init.return_value
444-
mock_client.step_reporter = StepReporter(mock_client)
409+
mock_client = setup_mock(mock_client_init)
445410

446-
variables = dict(utils.DEFAULT_VARIABLES)
447-
variables['rp_report_fixtures'] = True
448411
test_path = 'examples/fixtures/session_fixture_return'
449-
result = utils.run_pytest_tests(tests=[test_path], variables=variables)
450-
assert int(result) == 0, 'Exit code should be 0 (no errors)'
412+
run_tests(test_path)
451413

452414
start_count = mock_client.start_test_item.call_count
453415
finish_count = mock_client.finish_test_item.call_count
@@ -472,14 +434,10 @@ def test_session_fixture_setup(mock_client_init):
472434
@pytest.mark.skipif(sys.version_info < (3, 8), reason='Python 3.8+ required due to bugs in older versions')
473435
@mock.patch(REPORT_PORTAL_SERVICE)
474436
def test_package_fixture_setup(mock_client_init):
475-
mock_client = mock_client_init.return_value
476-
mock_client.step_reporter = StepReporter(mock_client)
437+
mock_client = setup_mock(mock_client_init)
477438

478-
variables = dict(utils.DEFAULT_VARIABLES)
479-
variables['rp_report_fixtures'] = True
480439
test_path = 'examples/fixtures/package_fixture_return'
481-
result = utils.run_pytest_tests(tests=[test_path], variables=variables)
482-
assert int(result) == 0, 'Exit code should be 0 (no errors)'
440+
run_tests(test_path)
483441

484442
start_count = mock_client.start_test_item.call_count
485443
finish_count = mock_client.finish_test_item.call_count
@@ -504,14 +462,10 @@ def test_package_fixture_setup(mock_client_init):
504462
@pytest.mark.skipif(sys.version_info < (3, 8), reason='Python 3.8+ required due to bugs in older versions')
505463
@mock.patch(REPORT_PORTAL_SERVICE)
506464
def test_module_fixture_setup(mock_client_init):
507-
mock_client = mock_client_init.return_value
508-
mock_client.step_reporter = StepReporter(mock_client)
465+
mock_client = setup_mock(mock_client_init)
509466

510-
variables = dict(utils.DEFAULT_VARIABLES)
511-
variables['rp_report_fixtures'] = True
512467
test_path = 'examples/fixtures/module_fixture_return'
513-
result = utils.run_pytest_tests(tests=[test_path], variables=variables)
514-
assert int(result) == 0, 'Exit code should be 0 (no errors)'
468+
run_tests(test_path)
515469

516470
start_count = mock_client.start_test_item.call_count
517471
finish_count = mock_client.finish_test_item.call_count
@@ -533,17 +487,15 @@ def test_module_fixture_setup(mock_client_init):
533487
assert not setup_call_kwargs['has_stats']
534488

535489

490+
491+
536492
@pytest.mark.skipif(sys.version_info < (3, 8), reason='Python 3.8+ required due to bugs in older versions')
537493
@mock.patch(REPORT_PORTAL_SERVICE)
538494
def test_class_fixture_setup(mock_client_init):
539-
mock_client = mock_client_init.return_value
540-
mock_client.step_reporter = StepReporter(mock_client)
495+
mock_client = setup_mock(mock_client_init)
541496

542-
variables = dict(utils.DEFAULT_VARIABLES)
543-
variables['rp_report_fixtures'] = True
544497
test_path = 'examples/fixtures/class_fixture_return'
545-
result = utils.run_pytest_tests(tests=[test_path], variables=variables)
546-
assert int(result) == 0, 'Exit code should be 0 (no errors)'
498+
run_tests(test_path)
547499

548500
start_count = mock_client.start_test_item.call_count
549501
finish_count = mock_client.finish_test_item.call_count

0 commit comments

Comments
 (0)