Run a test with two different pytest fixtures Run a test with two different pytest fixtures selenium selenium

Run a test with two different pytest fixtures


Here, you can pass your fixtures which gives your pages and subpages in test parameters which would be called dynamically as a first step of test. Like below.

When fixtures are on same page where tests are:

testfile.py

import pytestclass TestABC():    @pytest.fixture    def browser(self,request):        print "browser"    @pytest.fixture    def login(self,request,browser):        print "login"    @pytest.fixture    def subpage1(self,request,login):        print "subpage1"    @pytest.fixture    def subpage2(self, request, login):        print "subpage2"    @pytest.fixture    def subpage3(self, request, login):        print "subpage3"    @pytest.mark.parametrize('sub_page',                             ['subpage1', 'subpage2', 'subpage3'])    def test_can_log_out_subpage(self,sub_page,request):        request.getfixturevalue(sub_page) # with pytest>=3.0.0 use getfixturevalue instead of getfuncargvalue        print "test output of ", sub_page

Output:

browserloginsubpage1test output of  subpage1browserloginsubpage2test output of  subpage2browserloginsubpage3test output of  subpage3

When fixtures are at conftest.py

import pytest@pytest.fixturedef browser(request):    print "browser"@pytest.fixturedef login(request):    print "login"@pytest.fixturedef subpage1(request,login):    print "subpage1"@pytest.fixturedef subpage2(request, login):    print "subpage2"@pytest.fixturedef subpage3(request, login):    print "subpage3"

testfile.py

import pytestclass TestABC():    @pytest.mark.parametrize('sub_page',                             ['subpage1', 'subpage2', 'subpage3'])    def test_can_log_out_subpage(self,sub_page,request):        request.getfixturevalue(sub_page)  # with pytest>=3.0.0 use getfixturevalue instead of getfuncargvalue        print "test output of ", sub_page

Here, you will also get same output as above.

Hope it would help you.


So here is a example I worked out to demonstrate the reuse of fixtures. A fixture can reference another fixture - allowing for a layered approach to writing tests. Please see which one fits the bill for you:

import pytest@pytest.yield_fixture()def browser():    print("Launching browser")    b = {}    yield b    print("quitting browser")@pytest.fixture()def login(browser):    print("logging in")@pytest.fixture()def page(login):    print("on page")@pytest.fixture()def subpage(page):    print("on subpage")@pytest.yield_fixture()def logout(page):    yield page    print('performing logout using fixtures')def test_can_log_out(page):    print("logging out using test")    passdef test_can_log_style2(logout):    print("logging out using fixture")    passdef test_logout_page2(subpage, logout):    print("test can logout from page 2")    passdef test_logout_page2_style2(subpage):    print("test can logout from page 2 style2")    test_can_log_out(subpage)    pass

Output of test_can_log_out

Launching browserlogging inon page.logging out using testquitting browser

Output of test_can_log_style2

Launching browserlogging inon page.logging out using fixtureperforming logout using fixturesquitting browser

Output of test_logout_page2

Launching browserlogging inon pageon subpage.test can logout from page 2performing logout using fixturesquitting browser

Output of test_logout_page2_style2

Launching browserlogging inon pageon subpage.test can logout from page 2 style2logging out using testquitting browser


Generally :

@pytest.fixture(scope="function")def fixture_one():    # set up things    yield    # teardown@pytest.fixture(scope="function")def fixture_two():    # do things@pytest.mark.parametrize('fixture_func', [fixture_one, fixture_two])def test_things(fixture_func, request):    request.getfixturevalue(fixture_func.__name__)    assert foo == bar