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}"
'';
};