Skip to content

redsun.testing

Pytest fixtures for testing components, sessions and services.

A suite loads them with -p redsun.testing, in addopts or on the command line. Loading them makes every test keep session settings, session logs, acquisition data and catalogs under its tmp_path, drop the psygnal emissions it left queued, and turn automatic garbage collection off for the run, collecting after each test module on the main thread. A test that used build is collected after it too, and fails if a session build built is still alive, unless the test raised.

BuildSession

Bases: Protocol

Build a session, and hand it back typed as what was asked for.

__call__

__call__(
    container: type[SessionT] | SessionT,
    config: Source | Sequence[Source] | None = ...,
) -> SessionT

Build container, a session class made with config, or a session.

StartService

Bases: Protocol

Start a service as a session declaring it would, and hand it back.

__call__

__call__(
    name: str, launch: Launch, /, *, transport: str = ...
) -> Service

Start the service launch describes, declared under name.

transport is the one a session file names under services.transport: channel-access, pv-access or sharedbox.

build

build(
    request: FixtureRequest,
) -> Generator[BuildSession, None, None]

Return a function building a session, shut down when the test ends.

It takes a session class, made with the configuration given and laid over what the class declares, or a session already made. Sessions are shut down in reverse order; one the test shut down itself runs nothing again. A shutdown that raises does not stop the others: once every session has been shut down, the fixture raises that error, or an ExceptionGroup of all of them when more than one session raised.

Once the test is over, a session still alive fails the test with an error at teardown, unless the test itself raised. That includes a session the test keeps: one stored on self in a test class, whose instance pytest keeps until the run ends, or one made outside the test and passed in.

log_directory

log_directory(
    tmp_path: Path, monkeypatch: MonkeyPatch
) -> Generator[Path, None, None]

Write session log files under tmp_path, closing any the test left open.

data_directory

data_directory(
    tmp_path: Path, monkeypatch: MonkeyPatch
) -> Path

Put the default root of acquisition files and catalogs under tmp_path.

config_home

config_home(
    monkeypatch: MonkeyPatch, tmp_path: Path
) -> Path

Keep saved session settings in a config folder under tmp_path.

empty_emission_queue

empty_emission_queue() -> Generator[None, None, None]

Drop every psygnal emission the test left queued for another thread.

Left queued, an emission is delivered by the next test running the event loop, to whatever its target has become; a destroyed widget ends the interpreter.

no_automatic_collection

no_automatic_collection() -> Generator[None, None, None]

Turn Python's automatic garbage collection off for the whole run.

Python collects on whichever thread allocates, a background event loop included, and pyside6 aborts when a widget is destroyed off the GUI thread. With automatic collection off no thread collects by itself; collect_after_module collects on the main thread instead. A test that needs an object in a reference cycle freed calls gc.collect() itself.

collect_after_module

collect_after_module(
    no_automatic_collection: None,
) -> Generator[None, None, None]

Collect once after the last test of each module, on the main thread.

keep_automatic_collection_off

keep_automatic_collection_off(
    no_automatic_collection: None,
) -> Generator[None, None, None]

Fail a test that left automatic garbage collection on.

A test that turns it on, or off and then on again, restores the state it found before it ends. Nothing is collected here: the state is only read.

Raises:

Type Description
RuntimeError

If the test, or a session it left for build to shut down, turned automatic collection on; it is turned off again first, so the tests after it stay safe.

start_service

start_service(
    monkeypatch: MonkeyPatch,
) -> Generator[StartService, None, None]

Return a function starting a declared service, stopped when the test ends.

Once the services have stopped, their transports are released and the EPICS address lists they added themselves to are restored, as when a session ends.

inline_threads

inline_threads(
    request: FixtureRequest,
) -> Generator[ThreadExecutor, None, None]

Return a ThreadExecutor that runs each call where it is submitted.

The executor is named after the test and its pool is started, so a call's result is there when submit returns, and then callbacks run at once. Both are shut down when the test ends.

inline_processes

inline_processes(
    request: FixtureRequest,
) -> Generator[ProcessExecutor, None, None]

Return a ProcessExecutor that runs each call where it is submitted.

The executor is named after the test and its pool is started, so a call's result is there when submit returns. Each call and its answer still make a pickle round trip, so a value a real worker could not receive fails the call. Both are shut down when the test ends.

redsun.testing.qt

Pytest fixtures for testing Qt components.

A suite loads them with -p redsun.testing.qt, together with -p redsun.testing, which provides the no_automatic_collection fixture they need. Loading them makes a test using qapp that leaves the application's stylesheet, style, font or palette changed fail, and releases the slots of the widgets freed after each test module. On Windows under Qt's offscreen platform, they also point Qt at the system fonts, which that platform otherwise lacks.

The qapp here gives way to another plugin's, such as pytest-qt's, without losing the delivery of emissions queued for the main thread.

qapp

qapp() -> QApplication

Return the one QApplication of the whole run.

emit_queued_on_main_thread

emit_queued_on_main_thread(request: FixtureRequest) -> None

Deliver the psygnal emissions queued for the main thread to a test using qapp.

It asks for qapp by name, so whichever plugin supplies it, this one's or another's, has made the application before the delivery starts.

keep_application_as_found

keep_application_as_found(
    request: FixtureRequest,
) -> Generator[None, None, None]

Fail a test using qapp that left the application's stylesheet, style, font or palette changed.

A hook provider undoes in shutdown what it changed on the application, and a test that changes it by hand puts it back. A test that does not ask for the qapp fixture is not checked.

Raises:

Type Description
RuntimeError

If one of them differs after the test from before it; the stylesheet and the palette are put back first, so they do not style the tests after this one.

application_state

application_state(
    app: QApplication,
) -> tuple[str, str, str, QPalette | None]

Return app's stylesheet, style name and font, and its palette if one was set by hand.

A palette nobody set follows the style and the colour scheme, so it is left out.

release_slots_after_module

release_slots_after_module(
    no_automatic_collection: None,
) -> Generator[None, None, None]

Collect after the last test of each module, then release the slots of the widgets freed.