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_time value in the tool.pytest section of pyproject.toml. It must be in %Y-%m-%d %H:%M:%S format. 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(2015, 10, 21, 16, 29, 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 Browser sub-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:

pytest_logikal.django.live_url(name: str | None, args: Sequence[Any] | None, kwargs: dict[str, Any] | None) str

Return the path to a URL.

Parameters:
  • name – The URL pattern name.

  • args – The URL arguments to use.

  • kwargs – The URL keyword arguments to use.

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 browser fixture. Specify the language in the browser scenario or use the languages parameter of the set_browser decorator instead.

Note

You must also use the language fixture 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 browser fixture. When the django extra is installed, tests automatically run with all configured languages.

Note

You must also use the language fixture in your test when applying this decorator.

timezone

The django extra also provides the timezone fixture:

pytest_logikal.django.timezone(request: Any) Iterable[str]

Return the current time zone ID.

You may control the time zone for a specific test with the set_timezone decorator:

@pytest_logikal.django.set_timezone(*zone_ids: str) Callable[[Any], Any]

Mark a test to run with each of the specified time zones.

Parameters:

*zone_ids – The time zone IDs to use.

Note

You must also use the timezone fixture in your test when applying this decorator.