Fixtures
frozen_time
The frozen_timestamp and frozen_time fixtures can be used to freeze and manage time with the help of
the time-machine library:
- pytest_logikal.time.frozen_timestamp() datetime
Return the frozen timestamp.
The frozen timestamp can be set under the
frozen_timevalue in thetool.pytestsection ofpyproject.toml. It must be in%Y-%m-%d %H:%M:%Sformat. When the frozen timestamp is not configured, it defaults to the current timestamp generated at session startup.
An example configuration would be as follows:
[tool.pytest]
frozen_time = '1985-10-26 1:20:42'
- pytest_logikal.time.frozen_time() time_machine.Traveller
Freeze time and return a traveller instance for managing time.
Automatically applied to each test when a frozen timestamp has been configured.
You can manage time with the frozen_time fixture as
follows:
from datetime import datetime
from time_machine import Traveller
def test_frozen_time(frozen_time: Traveller) -> None:
destination_time = datetime(1985, 10, 26, 1, 21, 42)
frozen_time.move_to(destination_time)
assert datetime.now() == destination_time
mocker
The default installation provides the mocker fixture via pytest-mock.
browser
The browser extra provides a new
browser fixture:
- pytest_logikal.browser.browser() logikal_browser.Browser
Yield a scenario-specific
Browsersub-class instance.
Browser settings must be specified via the set_browser() decorator:
- @pytest_logikal.browser.set_browser(scenarios: Scenario | Iterable[Scenario], languages: Iterable[str] | None = None, headless: bool | None = None) Callable[[Any], Any]
Apply the given scenarios to the
browser()fixture.- Parameters:
scenarios – The scenarios to use.
languages – The languages to use. Defaults to using the scenario languages.
headless – Whether to use a headless browser instance.
You can specify browser scenarios with this decorator as follows:
from pytest_logikal import Browser, LiveURL, scenarios, set_browser
@set_browser(scenarios.desktop)
def test_single_scenario(browser: Browser, live_url: LiveURL) -> None:
browser.get(live_url('index'))
browser.check()
You can also run your test in multiple scenarios and override the scenario languages as follows:
from pytest_logikal import Browser, LiveURL, scenarios, set_browser
@set_browser([scenarios.desktop, scenarios.mobile_l], languages=['en-us', 'en-gb'])
def test_multiple_scenarios(browser: Browser, live_url: LiveURL) -> None:
browser.get(live_url('index'))
browser.check()
live_url
The django extra provides a new live_url fixture:
language
The django extra also provides the language fixture:
- pytest_logikal.django.language(request: Any) Iterable[str]
Return the currently activated language.
You may control the language for a specific test with the set_language or all_languages decorators:
- @pytest_logikal.django.set_language(*language_codes: str) Callable[[Any], Any]
Mark a test to run with each of the specified languages.
- Parameters:
*language_codes – The language codes to use.
Warning
This decorator should not be used together with the
browserfixture. Specify the language in the browser scenario or use thelanguagesparameter of theset_browserdecorator instead.Note
You must also use the
languagefixture in your test when applying this decorator.
- @pytest_logikal.django.all_languages Callable[[Any], Any]
Mark a test to run with every available language.
Warning
This decorator should not be used together with the
browserfixture. When thedjangoextra is installed, tests automatically run with all configured languages.Note
You must also use the
languagefixture in your test when applying this decorator.
timezone
The django extra also provides the timezone fixture:
You may control the time zone for a specific test with the set_timezone decorator: