【问题标题】:Run a test with two different pytest fixtures使用两个不同的 pytest 夹具运行测试
【发布时间】:2017-09-22 15:42:25
【问题描述】:

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

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 夹具,我正在模拟登录过程。我把它分成了几个固定装置:

  1. 获取 Selenium WebDriver 实例

    @pytest.fixture()
    def browser(request, data, headless):
        b = webdriver.Firefox(executable_path=DRIVERS_PATH + '/geckodriver')
        yield b
        b.quit()
    
  2. 登录网络应用

    @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. 访问页面

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

【问题讨论】:

  • 你想如何以及在哪里重复使用?
  • @TarunLalwani 我想同时使用page 和subpage 夹具运行测试。
  • 如果您在测试中调用subpage 作为参数,page 将自动执行。您也可以致电test_can_log_out(subpage) 进行注销
  • @TarunLalwani 你熟悉 pytest 吗?我自己不调用任何这些函数。
  • 是的,我熟悉 pytest,只是我想更好地理解你想要什么。我的意思是你可以在你的 subpage 夹具方法中添加一个 return test_can_log_out(page) 调用。

标签: python selenium testing pytest


【解决方案1】:

在这里,您可以传递您的fixtures,它为您的页面和子页面提供测试参数,这些参数将作为测试的第一步动态调用。如下所示。

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

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.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 时

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

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

希望对你有帮助。

【讨论】:

  • getfuncargvalue 已弃用。请改用getfixturevalue。
【解决方案2】:

一般:

@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

【讨论】:

  • 这看起来是一个有趣的方法。以后需要的时候我会记住的。
  • assert foo = bar 是否缺少=?
【解决方案3】:

所以这里有一个例子,我用来演示固定装置的重用。一个夹具可以引用另一个夹具 - 允许使用分层方法来编写测试。请查看哪一款适合您:

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

test_can_log_out 的输出

Launching browser
logging in
on page
.logging out using test
quitting browser

test_can_log_style2 的输出

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

test_logout_page2 的输出

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

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

【讨论】:

  • 感谢您的帮助。你能解释一下不同的风格以及它们如何解决我的问题吗?
  • 所以我会先发表评论,然后在您认为它解决了您的问题后更新答案。有两种方法可以重用注销。一是让它成为一个产量夹具,二是让它成为一个功能(也可以是一个测试)。现在一个需要重用注销代码的测试,可以使用logout作为最后一个参数,也可以直接在测试内部调用函数来执行注销。由于名称,此功能也可以用作测试。因此,如果看到我进行的 4 次测试,我从不重复注销代码。
  • 我更明白你之前打电话给test_can_log_out() 的意思。或者,我可以分解出与从两种不同测试方法调用的实用程序函数共有的代码。我一直在寻找一个使用依赖注入的解决方案,可能带有参数化的装置,我没有考虑自己调用函数的更直接的解决方案。
  • 这就是我编写示例代码的原因。因为有时我们会陷入以一种方式解决问题
  • @Code-Apprentice,希望这回答了您的问题?
【解决方案4】:

另一个对我有用的解决方案是使用类。它需要比上述解决方案更多的设置,但如果您最终不得不设置多个相关测试,它会很有帮助。

您在一个类中定义所有测试,然后为每个扩展该第一个类的夹具创建一个虚拟类:

@pytest.fixture(scope='class')
def fixture1(request):
    request.cls.fruit = 'apple'

@pytest.fixture(scope='class')
def fixture2(request):
    request.cls.fruit = 'banana'

class ClassOfRelatedTests:
    def test_is_banana(self):
        assert self.fruit == 'banana'

    def test_not_spinach(self):
        assert self.fruit != 'spinach'

@pytest.mark.usefixtures("fixture1")
class TestFixture1(ClassOfRelatedTests):
    pass

@pytest.mark.usefixtures("fixture2")
class TestFixture2(ClassOfRelatedTests):
    pass

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 2018-02-24
    相关资源
    最近更新 更多