Run a test with two different pytest fixtures

大憨熊 提交于 2019-12-07 06:34:53

问题


I am currently testing a web app using pytest and Selenium. All pages have "Home" and "Log Out" links, so I have written a test like this:

def test_can_log_out(page):
    link = page.find_element_by_partial_link_text('Log Out')
    link.click()
    assert 'YOU HAVE SUCCESSFULLY LOGGED OFF!' in starting_page.page_source

Now for the page fixture, I am simulating the login process. I have this broken into several fixtures:

  1. Get the Selenium WebDriver instances

    @pytest.fixture()
    def browser(request, data, headless):
        b = webdriver.Firefox(executable_path=DRIVERS_PATH + '/geckodriver')
        yield b
        b.quit()
    
  2. Log in to the web app

    @pytest.fixture()
    def login(browser):
        browser.get('http://example.com/login)
        user_name = browser.find_element_by_name('user_name')
        user_name.send_keys('codeapprentice')
        password = browser.find_element_by_name('password')
        password.send_keys('password1234')
        submit = browser.find_element_by_name('submit')
        submit.click()
        return browser
    
  3. Visit a page

    @pytest.fixture()
    def page(login):
        link = login.find_element_by_partial_link_text('Sub Page A')
        link.click()
        return login
    

This works very well and I can test logging out from this page. Now my question is that I have another page which can be visited from "Page A":

@pytest.fixture()
def subpage(page):
    button = login.find_element_name('button')
    button.click()
    return page

Now I want to run the exact same test with this fixture, also. Of course, I can copy/paste and make a few changes:

def test_can_log_out_subpage(subpage):
    link = page.find_element_by_partial_link_text('Log Out')
    link.click()
    assert 'YOU HAVE SUCCESSFULLY LOGGED OFF!' in starting_page.page_source

However, this violates the DRY principle. How can I reuse test_can_log_out() without this repetition?


回答1:


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 pytest

class 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.getfuncargvalue(sub_page)
        print "test output of ", sub_page

Output:

browser
login
subpage1
test output of  subpage1
browser
login
subpage2
test output of  subpage2
browser
login
subpage3
test output of  subpage3

When fixtures are at conftest.py

import pytest


@pytest.fixture
def browser(request):
        print "browser"

@pytest.fixture
def login(request):
    print "login"

@pytest.fixture
def subpage1(request,login):
    print "subpage1"

@pytest.fixture
def subpage2(request, login):
    print "subpage2"

@pytest.fixture
def subpage3(request, login):
    print "subpage3"

testfile.py

import pytest

class TestABC():

    @pytest.mark.parametrize('sub_page',
                             ['subpage1', 'subpage2', 'subpage3'])
    def test_can_log_out_subpage(self,sub_page,request):
        request.getfuncargvalue(sub_page)
        print "test output of ", sub_page

Here, you will also get same output as above.

Hope it would help you.




回答2:


So here is a example I worked out for you to explain different way of re-using stuff. Please see which one first 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")
    pass


def test_can_log_style2(logout):
    print("logging out using fixture")
    pass


def test_logout_page2(subpage, logout):
    print("test can logout from page 2")
    pass


def 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 browser
logging in
on page
.logging out using test
quitting browser

Output of test_can_log_style2

Launching browser
logging in
on page
.logging out using fixture
performing logout using fixtures
quitting browser

Output of test_logout_page2

Launching browser
logging in
on page
on subpage
.test can logout from page 2
performing logout using fixtures
quitting browser

Output of test_logout_page2_style2

Launching browser
logging in
on page
on subpage
.test can logout from page 2 style2
logging out using test
quitting browser


来源:https://stackoverflow.com/questions/46368468/run-a-test-with-two-different-pytest-fixtures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!