1{ lib
2, buildPythonPackage
3, callPackage
4, pythonOlder
5, fetchPypi
6, writeText
7
8# build
9, setuptools
10, setuptools-scm
11
12# propagates
13, attrs
14, exceptiongroup
15, iniconfig
16, packaging
17, pluggy
18, py
19, tomli
20}:
21
22buildPythonPackage rec {
23 pname = "pytest";
24 version = "7.4.3";
25 pyproject = true;
26
27 src = fetchPypi {
28 inherit pname version;
29 hash = "sha256-2YnRNpgt5OOynavMg4rVgcZOjtUsEfvobd69naCBjNU=";
30 };
31
32 outputs = [
33 "out"
34 "testout"
35 ];
36
37 nativeBuildInputs = [
38 setuptools
39 setuptools-scm
40 ];
41
42 propagatedBuildInputs = [
43 attrs
44 iniconfig
45 packaging
46 pluggy
47 py
48 tomli
49 ] ++ lib.optionals (pythonOlder "3.11") [
50 exceptiongroup
51 ];
52
53 postInstall = ''
54 mkdir $testout
55 cp -R testing $testout/testing
56 '';
57
58 doCheck = false;
59 passthru.tests.pytest = callPackage ./tests.nix { };
60
61 # Remove .pytest_cache when using py.test in a Nix build
62 setupHook = writeText "pytest-hook" ''
63 pytestcachePhase() {
64 find $out -name .pytest_cache -type d -exec rm -rf {} +
65 }
66 preDistPhases+=" pytestcachePhase"
67
68 # pytest generates it's own bytecode files to improve assertion messages.
69 # These files similar to cpython's bytecode files but are never laoded
70 # by python interpreter directly. We remove them for a few reasons:
71 # - files are non-deterministic: https://github.com/NixOS/nixpkgs/issues/139292
72 # (file headers are generatedt by pytest directly and contain timestamps)
73 # - files are not needed after tests are finished
74 pytestRemoveBytecodePhase () {
75 # suffix is defined at:
76 # https://github.com/pytest-dev/pytest/blob/7.2.1/src/_pytest/assertion/rewrite.py#L51-L53
77 find $out -name "*-pytest-*.py[co]" -delete
78 }
79 preDistPhases+=" pytestRemoveBytecodePhase"
80 '';
81
82 pythonImportsCheck = [
83 "pytest"
84 ];
85
86 meta = with lib; {
87 description = "Framework for writing tests";
88 homepage = "https://docs.pytest.org";
89 changelog = "https://github.com/pytest-dev/pytest/releases/tag/${version}";
90 maintainers = with maintainers; [ domenkozar lovek323 madjar lsix ];
91 license = licenses.mit;
92 };
93}