summary refs log tree commit diff
path: root/doc/contributing/coding-conventions.chapter.md
diff options
context:
space:
mode:
authordavidak <git@davidak.de>2021-04-24 17:01:05 +0200
committerdavidak <git@davidak.de>2021-04-24 18:39:45 +0200
commitc357fbeb5176c233aa0b36426800ab632eddf217 (patch)
tree3adb8f0417d2d59e75b6bf7784ebe738eef2827d /doc/contributing/coding-conventions.chapter.md
parentabd57b544e59b54a24f930899329508aa3ec3b17 (diff)
downloadnixpkgs-c357fbeb5176c233aa0b36426800ab632eddf217.tar
nixpkgs-c357fbeb5176c233aa0b36426800ab632eddf217.tar.gz
nixpkgs-c357fbeb5176c233aa0b36426800ab632eddf217.tar.bz2
nixpkgs-c357fbeb5176c233aa0b36426800ab632eddf217.tar.lz
nixpkgs-c357fbeb5176c233aa0b36426800ab632eddf217.tar.xz
nixpkgs-c357fbeb5176c233aa0b36426800ab632eddf217.tar.zst
nixpkgs-c357fbeb5176c233aa0b36426800ab632eddf217.zip
doc: add instructions for creating package tests
Diffstat (limited to 'doc/contributing/coding-conventions.chapter.md')
-rw-r--r--doc/contributing/coding-conventions.chapter.md70
1 files changed, 70 insertions, 0 deletions
diff --git a/doc/contributing/coding-conventions.chapter.md b/doc/contributing/coding-conventions.chapter.md
index eccf4f7436e..8e35e45f61d 100644
--- a/doc/contributing/coding-conventions.chapter.md
+++ b/doc/contributing/coding-conventions.chapter.md
@@ -512,3 +512,73 @@ If you do need to do create this sort of patch file, one way to do so is with gi
     ```ShellSession
     $ git diff > nixpkgs/pkgs/the/package/0001-changes.patch
     ```
+
+## Package tests {#sec-package-tests}
+
+Tests are important to ensure quality and make reviews and automatic updates easy.
+
+Nix package tests are a lightweight alternative to [NixOS module tests](https://nixos.org/manual/nixos/stable/#sec-nixos-tests). They can be used to create simple integration tests for packages while the module tests are used to test services or programs with a graphical user interface on a NixOS VM. Unittests that are included in the source code of a package should be executed in the `checkPhase`.
+
+### Writing package tests {#ssec-package-tests-writing}
+
+This is an example using the `phoronix-test-suite` package with the current best practices.
+
+Add the tests in `passthru.tests` to the package definition like this:
+
+```nix
+{ stdenv, lib, fetchurl, callPackage }:
+
+stdenv.mkDerivation {
+  …
+
+  passthru.tests = {
+    simple-execution = callPackage ./tests.nix { };
+  };
+
+  meta = { … };
+}
+```
+
+Create `tests.nix` in the package directory:
+
+```nix
+{ runCommand, phoronix-test-suite }:
+
+let
+  inherit (phoronix-test-suite) pname version;
+in
+
+runCommand "${pname}-tests" { meta.timeout = 3; }
+  ''
+    # automatic initial setup to prevent interactive questions
+    ${phoronix-test-suite}/bin/phoronix-test-suite enterprise-setup >/dev/null
+    # get version of installed program and compare with package version
+    if [[ `${phoronix-test-suite}/bin/phoronix-test-suite version` != *"${version}"*  ]]; then
+      echo "Error: program version does not match package version"
+      exit 1
+    fi
+    # run dummy command
+    ${phoronix-test-suite}/bin/phoronix-test-suite dummy_module.dummy-command >/dev/null
+    # needed for Nix to register the command as successful
+    touch $out
+  ''
+```
+
+### Running package tests {#ssec-package-tests-running}
+
+You can run these tests with:
+
+```ShellSession
+$ cd path/to/nixpkgs
+$ nix-build -A phoronix-test-suite.tests
+```
+
+### Examples of package tests {#ssec-package-tests-examples}
+
+Here are examples of package tests:
+
+- [Jasmin compile test](https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/compilers/jasmin/test-assemble-hello-world/default.nix)
+- [Lobster compile test](https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/compilers/lobster/test-can-run-hello-world.nix)
+- [Spacy annotation test](https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/python-modules/spacy/annotation-test/default.nix)
+- [Libtorch test](https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/libraries/science/math/libtorch/test/default.nix)
+- [Multiple tests for nanopb](https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/libraries/nanopb/default.nix)