How to test a plugin¶
You can test the components and services of a plugin with the fixtures
redsun uses for its own tests, from
redsun.testing.
Prerequisites¶
You need a plugin package with pytest tests, as
Package components as a plugin describes. The blocks
below are parts of one test module, and the whole module is at the end.
Install the fixtures¶
Add redsun with the testing extra to the plugin's development
dependencies:
pytest-asyncio runs the async tests, such as the service test below.
Load them¶
Load the module as a pytest plugin in pyproject.toml:
From then on every test keeps its session settings and session log files under
its own tmp_path. So do the acquisition files and catalogs a session puts in
their default location, and every test drops the psygnal emissions it left
queued for another thread. A session whose configuration names its own
storage directory still writes there. Nothing loads the module unless a suite
asks for it, so these fixtures never reach a project that only installs
redsun.
The fixtures also turn Python's automatic garbage collection off for the
whole run and collect once after each test file, because pyside6 ends the
process when a widget is freed on a background thread. Three things follow
for your tests:
- A test that checks an object in a reference cycle was freed calls
gc.collect()itself. -
A test that turns automatic collection on or off restores the state it found, or it fails with a message saying so:
-
What a test leaves in a reference cycle, a widget or a large array, stays in memory until the end of its file.
Garbage collection in the tests gives the reason.
A session built in a wider fixture isn't covered
The folder fixtures run for each test, so they don't cover a session built in a fixture scoped to a module or the whole run. Build the session in a per-test fixture.
Test Qt components¶
A session with a window needs a QApplication, and only an extra of redsun
installs Qt, so the fixtures for Qt components are a second plugin,
redsun.testing.qt. Install
it with the testing-qt extra and the Qt binding your plugin uses:
Write pyside instead of pyqt for pyside6. Then load the Qt plugin together
with the core one, whose fixtures it needs:
The plugin imports Qt as soon as pytest loads it, before the env table of
pytest-env sets its variables. A suite with both bindings installed
therefore sets QT_API in the environment pytest starts in, such as the
set_env of tox, rather than in pytest-env.
A test then asks for the qapp fixture to get the
run's one QApplication. After every test that asks for it, the plugin
checks the application: a test that left its stylesheet, style, font or
palette changed fails. This is how you find a
hook provider that doesn't put back in shutdown what it
changed.
pytest-qt defines a qapp fixture too, and a suite can load both plugins.
pytest then uses one of the two qapp fixtures, and either works: the
check still runs, and psygnal emissions queued from another thread still
reach the main thread.
Build a session¶
Ask for build. It builds a session class with the
configuration you give it, and shuts the session down when the test ends. The
session below has a window, so the test asks for qapp too:
def test_the_session_builds_every_component(
build: BuildSession, qapp: QApplication
) -> None:
"""Build every component of the session, its devices mocked."""
session = build(MyApp, {"mock": True, "strict": True})
assert set(session.devices) == {"motor"}
assert set(session.presenters) == {"ctrl"}
assert set(session.views) == {"panel"}
mock: True connects every device to a simulated backend, as in
Run without hardware. That doesn't suit a device
whose signals come from its service when it connects, as a fastcs device's
do, because a mocked one has none. Test such a device against its service
instead.
When the test is over, build checks that each session it built has been
freed, unless the test raised, since a traceback keeps the test's variables
and with them its session. A session still alive fails the test with an error
at teardown that names it. Something that outlives the test still refers to
that session, directly or through other objects, as a component does when it
stores its session in a list at module level.
The holder can be the test itself. pytest keeps the instance of a test class
until the run ends, so a session stored on self stays alive with it: delete
the attribute when the fixture that built the session tears down. A session
made outside the test and passed to build fails too, since whatever made it
still holds it.
To find any other holder, build and shut down the session in a short script,
and call gc.collect(). Then call gc.get_referrers, which returns the
objects referring to its argument, on the session and on each referrer in
turn, leaving out the script's own variables, until you reach an object your
plugin owns.
Start a service¶
Declare the service once, with the Launch a session class
uses:
Ask for start_service and give it the name
the service is declared under, the Launch, and the transport the session
file names under services.transport:
async def test_the_stage_moves(start_service: StartService) -> None:
"""Move the stage its service serves."""
service = start_service("stage_service", STAGE, transport="pv-access")
stage = MyStage(service.prefix, name="stage")
await stage.connect()
await stage.position.set(2.0)
assert await stage.position.get_value() == 2.0
When the test ends, the fixture stops the service, releases its transport as a session does, and restores the EPICS address list the service added itself to, so the next test starts from the same list. The test reads the prefix from the service it gets back.
Channel Access services started late are not found
A Channel Access client reads its address list once, the first time the
process uses Channel Access, so under channel-access a service started
after that isn't found. Prefer pv-access for services started in tests, or
start every Channel Access service the suite needs before its first client
connects.
The example in full¶
The whole module
"""The tests of the guide "How to test a plugin"."""
from __future__ import annotations
from typing import TYPE_CHECKING
from docs.examples.connect_on_demand import MyApp
from docs.examples.device_fastcs import MyStage
from redsun import Launch
if TYPE_CHECKING:
from qtpy.QtWidgets import QApplication
from redsun.testing import BuildSession, StartService
STAGE = Launch("stage_fastcs", ready="stage ready", prefix="STAGE:")
def test_the_session_builds_every_component(
build: BuildSession, qapp: QApplication
) -> None:
"""Build every component of the session, its devices mocked."""
session = build(MyApp, {"mock": True, "strict": True})
assert set(session.devices) == {"motor"}
assert set(session.presenters) == {"ctrl"}
assert set(session.views) == {"panel"}
async def test_the_stage_moves(start_service: StartService) -> None:
"""Move the stage its service serves."""
service = start_service("stage_service", STAGE, transport="pv-access")
stage = MyStage(service.prefix, name="stage")
await stage.connect()
await stage.position.set(2.0)
assert await stage.position.get_value() == 2.0