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 30let 31 self = buildPythonPackage rec { 32 pname = "pytest"; 33 version = "7.4.4"; 34 pyproject = true; 35 36 src = fetchPypi { 37 inherit pname version; 38 hash = "sha256-LPAAWSLGrOSj4uyLQIDrDZdT/ckxB0FTMvUM6eeZQoA="; 39 }; 40 41 outputs = [ 42 "out" 43 "testout" 44 ]; 45 46 nativeBuildInputs = [ 47 setuptools 48 setuptools-scm 49 ]; 50 51 propagatedBuildInputs = [ 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 { pytest = self; }; 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 maintainers = with maintainers; [ 110 lovek323 111 madjar 112 lsix 113 ]; 114 license = licenses.mit; 115 }; 116 }; 117in 118self