使用两个不同的 pytest 夹具运行测试 [英] Run a test with two different pytest fixtures

查看:45
本文介绍了使用两个不同的 pytest 夹具运行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 pytest 和 Selenium 测试一个网络应用程序.所有页面都有主页"和注销"链接,所以我写了一个这样的测试:

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

现在对于 page 固定装置,我正在模拟登录过程.我把它分成几个固定装置:

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

  1. 获取 Selenium WebDriver 实例

  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()

  • 登录网络应用

  • 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
    

  • 访问页面

  • Visit a page

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

  • 这很好用,我可以测试从这个页面注销.现在我的问题是我有另一个可以从页面 A"访问的页面:

    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
    

    然而,这违反了 DRY 原则.如何在不重复的情况下重用 test_can_log_out()?

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

    推荐答案

    在这里,您可以传递您的 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:

    测试文件.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.getfixturevalue(sub_page) # with pytest>=3.0.0 use getfixturevalue instead of getfuncargvalue
            print "test output of ", sub_page
    

    输出:

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

    当夹具在 conftest.py 时

    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"
    

    测试文件.py

    import pytest
    
    class 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.

    希望能帮到你.

    这篇关于使用两个不同的 pytest 夹具运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆