How to test assert statements with NixOS tests?

I have written a nixos module with assertions. Is there a god way to test that you get expected error when given a configuration that trigger assertions?

I have not found a way to do it with the nixos module test library. If you give a node with configuration that trigger assertions, test will not build. Build is falling with expected assertions. But we want to catch and check that error is what we expect.

I have found a way with bulltins.tryEval but then you only can check that build is falling but not check that it is falling for right reason.

I have look a round but not found any good solutions. But assume that someone have done something similar.

I found a solution that worked. Setup a nixos test where you import the module "${nixpkgs}/nixos/modules/installer/cd-dvd/channel.nix" so you have a channel inside your runner. Then use environment.etc to load config that we want to fail into test runner. We can than execute nixos-rebuild --fast dry-build 2>&1 and check that we get the expected error message.

pkgs.nixosTest {
    name = "test-that-module-fails-right";

    nodes = {
      machine = { config, lib, ... }: {
        imports = [
          # Need nix channel so we can use nixos-rebuild
          "${nixpkgs}/nixos/modules/installer/cd-dvd/channel.nix"
        ];

        environment.etc = {
          "nixos/my-module.nix" = {
            mode = "0600";
            source = ../my-module.nix;
          };

          "nixos/configuration.nix" = {
            mode = "0600";
            text = ''
            {
              imports = [ ./my-module.nix ];
              >>> Set config for my-module that shall fail <<<<<
            }
            '';
          };
        };

      };
    };

    testScript = 
    let 
      expectedError = "my expected error";
    in ''
      start_all()

      with subtest("Check that module fails the right way"):
        status, stdout = machine.execute("nixos-rebuild --fast dry-build 2>&1")
        assert "${expectedError}" in stdout, f"Expected: ${expectedError} but got {stdout}"
    '';
  };