python - Py.test fixture: Use function fixture in scope fixture -
i facing small issue pytest fixtures, appreciate help.
i have few function fixtures mentioned below. simplicity have not show implementation.
@pytest.fixture() def get_driver(): pass @pytest.fixture() def login(get_driver): pass @pytest.fixture() def settings(login): pass
the problem need 1 more (session level) fixture setup before run first test case. (actually start testing). i.e. go settings page , create few settings. (after login)
now problem can't using session level fixture, can't use function level fixture in session level. or can i?
@pytest.fixture(scope="session") def setup(settings): settings.create_settings() pass
you need use workaround. action needs done in function
scoped fixture autouse
set true
.
you need initialize variable in session
based fixture, check if settings
have been done or not. if not done, settings , change flag false
below working example
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 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 ready") return (init, driver, login) @pytest.fixture() def non_logged_in_driver(init, driver): print("non logged in driver ready") return (init, driver) def test_1(logged_in_driver): print("test_1") def test_2(non_logged_in_driver): print("test_2")
output below
test.py driver called settings being done first time init called login called logged in driver ready .test_1 driver called init called non logged in driver ready .test_2
so can see settings happens once
wiki
Comments
Post a Comment