简体   繁体   中英

Using fixtures in Pytest

I have the following code in the conftest.py file

@pytest.fixture(scope="session", autouse=True)
def app(request):
    global fixture
    browser = request.config.getoption("--browser")
    base_url=target['baseUrl'])
    fixture = Application(browser=browser,base_url=web_config['baseUrl'])
    print("\n BEFORE SESSION")
    fixture.session.login()
    return fixture


@pytest.fixture(scope="session", autouse=True)
def stop(request):
    def fin():
        print("\n AFTER SESSION")
        fixture.session.ensure_logout()
        fixture.destroy()
    request.addfinalizer(fin)
    return fixture

The test file looks like this. Ie fixture, I obviously do not call.

import pytest

@pytest.yield_fixture()
def setup_method():
    print("\n BEFORE METHOD")
    yield
    print("\n AFTER METHOD")

@pytest.mark.usefixtures("setup_method")
def test_add_text_element(app):
    print("\n RUN TEST")
    app.element.add_blank_page()
    app.element.add_element(element_name='Header')

But what if I need to set some other class settings? If I get another fixture, how can I use it in the test file, instead of the one used now?

All about fixtures in py.test you can find in this doc. Below you can found an example how to use fixtures. First of all don't use global . Then be careful about autouse parameter of fixtures. For setup and teardown yield_fixture will be you choice. Use usefixtures as decorator for class. Class will be good to organize your test code. You can found more information about usage in this article (RUS)

conftest.py

@pytest.yield_fixture()
def destroy_method():
    yield
    print("\n DESTROY")


@pytest.yield_fixture(scope="session", autouse=True)
def app(request):
    browser = request.config.getoption("--browser")
    fixture = Application(browser=browser, base_url=web_config['baseUrl'])
    print("\n BEFORE SESSION")
    fixture.session.login()
    yield fixture
    print("\n AFTER SESSION")
    fixture.session.ensure_logout()
    fixture.destroy()

Test file will be looks like:

@pytest.yield_fixture()
def setup_method():
    print("\n BEFORE METHOD")
    yield
    print("\n AFTER METHOD")


@pytest.fixture()
def fix1():
    return 1


@pytest.fixture()
def fix2():
    return 2


@pytest.mark.usefixtures("setup_method", "destroy_method")
class TestSuiteA:
    def test_add_text_element(self, fix1, fix2):
        print("\n RUN TEST")
        assert fix1 + 1 == fix2

From pytest docs:

"yield_fixture" functions:

Since pytest-3.0, fixtures using the normal fixture decorator can use a yield statement to provide fixture values and execute teardown code, exactly like yield_fixture in previous versions.

Marking functions as yield_fixture is still supported, but deprecated and should not be used in new code.

Reference: https://docs.pytest.org/en/latest/yieldfixture.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM