I am trying to execute a finalizer fixture after all parameters of my test case ran. The problem is my finalizer is getting executed after every parameter run so the values that i need are getting reset. Example:
@pytest.mark.parametrize('testcaseid',
['1', '2', '3'])
@pytest.mark.parametrize('lang',
["EN",
"FR"])
def test_lang(self, request, base_url, lang, testcaseid, connect_testrail):
try:
base_url = base_url.replace("english", "french") if lang == "FR" else base_url
self.navigate(base_url)
self.wait_for_page_loaded()
result = self.check_correct_lang_displayed()
assert result
connect_testrail.add_to_result(testcaseid, result="Passed", cmnt=lang)
except Exception as e:
connect_testrail.add_to_result(testcaseid, result="Failed", cmnt=lang + ". Error: " + str(e))
pytest.fail(str(e))
my add_to_result function in a separate class is as follows:
def add_to_result(self, case_id, result="Untested", cmnt='None'):
self.testcase.setdefault(case_id, [])
self.testcase[case_id].append(result)
self.testcase[case_id].append(cmnt)
my teardown finalizer in the conftest file is this:
@pytest.fixture(scope='function', autouse=True)
def update_test_case_status(request):
logging.info("Starting Test case...")
def update_at_the_end():
logging.info("Ending Test Case...")
results = test_results()
for values in results.itervalues():
if "Failed" in values[0]:
test_rail_conn.update_test_status(test_result="Failed")
break
else:
test_rail_conn.update_test_status(test_result="Passed")
test_rail_conn.testcase.clear()
request.addfinalizer(update_at_the_end)
The goal is to have all test cases with their results in the dictionary 'testcase' and at the end during the finalizer i want to check the value[0] field = result and see if anything failed. Problem is that after every parameter run, the testcase field is cleared and i only get single result.
Any help is appreciated!
Fixtures can have 'class' scope (@pytest.fixture(scope='class')
) and be applied to a class with @pytest.mark.usesfixtures("fixture_name")
. This way the fixture is only used once for the tests that get run, grouped together (and including their parametrization), inside the class:
https://pytest.org/latest/unittest.html#mixing-pytest-fixtures-into-unittest-testcase-style-tests
You can also see that 'class' is available as a scope for fixtures, described here: