2

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!

Froodo
  • 573
  • 1
  • 6
  • 16

1 Answers1

0

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:

https://pytest.org/latest/fixture.html

Mumbleskates
  • 1,248
  • 10
  • 18
  • My add_to_result function is part of a class related to test_rail. and my update_test_case_status fixture is in conftest file...im not sure what you mean tho.. – Froodo Mar 15 '16 at 15:52
  • @Froodo The tests themselves can be put into classes, which can be marked as using a fixture with `pytest.mark.usefixtures()`. The tests in that class will then be run together and have that fixture available for all of them, and it will only be run once if you use `@pytest.mark.fixture(scope="class")`. I'll edit the answer to include more relevant documentation. – Mumbleskates Mar 16 '16 at 22:30