微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 使用两个不同的pytest fixture运行测试

我目前正在使用pytest和Selenium测试一个Web应用程序.所有页面都有“Home”和“Log Out”链接,所以我写了一个这样的测试:

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

现在对于页面夹具,我正在模拟登录过程.我把它分成了几个装置:

>获取Selenium WebDriver实例

@pytest.fixture()
def browser(request, data, headless):
    b = webdriver.Firefox(executable_path=DRIVERS_PATH + '/geckodriver')
    yield b
    b.quit()

>登录Web应用程序

@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

>访问页面

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

这非常有效,我可以测试从此页面注销.现在我的问题是我有一个页面可以从“页面A”访问:

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

现在我想用这个灯具运行完全相同的测试.当然,我可以复制/粘贴并进行一些更改:

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

解决方法:

在这里,您可以传递您的页面和子页面的测试参数,这些测试参数将作为测试的第一步动态调用.如下.

当灯具位于测试所在的同一页面时:

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

输出

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

当灯具处于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

在这里,您还将获得与上面相同的输出.

希望它会对你有所帮助.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐