Testing
Every non-trivial class has tests. No test needs a network or a device, except the hardware tests, which you start on purpose. The tests use xUnit.
Categories
Section titled “Categories”| Category | Run with | What |
|---|---|---|
| (none) | manage test unit |
Fast tests with fakes. CI runs them on Windows, Linux and macOS for every release |
Perf |
manage test perf |
100,000 devices, 50,000 tasks, with a time budget |
Hardware |
manage test hardware |
Read-only, against your cameras |
RealModel |
manage test models |
Real downloaded models and photos (Vision Center: RF-DETR medium, two COCO photos). The build downloads them once into the model cache, SHA-256 checked; without them the tests skip |
Gpu |
manage test gpu |
The vision add-ons on the graphics card (DirectML on Windows x64, CUDA on Linux x64) with the real models, and Text recognition and Vision Center at once in one process. Skip with the reason without a card. Never in manage test all or CI; run one at a time, nothing else on the card |
RecipientStack |
manage test recipients |
Real MQTT, Modbus and OPC UA servers in Docker, connect and write once to each. Also HardwareWrite, so no other run and no CI touches them |
HardwareWrite |
never on a device from manage |
Change a device. Only on purpose, only on a test device |
Mark a test with [Trait("Category", "Perf")] and so on. A pull request runs the unit tests on Linux.
Task plugins with fakes
Section titled “Task plugins with fakes”A task plugin needs two fakes: an IVapixClient that answers like the device, and an ITaskExecutionContext that
records the steps. The package Oadm.Sdk.Testing has both (FakeCamera, FakeTaskContext), a device (FakeDevice)
and StepRun, which ends the task exactly like the server: success adds the step Completed, a failure fails the running
step and skips the rest. FakeCamera answers parameter reads and writes, basic device information and the API list
itself, and records every request; Respond answers anything else.
var camera = new FakeCamera();var ctx = new FakeTaskContext(camera);
await StepRun.RunAsync(ctx.Steps, () => plugin.ExecuteAsync(ctx, new FakeDevice(), """{"name":"Entrance"}""", CancellationToken.None));
Assert.Equal( ["Check compatibility: Done", "Read the current name: Done", "Set the name: Done", "Verify the name: Done", "Completed: Done"], StepRun.Lines(ctx.Steps));The task plugin tutorial has the complete tests. For devices at a site the package
has FakeDeviceNetwork (pings, open ports, names, OADM’s address; FakeTaskContext.Network), FakeSites and
FakePluginNetwork (FakeCoreContext.FakeSites, FakeNetwork) with in-memory UDP and TCP relays (IsReleased
after ReleaseAsync, IsDisposed after a dispose): see
Sites. Test at least:
- the happy path, step by step;
- a device without the API: nothing is written, the error says “Nothing was changed”;
- invalid input: it fails before the first request;
CanRun,NotSupportedReasonandGetTaskName.
Feed parsers with answers recorded from real devices (Fixtures/), not with hand-written ones. Note the device and AXIS
OS a fixture came from.
Code that waits takes a TimeProvider. Tests pass a FakeTimeProvider (Microsoft.Extensions.TimeProvider.Testing)
and advance it, so a 3-minute timeout is tested in milliseconds.
A test never depends on how fast the machine is or what runs next to it:
- Wait for what another thread does with
Eventually.TrueAsync(() => condition, "what")fromOadm.Sdk.Testing. It checks every 10 ms and gives up after 30 s, so it returns as soon as the condition holds. Never wait a fixed time and then assert. - Check speed only in a
Perftest, with a generous budget. - Restore static settings a test changes, and use unique folders, ports and file names per test.
Core plugins
Section titled “Core plugins”Start the server part with FakeCoreContext (Oadm.Sdk.Testing: devices, a FakeCamera per device, settings, the
published events, the tasks it starts) and call InvokeAsync like the page would. Its tasks stay active until
EndTask(id, TaskEndState.Failed, "message") ends one and sets what GetOutcomeAsync answers. Its records are a
MemoryPluginRecords (PluginRecords), its event publisher is null until you set a FakeEventPublisher, like a host
without event rules. For events, the
plugin test projects use the server’s real PluginEventHub and an in-memory page context; the I/O Monitor and Image
Health tests show the pattern. For the page, construct the view model with a fake ICorePluginClientContext and check
what it shows after each answer and event.
Call through the context to get the host’s rules: ctx.InvokeAsync(plugin, ctx.CallAsGroup([inside.Id]), method, payload) calls as a user with a device group. Every plugin runs a PluginCanary: it calls each method with a device
outside the user’s group and fails when the method reaches it. See Device groups.
Tests with their own device and VAPIX fakes call with FakeCallContext.Over(devices, vapix) or
FakeCallContext.OverGroup(deviceIds, devices, vapix).
Headless UI tests
Section titled “Headless UI tests”Dialogs and pages are rendered offscreen with Avalonia headless, with the real theme. A test can click buttons through the view model, check what is shown and save a screenshot:
var session = HeadlessUnitTestSession.StartNew(typeof(HeadlessEntry));await session.Dispatch(() =>{ var view = new ImageHealthView(); var window = Headless.Host(view); // the real host page around the view window.Show(); view.DataContext = viewModel; Headless.Pump(); Assert.Equal(7, viewModel.Rows.Count); Headless.Capture(window, "image-health-dashboard.png"); window.Close();}, CancellationToken.None);A body that awaits must return a value. Dispatch has no overload for a plain Task: an async () => { ... } without
a value is only started, so every assertion in it is lost, also the ones before the first await. End it with
return true;:
await session.Dispatch(async () =>{ await viewModel.LoadAsync(CancellationToken.None); Assert.Equal(7, viewModel.Rows.Count); return true;}, CancellationToken.None);HeadlessDispatchConventionTests (in tests/Oadm.ApiDocs.Tests) fails on an async body without a value in tests/
and samples/.
HeadlessEntry, Headless.Host, Pump and Capture are in the Support.cs of a page plugin’s tests, e.g.
tests/Oadm.Plugins.ImageHealth.Tests/Support.cs; copy them.
Screenshots are written only when OADM_SCREENSHOT_DIR is set:
OADM_SCREENSHOT_DIR=artifacts/screens ./manage.sh test unitLook at the screenshots; assert behaviour and content, never pixel positions or sizes. OADM_TEST_APPEARANCE=Light
renders the light appearance. UI is only tested this way: never automate the real desktop.
A server can manage 100,000 devices. Every feature with a device list has a Perf test with DeviceLimits.MaxDevices (100,000) fake devices that
keeps search, select all, the summary and updates fast, writes its measured times to the test output and has a
generous but meaningful budget.
A few Perf tests run for several minutes (a task on 100,000 devices: about 9). With --blame-hang-timeout, give
Perf runs 20 minutes; 5 minutes is enough for the unit tests.
Real cameras
Section titled “Real cameras”Copy dev-cameras.example.yaml to dev-cameras.yaml at the repository root (git-ignored: it holds passwords) and
list your devices:
cameras: - address: 192.168.0.90 user: root password: change-me scheme: https note: "AXIS P3265-V lab"Tests marked [HardwareFact] and [Trait("Category", "Hardware")] read from them and are skipped when the file is
missing (CI). They must not change the devices. Tests that write are HardwareWrite, need their own environment
variables (for example OADM_STORAGE_WRITE_URL), and put the device back as it was. Run them only on a test device,
and write in the plugin’s README.md what you verified on which model and AXIS OS.
Try it in the app
Section titled “Try it in the app”./manage.sh run devbuilds everything and starts the server and the client. Plugins come fromartifacts/plugins/../manage.sh run client --fakestarts the client with sample devices and no server, for the host pages. Most built-in plugin pages have a backend inFakeOadmApi; your plugin needs the real server.