Skip to content

Commit 0960184

Browse files
committed
Fix: edits from review
1 parent 0455fa7 commit 0960184

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

images/python-package-test-tools.png

93.8 KB
Loading

images/test-tools.png

-49.6 KB
Binary file not shown.

tests/run-tests.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ is working as expected. However, it's also important to think about your code ru
99
On this page, you will learn about the tools that you can use to both run tests in isolated environments and across
1010
Python versions.
1111

12+
13+
1214
### Tools to run your tests
1315

1416
There are three types of tools that will make is easier to setup and run your tests in various environments:
@@ -18,7 +20,7 @@ There are three types of tools that will make is easier to setup and run your te
1820
3. **Continuous Integration (CI):** is the last tool that you'll need to run your tests. CI will not only allow you to replicate any automated builds you create using nox or tox to run your package in different Python environments. It will also allow you to run your tests on different operating systems (Windows, Mac and Linux). [We discuss using CI to run tests here](tests-ci).
1921

2022
:::{figure-md}
21-
![Figure showing three boxes - the first has Test Frameworks in it, the second Test Runner and the third Continuous Integration....](../images/test-tools.png)
23+
![Figure showing three boxes - the first has Test Frameworks in it, the second Test Runner and the third Continuous Integration....](../images/python-package-test-tools.png)
2224

2325
There are three types of tools that will help you develop and run your tests. Test frameworks like pytest
2426
provide syntax and a **framework** for you to write and
@@ -97,9 +99,14 @@ with it. Make also won't manage environments for you like **nox** will do.
9799
set up virtual environments, and run tests across Python versions using the environment manager of your choice with a
98100
single command.
99101

102+
:::{note} Nox Installations
103+
104+
When you use nox to run tests across different Python versions, nox will create and manage individual `venv` environments for each Python version that you specify in the nox function. It will setup everything that you need to run tests in each environment for you.
105+
:::
106+
100107
Nox can also be used for other development tasks such as building
101-
documentation, creating your package distribution, and testing across various
102-
environment managers such as `conda` and `pip`.
108+
documentation, creating your package distribution, and testing installations
109+
across both PyPI related environments (e.g. venv, virtualenv) and `conda` (e.g. `conda-forge`).
103110

104111
## Test Environments
105112

@@ -113,17 +120,24 @@ Note that for the code below to work, you need to have all 4 versions of Python
113120

114121
### Nox with venv environments
115122

116-
```{admonition} TODO:
123+
```{todo}
117124
TODO: add some tests above and show what the output would look like in the examples below...
118125
```
119126

120127
Below is an example of setting up nox to run tests using `venv` which is the built in environment manager that comes with base Python.
121128

122129
Note that the example below assumes that you have [setup your `pyproject.toml` to declare test dependencies in a way that pip
123-
can understand](../package-structure-code/declare-dependencies.md). An example of that setup is below.
130+
can understand](../package-structure-code/declare-dependencies.md). An example
131+
of that setup is below.
124132

125133
```toml
134+
[build-system]
135+
requires = ["hatchling"]
136+
build-backend = "hatchling.build"
137+
126138
[project]
139+
name = "pyosPackage"
140+
version = "0.1.0"
127141
dependencies = [
128142
"geopandas",
129143
"xarray",
@@ -157,9 +171,9 @@ Above you create a nox session in the form of a function
157171
with a `@nox.session` decorator. Notice that within the decorator you declare the versions of python that you
158172
wish to run.
159173

160-
To run the above you'd use the command where `-s` stands for
161-
session. Your function above is called test, therefore
162-
the session name is test.
174+
To run the above you'd use the command where `--session`. You may also see
175+
people using the shortcut for session `-s`. Your function above
176+
is called test, therefore the session name is test.
163177

164178
```
165179
nox -s test

tests/tests-ci.md

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,22 @@ locally.
1919

2020
## Example GitHub action that runs tests
2121

22-
Below is an example github action that runs tests using nox
22+
Below is an example GitHub action that runs tests using nox
2323
across both Windows, Mac and Linux and on Python versions
2424
3.9-3.11. It also includes two steps that make your build more
2525
efficient so your dependencies aren't downloaded multiple times.
2626

27+
To work properly, this file should be located in a root directory of your
28+
GitHub repository:
29+
30+
```bash
31+
pyospackage/
32+
├──.github/
33+
└── workflows/
34+
└── run-tests.yml # The name of this file can be whatever you wish
35+
```
36+
37+
2738
```yaml
2839
name: Pytest unit/integration
2940

@@ -50,27 +61,10 @@ jobs:
5061

5162
steps:
5263
- uses: actions/checkout@v4
53-
with:
54-
# fetch more than the last single commit to help scm generate proper version
55-
fetch-depth: 20
5664
- name: Set up Python ${{ matrix.python-version }}
5765
uses: actions/setup-python@v4
5866
with:
5967
python-version: ${{ matrix.python-version }}
60-
cache: pip # By adding cache here, you are telling actions to reuse installed dependencies rather than re-downloading and installing them each time. This speeds up your workflow
61-
62-
# This step and the step below are an optional steps to cache variables to make your build faster / more efficient
63-
- name: Set Variables
64-
id: set_variables
65-
shell: bash
66-
run: |
67-
echo "PY=$(python -c 'import hashlib, sys;print(hashlib.sha256(sys.version.encode()+sys.executable.encode()).hexdigest())')" >> $GITHUB_OUTPUT
68-
echo "PIP_CACHE=$(pip cache dir)" >> $GITHUB_OUTPUT
69-
- name: Cache dependencies
70-
uses: actions/cache@v3
71-
with:
72-
path: ${{ steps.set_variables.outputs.PIP_CACHE }}
73-
key: ${{ runner.os }}-pip-${{ steps.set_variables.outputs.PY }}
7468
- name: Install dependencies
7569
run: |
7670
python -m pip install --upgrade pip
@@ -80,7 +74,8 @@ jobs:
8074
- name: Run tests with pytest & nox
8175
run: |
8276
nox -s tests-${{ matrix.python-version }}
83-
# You only need to upload code coverage once to codecov
77+
# You only need to upload code coverage once to codecov unless you have a
78+
# more complex build that you need coverage for.
8479
- name: Upload coverage to Codecov
8580
if: ${{ matrix.os == 'ubuntu-latest' && matrix.python-version == '3.10'}}
8681
uses: codecov/codecov-action@v3

0 commit comments

Comments
 (0)