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
¶
StartService
¶
build
¶
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
¶
Write session log files under tmp_path, closing any the test left open.
data_directory
¶
Put the default root of acquisition files and catalogs under tmp_path.
config_home
¶
Keep saved session settings in a config folder under tmp_path.
empty_emission_queue
¶
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
¶
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 once after the last test of each module, on the main thread.
keep_automatic_collection_off
¶
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 |
start_service
¶
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
¶
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
¶
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.
emit_queued_on_main_thread
¶
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
¶
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
¶
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
¶
Collect after the last test of each module, then release the slots of the widgets freed.