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.4.1";
33 pyproject = true;
34
35 src = fetchPypi {
36 inherit pname version;
37 hash = "sha256-fGf9aRdIdzWe2Tcew6+KPSsEdBgYxR5emcwXQiUfqTw=";
38 };
39
40 outputs = [
41 "out"
42 "testout"
43 ];
44
45 build-system = [
46 setuptools
47 setuptools-scm
48 ];
49
50 dependencies = [
51 iniconfig
52 packaging
53 pluggy
54 pygments
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 requests
68 setuptools
69 xmlschema
70 ];
71 };
72
73 postInstall = ''
74 mkdir $testout
75 cp -R testing $testout/testing
76 '';
77
78 doCheck = false;
79 # FIXME(jade): perhaps this should be the default?
80 # https://github.com/NixOS/nixpkgs/issues/435069
81 dontWrapPythonPrograms = true;
82 passthru.tests.pytest = callPackage ./tests.nix { };
83
84 # Remove .pytest_cache when using py.test in a Nix build
85 setupHook = writeText "pytest-hook" ''
86 pytestcachePhase() {
87 find $out -name .pytest_cache -type d -exec rm -rf {} +
88 }
89 appendToVar preDistPhases pytestcachePhase
90
91 # pytest generates it's own bytecode files to improve assertion messages.
92 # These files similar to cpython's bytecode files but are never laoded
93 # by python interpreter directly. We remove them for a few reasons:
94 # - files are non-deterministic: https://github.com/NixOS/nixpkgs/issues/139292
95 # (file headers are generatedt by pytest directly and contain timestamps)
96 # - files are not needed after tests are finished
97 pytestRemoveBytecodePhase () {
98 # suffix is defined at:
99 # https://github.com/pytest-dev/pytest/blob/7.2.1/src/_pytest/assertion/rewrite.py#L51-L53
100 find $out -name "*-pytest-*.py[co]" -delete
101 }
102 appendToVar preDistPhases pytestRemoveBytecodePhase
103 '';
104
105 pythonImportsCheck = [ "pytest" ];
106
107 meta = with lib; {
108 description = "Framework for writing tests";
109 homepage = "https://docs.pytest.org";
110 changelog = "https://github.com/pytest-dev/pytest/releases/tag/${version}";
111 teams = [ teams.python ];
112 license = licenses.mit;
113 };
114}