Skip to content

Commit 1e33b44

Browse files
authored
Enh: align tests section better with our lessons (#604)
* enh: add uvx instructions * enh: bold was broken * enh: nox is awesome but default to hatch * enh: align better with lessons as taught * enh: relink and cleanup following brand guielines * Apply suggestions from code review * enh: tests * fix * Update tests/run-tests.md * Update tests/run-tests.md * Fix: cleanup links and other bugs in pages
1 parent 5266ae6 commit 1e33b44

12 files changed

Lines changed: 870 additions & 334 deletions

File tree

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ Initiatives.
2424

2525
## Contributing statement
2626

27-
2827
## How to setup
2928

3029
This repository contains the source files for the [pyOpenSci Python packaging guide](https://pyopensci.org/python-package-guide).
@@ -44,44 +43,49 @@ To build, follow these steps:
4443
1. Install `nox`
4544

4645
```console
47-
$ python -m pip install nox
46+
python -m pip install nox
4847
```
48+
4949
2. Build the documentation:
5050

5151
```console
52-
$ nox -s docs
52+
nox -s docs
5353
```
5454

5555
This should create a local environment in a `.nox` folder, build the documentation (as specified in the `noxfile.py` configuration), and the output will be in `_build/html`.
5656

5757
To build live documentation that updates when you update local files, run the following command:
5858

5959
```console
60-
$ nox -s docs-live
60+
nox -s docs-live
6161
```
6262

63+
If you are a uv user, you can also skip installing `nox` and use `uvx` instead:
64+
65+
`uvx nox -s docs-live`
66+
6367
### Building for release
6468

6569
When building for release, the docs are built multiple times for each translation,
6670
but translations are only included in the production version of the guide after some completion threshold.
6771

6872
The sphinx build environment is controlled by an environment variable `SPHINX_ENV`
6973

70-
- when `SPHINX_ENV=development` (default), sphinx assumes all languages are built,
74+
* when `SPHINX_ENV=development` (default), sphinx assumes all languages are built,
7175
and includes them in the language selector
72-
- when `SPHINX_ENV=production`, only those languages in `release_languages` (set in `conf.py`)
76+
* when `SPHINX_ENV=production`, only those languages in `release_languages` (set in `conf.py`)
7377
are built and included in the language selector.
7478

7579
Most of the time you should not need to set `SPHINX_ENV`,
7680
as it is forced by the primary nox sessions intended to be used for release or development:
7781

7882
`SPHINX_ENV=development`
79-
- `docs-live` - autobuild english
80-
- `docs-live-lang` - autobuild a single language
81-
- `docs-live-langs` - autobuild all languages
83+
* `docs-live` - autobuild english
84+
* `docs-live-lang` - autobuild a single language
85+
* `docs-live-langs` - autobuild all languages
8286

8387
`SPHINX_ENV=production`
84-
- `build-test` - build all languages for production
88+
* `build-test` - build all languages for production
8589

8690
## Contributing to this guide
8791

_static/pyos.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,21 +340,21 @@ See https://github.com/pydata/pydata-sphinx-theme/pull/1784
340340
src: url("./fonts/poppins-v20-latin-600.woff2") format("woff2"); /* Chrome 36+, Opera 23+, Firefox 39+, Safari 12+, iOS 10+ */
341341
}
342342

343-
/* nunitosans-regular - latin */
343+
/* nunitosans-variable - latin */
344344
@font-face {
345345
font-display: swap;
346346
font-family: "NunitoSans";
347347
font-style: normal;
348-
font-weight: 400;
348+
font-weight: 200 800;
349349
src: url("./fonts/NunitoSans-VariableFont.woff2") format("woff2");
350350
}
351351

352-
/* nunitosans-italic - latin */
352+
/* nunitosans-italic-variable - latin */
353353
@font-face {
354354
font-display: swap;
355355
font-family: "NunitoSans";
356356
font-style: italic;
357-
font-weight: 400;
357+
font-weight: 200 800;
358358
src: url("./fonts/NunitoSans-Italic-VariableFont.woff2") format("woff2");
359359
}
360360

conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
".pytest_cache/README.md",
177177
"vale-styles/*",
178178
"CODE_OF_CONDUCT.md",
179+
"CLAUDE.md",
179180
]
180181

181182
# For sitemap generation

index.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ We support the Python tools that scientists need to create open science workflow
1717

1818
```{only} html
1919
![GitHub release (latest by date)](https://img.shields.io/github/v/release/pyopensci/python-package-guide?color=purple&display_name=tag&style=plastic)
20-
[![](https://img.shields.io/github/stars/pyopensci/python-package-guide?style=social)](https://github.com/pyopensci/contributing-guide)
20+
[![](https://img.shields.io/github/stars/pyopensci/python-package-guide?style=social)](https://github.com/pyopensci/python-package-guide)
2121
[![DOI](https://zenodo.org/badge/556814582.svg)](https://zenodo.org/badge/latestdoi/556814582)
2222
```
2323

@@ -56,7 +56,7 @@ Community docs
5656
Publish your docs
5757
```
5858

59-
## Tutorial Series: Create a Python Package
59+
## Tutorial Series: Create a Python Package
6060

6161
The first round of our community-developed, how to create a Python package tutorial series for scientists is complete! Join our community review process or watch development of future tutorials in our [GitHub repo here](https://github.com/pyOpenSci/python-package-guide).
6262

@@ -91,6 +91,7 @@ The first round of our community-developed, how to create a Python package tutor
9191
:class-card: left-aligned
9292

9393
* [Introduction to Hatch](/tutorials/get-to-know-hatch)
94+
* [Run Python scripts using Hatch](/tutorials/run-python-scripts-hatch)
9495

9596
:::
9697
::::
@@ -193,7 +194,6 @@ Learn about best practices for:
193194

194195
## Tests
195196

196-
197197
:::::{grid} 1 1 2 2
198198
:class-container: text-center
199199
:gutter: 3
@@ -212,7 +212,8 @@ Learn about best practices for:
212212
:::{card} ✨ Run your tests ✨
213213
:class-card: left-aligned
214214

215-
* [Run tests locally](tests/run-tests)
215+
* [Run tests locally with Hatch](tests/run-tests)
216+
* [Run tests with nox](tests/run-tests-nox)
216217
* [Run tests in CI](tests/tests-ci)
217218
:::
218219
::::
@@ -245,7 +246,7 @@ contribute.
245246

246247
:::::
247248

248-
:::{image} https://www.pyopensci.org/images/people-building-blocks.jpg
249+
:::{image} <https://www.pyopensci.org/images/people-building-blocks.jpg>
249250
:align: right
250251
:width: 350
251252
:alt: xkcd comic showing a stick figure on the ground and one in the air. The one on the ground is saying. `You're flying! how?` The person in the air replies `Python!` Below is a 3 rectangle comic with the following text in each box. Box 1 - I learned it last night. Everything is so simple. Hello world is just print hello world. Box 2 - the person on the ground says - come join us programming is fun again. It's a whole new world. But how are you flying? box 3 - the person flying says - i just typed import antigravity. I also sampled everything in the medicine cabinet. But i think this is the python. The person on the ground is saying - that's it?

package-structure-code/intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This section covers everything you need to structure your Python package, configure metadata, choose build tools, and publish your package to PyPI and conda-forge.
44

5-
:::::{grid} 1 2
5+
:::::{grid} 1 1 2 2
66
:gutter: 3
77

88
::::{grid-item}

package-structure-code/python-package-structure.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Want to learn how to create the structure to build your package? Click here.
3939

4040
An example of the **src/package** layout structure is below.
4141

42-
```{console}
42+
```bash
4343
myPackageRepoName
4444
├── CHANGELOG.md ┐
4545
├── CODE_OF_CONDUCT.md │

tests/code-cov.md

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,44 @@ using code coverage effectively.
1010
A good practice is to ensure that every line of your code runs at least once
1111
during your test suite. This helps you:
1212

13-
- Identify untested parts of your codebase.
14-
- Catch bugs that might otherwise go unnoticed.
15-
- Build confidence in your software's stability.
13+
- **Identify untested parts:** Parts of your codebase that are not
14+
covered by tests.
15+
- **Catch bugs:** Bugs that might otherwise go unnoticed.
16+
- **Build confidence:** Confidence in your software's stability.
1617

1718
## Limitations of code coverage
1819

1920
While high code coverage is valuable, it has its limits:
2021

21-
- **Difficult-to-test code:** Some parts of your code might be challenging to
22-
test, either due to complexity or limited resources.
23-
- **Missed edge cases:** Running all lines of code doesnt guarantee that edge
24-
cases are handled correctly.
22+
- **Difficult-to-test code:** Some parts of your code might be
23+
challenging to test, either due to complexity or limited resources.
24+
- **Missed edge cases:** Running all lines of code doesn't guarantee
25+
that edge cases are handled correctly.
2526

2627
Ultimately, you should focus on how your package will be used and ensure your
2728
tests cover those scenarios adequately.
2829

2930
## Tools for analyzing Python package code coverage
3031

31-
Some common services for analyzing code coverage are [codecov.io](https://about.codecov.io/) and [coveralls.io](https://coveralls.io/). These projects are free for open source tools and will provide dashboards that tell you how much of your codebase is covered during your tests. We recommend setting up an account (on either CodeCov or Coveralls) and using it to keep track of your code coverage.
32+
Some common services for analyzing code coverage are
33+
[codecov.io](https://about.codecov.io/) and [coveralls.io](https://coveralls.io/).
34+
These projects are free for open source tools and provide dashboards that
35+
show how much of your codebase is covered during your tests. We recommend
36+
setting up an account (on either CodeCov or Coveralls) and using it to
37+
keep track of your code coverage.
3238

3339
:::{figure} ../images/code-cov-stravalib.png
3440
:height: 450px
3541
:alt: Screenshot of the code cov service - showing test coverage for the stravalib package. This image shows a list of package modules and the associated number of lines and % lines covered by tests. At the top of the image, you can see what branch is being evaluated and the path to the repository.
3642

37-
The CodeCov platform is a useful tool if you wish to track code coverage visually. Using it, you can not only get the same summary information that you can get with the **pytest-cov** extension. You can also see what lines are covered by your tests and which are not. Code coverage is useful for evaluating unit tests and/or how much of your package code is "covered". It, however, will not evaluate things like integration tests and end-to-end workflows.
43+
The CodeCov platform is a useful tool if you wish to track code coverage
44+
visually. Using it, you can get the same summary information that you can get
45+
with the **pytest-cov** extension. You can also see what lines are covered by
46+
your tests and which are not. Code coverage is useful for evaluating
47+
[unit tests](test-types.md#unit-tests) and/or how much of your package code
48+
is "covered". It, however, will not evaluate things like
49+
[integration tests](test-types.md#integration-tests) and
50+
[end-to-end workflows](test-types.md).
3851

3952
:::
4053

@@ -46,21 +59,37 @@ You can also create and upload typing reports to CodeCov.
4659

4760
## Exporting Local Coverage Reports
4861

49-
In addition to using services like CodeCov or Coveralls, you can generate local coverage reports directly using the **coverage.py** tool. This can be especially useful if you want to create reports in Markdown or HTML format for offline use or documentation.
62+
In addition to using services like CodeCov or Coveralls, you can generate
63+
local coverage reports directly using the **coverage.py** tool. This can be
64+
especially useful if you want to create reports in Markdown or HTML format for
65+
offline use or documentation.
5066

5167
To generate a coverage report in **Markdown** format, run:
5268

5369
```bash
54-
$ python -m coverage report --format=markdown
70+
python -m coverage report --format=markdown
5571
```
56-
This command will produce a Markdown-formatted coverage summary that you can easily include in project documentation or share with your team.
5772

58-
To generate an HTML report that provides a detailed, interactive view of which lines are covered, use:
73+
This command will produce a Markdown-formatted coverage summary that you can
74+
include in project documentation or share with your team.
75+
76+
To generate an HTML report that provides a detailed, interactive view of which
77+
lines are covered, use:
5978

6079
```bash
6180
python -m coverage html
6281
```
6382

64-
The generated HTML report will be saved in a directory named htmlcov by default. Open the index.html file in your browser to explore your coverage results.
83+
The generated HTML report will be saved in a directory named `htmlcov` by
84+
default. Open the `index.html` file in your browser to explore your coverage
85+
results.
86+
87+
These local reports are an excellent way to quickly review coverage without
88+
setting up an external service.
89+
90+
## Next steps
6591

66-
These local reports are an excellent way to quickly review coverage without setting up an external service.
92+
Writing meaningful tests is the foundation of useful coverage.
93+
See [Write tests](write-tests.md) and [Test types](test-types.md)
94+
to learn more about developing better test suites. Learn how to run your tests both
95+
[locally](run-tests.md) and in [continuous integration](tests-ci.md).

tests/index.md

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
(tests-intro)=
22
# Tests and data for your Python package
33

4-
Tests are an important part of your Python package because they
5-
provide a set of checks that ensure that your package is
4+
Adding tests to your package provides a set of checks that ensure that its
65
functioning how you expect it to.
76

8-
In this section, you will learn more about the importance of writing
9-
tests for your Python package and how you can set up infrastructure
10-
to run your tests both locally and on GitHub.
11-
7+
In this section, you will learn about the importance of writing
8+
tests for your Python package, different [types of tests that you should consider](test-types) and how you can set up infrastructure
9+
to run your tests both [locally](run-tests) and [on GitHub](tests-ci).
1210

1311
:::::{grid} 1 1 3 2
1412
:class-container: text-center
@@ -20,9 +18,8 @@ to run your tests both locally and on GitHub.
2018
:link-type: doc
2119
:class-card: left-aligned
2220

23-
Learn more about the art of writing tests for your Python package.
24-
Learn about why you should write tests and how they can help you and
25-
potential contributors to your project.
21+
Learn about the importance of writing tests for your Python
22+
package and how they help you and potential contributors.
2623
:::
2724
::::
2825

@@ -32,8 +29,8 @@ potential contributors to your project.
3229
:link-type: doc
3330
:class-card: left-aligned
3431

35-
There are three general types of tests that you can write for your Python
36-
package: unit tests, integration tests and end-to-end (or functional) tests. Learn about all three.
32+
Get to know the three test types: unit, integration, and
33+
end-to-end tests. Learn when and how to use each.
3734
:::
3835
::::
3936

@@ -43,32 +40,44 @@ package: unit tests, integration tests and end-to-end (or functional) tests. Lea
4340
:link-type: doc
4441
:class-card: left-aligned
4542

46-
If you expect your users to use your package across different versions
47-
of Python, then using an automation tool such as nox to run your tests is useful. Learn about the various tools that you can use to run your tests across python versions here.
43+
Learn about testing tools like pytest, nox, and tox to run
44+
tests across different Python versions on your computer. And explore examples of using Hatch with UV as a task runner to run tests across Python versions.
4845
:::
4946
::::
5047

5148
::::{grid-item}
52-
:::{card} ✨ Run tests online (using CI) ✨
53-
:link: tests-ci
49+
:::{card} ✨ Run tests locally (using nox) ✨
50+
:link: run-tests-nox
5451
:link-type: doc
5552
:class-card: left-aligned
5653

57-
Continuous integration platforms such as GitHub Actions can be
58-
useful for running your tests across both different Python versions
59-
and different operating systems. Learn about setting up tests to run in Continuous Integration here.
54+
Nox is a python powered task runner that can be used to run tests. Learn how to use nox to run tests.
6055
:::
6156
::::
6257

63-
:::::
64-
58+
::::{grid-item}
59+
:::{card} ✨ Run tests online (using CI) ✨
60+
:link: tests-ci
61+
:link-type: doc
62+
:class-card: left-aligned
6563

66-
:::{figure-md} fig-target
64+
Set up continuous integration with GitHub Actions to run
65+
tests across Python versions and operating systems.
66+
:::
67+
::::
6768

68-
<img src="../images/packaging-lifecycle.png" alt="" width="800px">
69+
::::{grid-item}
70+
:::{card} ✨ Code coverage ✨
71+
:link: code-cov
72+
:link-type: doc
73+
:class-card: left-aligned
6974

70-
Graphic showing the elements of the packaging process.
75+
Measure how much of your package code runs during tests.
76+
Learn to generate local reports and visualize coverage online.
7177
:::
78+
::::
79+
80+
:::::
7281

7382
```{toctree}
7483
:hidden:
@@ -79,6 +88,7 @@ Intro <self>
7988
Write tests <write-tests>
8089
Test types <test-types>
8190
Run tests locally <run-tests>
91+
Run tests with Nox <run-tests-nox>
8292
Run tests online (using CI) <tests-ci>
8393
Code coverage <code-cov>
8494
```

0 commit comments

Comments
 (0)