Skip to content

Commit 8f44ec8

Browse files
committed
Repalce pytest-cov with coverage and cov-defaults
1 parent 9efd49a commit 8f44ec8

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This repository aims to provide a starting template for Python projects containi
1111

1212
I tried to incorporate most "best practices" but in the end, most of the design choices and tools are just personal preferences.
1313

14+
The following tools are used: black, codecov, pre-commit, pylint, pytest, pytest-cov, tox
1415
<br>
1516

1617
## Source code
@@ -42,8 +43,8 @@ _When to use pytest, coverage and tox?_
4243

4344
Personally, I mostly use just pytest without coverage to test in my working environment with `pytest -svv test` or a specific
4445
test module. Before committing, however, it is a good idea to check if your code also runs in different environments, which is where
45-
tox comes in. Running just `tox`, will test in all environments specified in [tox.ini](tox.ini)'s envlist and may take some
46-
time. Certain environments can be selected with `tox -e py37`. Note that tox must be able to find a Python interpreter for
46+
`tox` comes in. Running just `tox`, will test in all environments specified in [tox.ini](tox.ini)'s envlist and may take some
47+
time. Certain environments can be selected with `tox -e py37`. Note that `tox` must be able to find a Python interpreter for
4748
each version given in the envlist.
4849

4950
<details><summary>How to provide the Python interpreters for tox.</summary>
@@ -73,7 +74,12 @@ Finally, some handy features of pytest you should be aware of:
7374

7475
Packaging is done with [`setuptools`](https://setuptools.pypa.io/en/latest/index.html), which is configured through the `pyproject.toml` and/or `setup.cfg`/`setup.py` files.
7576

76-
<details><summary>`pyproject.toml` vs. `setup.cfg` vs `setup.py`</summary>
77+
<details>
78+
<summary>
79+
<code>pyproject.toml</code> vs.
80+
<code>setup.cfg</code> vs
81+
<code>setup.py</code>
82+
</summary>
7783

7884
The `setup.py` file is a Python script and configuration is passed through keyword arguments of `setuptools.setup()`. This is not recommended due to possible security and parsing issues. The same setup can be accomplished in a declarative style within `setup.cfg` and `setup.py` remains mostly empty only calling `setuptools.setup()`.
7985
The `pyproject.toml` file aims to unify configuration files including various tools like black or pytest. For packaging, it is very similar to `setup.cfg`. However, `pyproject.toml` has not been adopted as the default yet and many projects still use `setup.cfg` to declare the packaging setup. Note that `setup.py` is not necessary if a `pyproject.toml` is present.
@@ -87,7 +93,7 @@ The `pyproject.toml` file aims to unify configuration files including various to
8793

8894
[](https://setuptools.pypa.io/en/latest/userguide/declarative_config.html#using-a-src-layout)
8995

90-
#### [`setup.cfg`](https://setuptools.pypa.io/en/latest/userguide/declarative_config.html)
96+
#### `setup.cfg`
9197

9298
- declarative configuration for setuptools
9399
- [_metadata_](https://setuptools.pypa.io/en/latest/userguide/declarative_config.html#metadata): must at least contain _name_ and _version_
@@ -97,6 +103,7 @@ The `pyproject.toml` file aims to unify configuration files including various to
97103
- [_options.package_data_](https://setuptools.pypa.io/en/latest/userguide/datafiles.html#package-data): inclusion of other, non-Python files (marker files, data, ...)
98104
- alternative: `MANIFEST.in`
99105
- [_options.entry_points_](https://setuptools.pypa.io/en/latest/userguide/entry_point.html): entry point for command line interface
106+
- can also hold configuration of other tools
100107

101108
<br>
102109

@@ -106,7 +113,7 @@ The package can be installed with `pip install .` or something like `pip install
106113

107114
## CI - Continuous Integration (with GitHub Actions)
108115

109-
**Important!** CI in private repositories is generally limited (to x minutes of execution time).
116+
**Important!** CI in private repositories is generally limited (to _x_ minutes of execution time).
110117

111118
...
112119

@@ -115,3 +122,4 @@ The package can be installed with `pip install .` or something like `pip install
115122
## Additional tools
116123

117124
[pre-commit.ci](https://github.com/apps/pre-commit-ci/)
125+
[codecov](https://github.com/apps/codecov/)

pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@ disallow_untyped_defs = true
1717
warn_redundant_casts = true
1818
warn_unreachable = true
1919
warn_unused_ignores = true
20+
21+
22+
[tool.coverage.run]
23+
plugins = ["covdefaults"]
24+
source = ["./src"]
25+
26+
[tool.coverage.report]
27+
fail_under = 50

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ console_scripts =
3737
dev =
3838
black
3939
codecov
40+
covdefaults
41+
coverage
4042
pre-commit
4143
pylint
4244
pytest
43-
pytest-cov
4445
tox
4546

4647
[options.package_data]

test/test_math/test_square.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111

1212

1313
@pytest.mark.parametrize("value", [1.0, 2, 3.0])
14-
def test_add(value: int | float) -> None:
14+
def test_squarer(value: int | float) -> None:
1515
assert pytest.approx(square_a_number(value)) == value * value
1616

1717

18+
def test_squarer_fail() -> None:
19+
with pytest.raises(TypeError):
20+
square_a_number("2")
21+
22+
1823
def test_dummy() -> None:
1924
arr = np.array([1.0]) / 3.0
2025
print(arr)

tox.ini

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ envlist = py{39,310,311}
33

44
[testenv]
55
deps =
6-
pytest
7-
pytest-cov
6+
covdefaults
87
coverage
8+
pytest
99
commands =
10-
pytest -svv --cov=./src --cov-report=term-missing {posargs:test}
10+
coverage erase
11+
coverage run -m pytest -svv {posargs:test}
12+
coverage report

0 commit comments

Comments
 (0)