nixpkgs/nixos/doc/manual/development
Maximilian Bosch a1dfaf51e2
nixos/test-driver: integrate Python unittest assertions
Replaces / Closes #345948

I tried to integrate `pytest` assertions because I like the reporting,
but I only managed to get the very basic thing and even that was messing
around a lot with its internals.

The approach in #345948 shifts too much maintenance effort to us, so
it's not really desirable either.

After discussing with Benoit on Ocean Sprint about this, we decided that
it's probably the best compromise to integrate `unittest`: it also
provides good diffs when needed, but the downside is that existing tests
don't benefit from it.

This patch essentially does the following things:

* Add a new global `t` that is an instance of a `unittest.TestCase`
  class. I decided to just go for `t` given that e.g.
  `tester.assertEqual` (or any other longer name) seems quite verbose.

* Use a special class for errors that get special treatment:
  * The traceback is minimized to only include frames from the
    testScript: in this case I don't really care about anything else and
    IMHO that's just visual noise.

    This is not the case for other exceptions since these may indicate a
    bug and then people should be able to send the full traceback to the
    maintainers.
  * Display the error, but with `!!!` as prefix to make sure it's
    easier to spot in between other logs.

This looks e.g. like

    !!! Traceback (most recent call last):
    !!!   File "<string>", line 7, in <module>
    !!!     foo()
    !!!   File "<string>", line 5, in foo
    !!!     t.assertEqual({"foo":[1,2,{"foo":"bar"}]},{"foo":[1,2,{"bar":"foo"}],"bar":[1,2,3,4,"foo"]})
    !!!
    !!! NixOSAssertionError: {'foo': [1, 2, {'foo': 'bar'}]} != {'foo': [1, 2, {'bar': 'foo'}], 'bar': [1, 2, 3, 4, 'foo']}
    !!! - {'foo': [1, 2, {'foo': 'bar'}]}
    !!! + {'bar': [1, 2, 3, 4, 'foo'], 'foo': [1, 2, {'bar': 'foo'}]}
    cleanup
    kill machine (pid 9)
    qemu-system-x86_64: terminating on signal 15 from pid 6 (/nix/store/wz0j2zi02rvnjiz37nn28h3gfdq61svz-python3-3.12.9/bin/python3.12)
    kill vlan (pid 7)
    (finished: cleanup, in 0.00 seconds)

Co-authored-by: bew <bew@users.noreply.github.com>
2025-03-20 12:30:58 +00:00
..
activation-script.section.md
assertions.section.md
bootspec.chapter.md nixos/activation/bootspec: fix style 2024-06-05 11:14:36 +08:00
building-parts.chapter.md
developing-the-test-driver.chapter.md
development.md
etc-overlay.section.md
freeform-modules.section.md
importing-modules.section.md nixos/eval-config: Deprecate NIXOS_EXTRA_MODULE_PATH 2024-10-18 17:04:35 +02:00
linking-nixos-tests-to-packages.section.md
meta-attributes.section.md nixos/eval-config: Deprecate NIXOS_EXTRA_MODULE_PATH 2024-10-18 17:04:35 +02:00
nixos-tests.chapter.md
non-switchable-systems.section.md Revert "NixOS apply script" 2024-11-01 20:18:09 -04:00
option-declarations.section.md doc/manual: replace bad advice about package options and overlays 2025-01-08 12:49:40 -08:00
option-def.section.md
option-types.section.md lib.types: add luaInline 2025-03-15 10:29:28 +00:00
replace-modules.section.md
running-nixos-tests-interactively.section.md nixos/manual: add hint to interactive testing about Internet access 2024-08-31 14:40:14 +00:00
running-nixos-tests.section.md
settings-options.section.md formats.lua: init 2025-03-19 21:18:49 +00:00
sources.chapter.md
testing-installer.chapter.md treewide: use pkgs.nixos-enter instead of config.system.build.nixos-enter 2024-09-21 16:13:36 +03:00
unit-handling.section.md Document the logic around X-NotSocketActivated in the manual 2024-12-09 08:57:47 -05:00
what-happens-during-a-system-switch.chapter.md Revert "NixOS apply script" 2024-11-01 20:18:09 -04:00
writing-documentation.chapter.md
writing-modules.chapter.md nixos/doc: Split mkdir mode into chmod command for clarity (#236740) 2024-11-01 11:09:52 +08:00
writing-nixos-tests.section.md nixos/test-driver: integrate Python unittest assertions 2025-03-20 12:30:58 +00:00