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 passthru.tests.pytest = callPackage ./tests.nix { };
80
81 # Remove .pytest_cache when using py.test in a Nix build
82 setupHook = writeText "pytest-hook" ''
83 pytestcachePhase() {
84 find $out -name .pytest_cache -type d -exec rm -rf {} +
85 }
86 appendToVar preDistPhases pytestcachePhase
87
88 # pytest generates it's own bytecode files to improve assertion messages.
89 # These files similar to cpython's bytecode files but are never laoded
90 # by python interpreter directly. We remove them for a few reasons:
91 # - files are non-deterministic: https://github.com/NixOS/nixpkgs/issues/139292
92 # (file headers are generatedt by pytest directly and contain timestamps)
93 # - files are not needed after tests are finished
94 pytestRemoveBytecodePhase () {
95 # suffix is defined at:
96 # https://github.com/pytest-dev/pytest/blob/7.2.1/src/_pytest/assertion/rewrite.py#L51-L53
97 find $out -name "*-pytest-*.py[co]" -delete
98 }
99 appendToVar preDistPhases pytestRemoveBytecodePhase
100 '';
101
102 pythonImportsCheck = [ "pytest" ];
103
104 meta = with lib; {
105 description = "Framework for writing tests";
106 homepage = "https://docs.pytest.org";
107 changelog = "https://github.com/pytest-dev/pytest/releases/tag/${version}";
108 teams = [ teams.python ];
109 license = licenses.mit;
110 };
111}