Py.test fixture:在示波器夹具

时间:2017-08-22 12:04:30

标签: python python-2.7 selenium pytest fixtures

我正面临着pytest灯具的一个小问题,非常感谢你的帮助。

我有一些功能装置,如下所述。为简单起见,我没有展示实施。

@pytest.fixture()
def get_driver():
    pass

@pytest.fixture()
def login(get_driver):
    pass

@pytest.fixture()
def settings(login):
    pass

问题是我还需要一个(会话级)夹具,它在我运行第一个测试用例之前进行设置。 (实际上开始测试)。即转到设置页面并创建一些设置。 (登录后)

现在的问题是我无法使用会话级夹具这样做,因为我无法在会话级别使用功能级夹具。 还是我可以?

@pytest.fixture(scope="session")
def setup(settings):
    settings.create_settings()
    pass

3 个答案:

答案 0 :(得分:1)

您需要使用变通方法。该操作需要在function范围内进行,autouse设置为True

您需要在基于session的fixture中初始化变量,这将检查settings是否已完成。如果没有完成,那么您将进行设置并将标志更改为False

以下是一个工作示例

import pytest


@pytest.fixture(scope="session", autouse=True)
def settings_page():
    config = {"config_done": False}
    return config


@pytest.fixture()
def init(request):
    print("init called")
    return "init"


@pytest.fixture()
def driver():
    print("driver called")

    return "driver"


@pytest.fixture(autouse=True)
def init_settings(settings_page, driver):
    if not settings_page["config_done"]:
        print("Settings being done only the first time")
        settings_page["config_done"] = True


@pytest.fixture()
def login():
    print("login called")
    return "login"


@pytest.fixture()
def logged_in_driver(init, driver, login):
    print("logged in driver is ready")
    return (init, driver, login)


@pytest.fixture()
def non_logged_in_driver(init, driver):
    print("non logged in driver is ready")
    return (init, driver)


def test_1(logged_in_driver):
    print("test_1")


def test_2(non_logged_in_driver):
    print("test_2")

输出如下

test.py driver called
Settings being done only the first time
init called
login called
logged in driver is ready
.test_1
driver called
init called
non logged in driver is ready
.test_2

所以你可以看到设置只发生一次

答案 1 :(得分:0)

此解决方法尝试在不创建会话级别装置的情况下实现相同的结果。它可以满足您对上面评论中提到的called only once before first test case设置的需求。

创建conftest.py并放置以下内容:

import pytest


def pytest_sessionstart(session):
    print("Initialising Session Level Settings")
    login = "A specialized login"
    settings(login).create_settings()


@pytest.fixture()
def get_driver():
    print("Get Driver")


@pytest.fixture()
def login(get_driver):
    print("Login")


@pytest.fixture()
def settings(login):
    print("Settings")

    class Setter:
        def create_settings(self):
            print("[Success] Settings created!")
    return Setter()

然后您可以在测试中使用它,如下所示:

def test_one(settings):
    print("Test one...")


def test_two(settings):
    print("Test two...")

执行测试应该产生接近你想要的效果(基于我到目前为止从你给出的信息中推断出来的结果)。

(py362) D:\repo\git\ens_qa>py.test -sv stackoverflow\ScopeWorkaroundTests.py
Initialising Session Level Settings
Settings
[Success] Settings created!
============================= test session starts =============================
platform win32 -- Python 3.6.2, pytest-3.2.1, py-1.4.34, pluggy-0.4.0 -- d:\virtualenv\py362\scripts\python.exe
cachedir: .cache
rootdir: D:\repo\git\ens_qa, inifile:
collected 2 items

stackoverflow/ScopeWorkaroundTests.py::test_one Get Driver
Login
Settings
Test one...
PASSED
stackoverflow/ScopeWorkaroundTests.py::test_two Get Driver
Login
Settings
Test two...
PASSED

========================== 2 passed in 0.05 seconds ===========================

(py362) D:\repo\git\ens_qa>

答案 2 :(得分:0)

我认为最简单的方法是添加一些标志并使用autouse,如下所示:

import pytest

flag = True

@pytest.fixture()
def get_driver():
    pass

@pytest.fixture()
def login(get_driver):
    pass

@pytest.fixture()
def settings(login):
    pass

@pytest.fixture(autouse = True)
def setup(settings):
    global flag
    if flag:
        settings.create_settings()
        flag = False
    pass