用两个不同的pytest灯具进行测试

时间:2017-09-22 15:42:26

标签: python selenium testing pytest

我目前正在使用pytest和Selenium测试网络应用。所有页面都有“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

现在对于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
    
  4. 这非常有效,我可以测试从此页面注销。现在我的问题是我有另一个页面可以从“页面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()

3 个答案:

答案 0 :(得分:6)

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

在这里,您也可以获得与上面相同的输出。

希望它会对你有所帮助。

答案 1 :(得分:1)

通常:

@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

答案 2 :(得分:0)

所以这里有一个例子,我为你解释了重复使用东西的不同方式。请先查看哪一张账单。

A.createOrReplaceTempView("A");
B.createOrReplaceTempView("B");

String query = "Select * from A UNION Select * from B";

Dataset<Row> result = sparkSession.sql(query);
result.show();

输出test_can_log_out

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_style2

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

输出test_logout_page2

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

输出test_logout_page2_style2

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