Flakes

flake.nix

Building on top of the simple classic example the same type of structure could also be expressed in a flake.nix:

{
  description = "A very basic flake using nix-unit";

  outputs = { self, nixpkgs }: {
    libTests = {
      testPass = {
        expr = 1;
        expected = 1;
      };
    };
  };
}

And is evaluated with nix-unit like so:

$ nix-unit --flake '.#libTests'

flake checks

You can also use nix-unit in flake checks (link).

Create a tests and checks outputs.

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs";
    nix-unit.url = "github:nix-community/nix-unit";
    nix-unit.inputs.nixpkgs.follows = "nixpkgs";
  };
  outputs = { self, nixpkgs, nix-unit, ... }:
    let
      forAllSystems = nixpkgs.lib.genAttrs [
        "x86_64-linux"
        "aarch64-linux"
        "x86_64-darwin"
        "x86_64-windows"
      ];
    in
    {
      tests.testPass = { expr = 3; expected = 4; };

      checks = forAllSystems (system: {
        default = nixpkgs.legacyPackages.${system}.runCommand "tests"
          {
            nativeBuildInputs = [ nix-unit.packages.${system}.default ];
          } ''
          export HOME="$(realpath .)"
          # The nix derivation must be able to find all used inputs in the nix-store because it cannot download it during buildTime.
          nix-unit --eval-store "$HOME" \
            --extra-experimental-features flakes \
            --override-input nixpkgs ${nixpkgs} \
            --flake ${self}#tests
          touch $out
        '';
      });
    };
}

Run nix flake check and get an error as expected.

error: builder for '/nix/store/73d58ybnyjql9ddy6lr7fprxijbgb78n-nix-unit-tests.drv' failed with exit code 1;
       last 10 log lines:
       > /build/nix-20-1/expected.nix --- 1/2 --- Nix
       > 1 3
       >
       > /build/nix-20-1/expected.nix --- 2/2 --- Nix
       > 1 4
       >
       >
       >
       > 😢 0/1 successful
       > error: Tests failed
       For full logs, run 'nix log /nix/store/73d58ybnyjql9ddy6lr7fprxijbgb78n-nix-unit-tests.drv'.