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