Home > Software engineering >  Pytest - Run 2 tests on the same page / same browser session with separate assertion for each test
Pytest - Run 2 tests on the same page / same browser session with separate assertion for each test

Time:07-17

there is a framework to run tests. It has a separate method that yields web driver and closes the browser after the entire test is finished, which goes with Pytest and assertion at the end of the test.

@pytest.fixture
def driver(some_request):
    *getting the needed driver*
    yield driver
    *do something else*
        driver.quit()

The problem is sometimes it's needed to run 2 or more tests during the same browser session and the same web page. For instance, one test to check filters and the second one to clear the filters and reset the filters' state.

def test_test1_and_test2(something_from_conftest):
    *test1: do something on and with the page*
        assert *assertion expression*
    *test2: do something on and with the page*
        assert *assertion expression*

So, are there any solutions for such cases? How to implement it this way:

  1. open the page by web driver,
  2. do test_1 and assert it,
  3. do test_2 and assert it,
  4. and only after that quit the driver.

The reason I'm asking is when you assert the test_1 and it fails, test_2 won't be tested at all, because assertion quits the test.

I can do both assertions in the end, but still how to perform 2 or several tests using Pytest during one browser session and on the same page?

CodePudding user response:

There are two paths you can follow:

  1. Use a separate fixture with extended scope, like a "module" or "session". This way the code after yield would be executed once all of your tests have finished.
  2. Combine the two tests and change the way you assert - do anyone of the ideas in this stack overflow thread: using if statements to build list of errors and assert in the end, use any of pytest-assume, pytest_check or delayed-assert libraries.
  • Related