|
| 1 | +```{eval-rst} |
| 2 | +:og:description: Learn how to use Nox to run tests for your Python package |
| 3 | + locally across multiple Python versions and operating systems. |
| 4 | +:og:title: Run tests for your Python package with Nox |
| 5 | +``` |
| 6 | + |
| 7 | +# Run tests with Nox |
| 8 | + |
| 9 | +**Nox** is a Python-based automation tool for running tests across multiple |
| 10 | +Python versions and managing isolated test environments. If you prefer |
| 11 | +Python-driven configuration over TOML, or need complex automation workflows, |
| 12 | +Nox is an excellent choice. |
| 13 | + |
| 14 | +For more information about Nox, see the |
| 15 | +[official Nox documentation](https://nox.thea.codes/) or the |
| 16 | +[Scientific Python guide to testing](https://scientific-python.org/tools/testing). |
| 17 | + |
| 18 | +## Why Nox? |
| 19 | + |
| 20 | +**Nox** is a great automation tool because it: |
| 21 | + |
| 22 | +* Is Python-based, making it accessible if you already know Python |
| 23 | +* Will create isolated environments to run workflows |
| 24 | +* Supports complex, custom automation beyond standard testing |
| 25 | +* Is flexible and powerful for intricate build and test scenarios |
| 26 | + |
| 27 | +`nox` simplifies creating and managing testing environments. With `nox`, you |
| 28 | +can set up virtual environments and run tests across Python versions using the |
| 29 | +environment manager of your choice with a single command. |
| 30 | + |
| 31 | +## Set up Nox |
| 32 | + |
| 33 | +To get started with Nox, you create a `noxfile.py` file at the root of your |
| 34 | +project directory. You then define commands using Python functions. |
| 35 | + |
| 36 | +:::{note} |
| 37 | +Nox installations |
| 38 | + |
| 39 | +When you install and use Nox to run tests across different Python versions, |
| 40 | +Nox will create and manage individual `venv` environments for each Python |
| 41 | +version that you specify in the Nox function. Nox will manage each environment |
| 42 | +on its own. |
| 43 | +::: |
| 44 | + |
| 45 | +Nox can also be used for other development tasks such as building |
| 46 | +documentation, creating your package distribution, and testing installations |
| 47 | +across both PyPI-related environments (e.g., venv, virtualenv) and `conda` |
| 48 | +(e.g., `conda-forge`). |
| 49 | + |
| 50 | +## Test environments |
| 51 | + |
| 52 | +By default, `nox` uses Python's built-in `venv` environment manager. A virtual |
| 53 | +environment (`venv`) is a self-contained Python environment that allows you to |
| 54 | +isolate and manage dependencies for different Python projects. It helps ensure |
| 55 | +that project-specific libraries and packages do not interfere with each other, |
| 56 | +promoting a clean and organized development environment. |
| 57 | + |
| 58 | +### Nox with venv environments |
| 59 | + |
| 60 | +Below is an example of setting up Nox to run tests using `venv`, which is the |
| 61 | +built-in environment manager that comes with base Python. |
| 62 | + |
| 63 | +Note that the example below assumes that you have setup your `pyproject.toml` |
| 64 | +to declare test dependencies using `project.optional-dependencies`: |
| 65 | + |
| 66 | +```toml |
| 67 | +[build-system] |
| 68 | +requires = ["hatchling"] |
| 69 | +build-backend = "hatchling.build" |
| 70 | + |
| 71 | +[project] |
| 72 | +name = "pyosPackage" |
| 73 | +version = "0.1.0" |
| 74 | +dependencies = [ |
| 75 | + "geopandas", |
| 76 | + "xarray", |
| 77 | +] |
| 78 | + |
| 79 | +[project.optional-dependencies] |
| 80 | +tests = ["pytest", "pytest-cov"] |
| 81 | +``` |
| 82 | + |
| 83 | +With this setup, you can use `session.install(".[tests]")` to install your |
| 84 | +test dependencies. Notice that below one single Nox session allows you to run |
| 85 | +your tests on 4 different Python environments (Python 3.9, 3.10, 3.11, and |
| 86 | +3.12). |
| 87 | + |
| 88 | +:::{note} |
| 89 | +For this to run you will need to have python3.9, python3.10, python3.11, and |
| 90 | +python3.12 installed on your computer. Otherwise nox will skip running tests |
| 91 | +for whatever versions are missing. |
| 92 | +::: |
| 93 | + |
| 94 | +```python |
| 95 | +# This code would live in a noxfile.py file located at the root of your |
| 96 | +# project directory |
| 97 | +import nox |
| 98 | + |
| 99 | +@nox.session(python=["3.9", "3.10", "3.11", "3.12"]) |
| 100 | +def test(session): |
| 101 | + # Install dependencies |
| 102 | + session.install(".[tests]") |
| 103 | + # Run tests |
| 104 | + session.run("pytest") |
| 105 | +``` |
| 106 | + |
| 107 | +Above you create a Nox session in the form of a function with a |
| 108 | +`@nox.session` decorator. Notice that within the decorator you declare the |
| 109 | +versions of Python that you wish to run. |
| 110 | + |
| 111 | +To run the above, you'd execute the following command, specifying which |
| 112 | +session with `--session` (sometimes shortened to `-s`). Your function above is |
| 113 | +called `test`, therefore the session name is `test`: |
| 114 | + |
| 115 | +```bash |
| 116 | +nox --session test |
| 117 | +``` |
| 118 | + |
| 119 | +### Nox with conda / mamba |
| 120 | + |
| 121 | +Below is an example for setting up Nox to use mamba (or conda) for your |
| 122 | +environment manager. Unlike venv, conda can automatically install the various |
| 123 | +versions of Python that you need. You won't need to install all four Python |
| 124 | +versions if you use conda/mamba, like you do with `venv`. |
| 125 | + |
| 126 | +:::{note} |
| 127 | +For `conda` to work with `nox`, you will need to ensure that either `conda` or |
| 128 | +`mamba` is installed on your computer. |
| 129 | +::: |
| 130 | + |
| 131 | +```python |
| 132 | +# This code should live in your noxfile.py file |
| 133 | +import nox |
| 134 | + |
| 135 | +# The syntax below allows you to use mamba / conda as your environment |
| 136 | +# manager. If you use this approach, you don't have to worry about installing |
| 137 | +# different versions of Python |
| 138 | + |
| 139 | +@nox.session(venv_backend='mamba', python=["3.9", "3.10", "3.11", "3.12"]) |
| 140 | +def test_mamba(session): |
| 141 | + """Nox function that installs dev requirements and runs tests on Python |
| 142 | + 3.9 through 3.12. |
| 143 | + """ |
| 144 | + # Install dev requirements |
| 145 | + session.conda_install(".[tests]") |
| 146 | + # Run tests using any parameters that you need |
| 147 | + session.run("pytest") |
| 148 | +``` |
| 149 | + |
| 150 | +To run the above session you'd use: |
| 151 | + |
| 152 | +```bash |
| 153 | +nox --session test_mamba |
| 154 | +``` |
| 155 | + |
| 156 | +## Hatch vs Nox |
| 157 | + |
| 158 | +If you're trying to decide between Hatch and Nox, see the |
| 159 | +[comparison and recommendations on the main testing page](run-tests.md#nox-vs-hatch-choosing-the-right-tool). |
| 160 | + |
| 161 | +## In summary |
| 162 | + |
| 163 | +* **Choose Hatch** if you're already using Hatch for packaging and want |
| 164 | + everything in one place |
| 165 | +* **Choose Nox** if you need maximum flexibility, prefer Python-driven |
| 166 | + configuration, or need complex automation workflows |
| 167 | + |
| 168 | +## Next steps |
| 169 | + |
| 170 | +Now that you understand how to run tests locally with Nox, you can learn about |
| 171 | +[running tests automatically with continuous integration](tests-ci) or |
| 172 | +[running tests with Hatch](run-tests.md). |
0 commit comments