Skip to content

Commit 618694e

Browse files
authored
Make pytest treat warnings as errors (#335)
Warnings are usually a sign of something that can be improved or even wrong, like tasks not being properly awaited or cancelled, so it's a good idea to treat them as errors to avoid them getting unnoticed. This PR also moves `pytest` options to the `pyproject.toml` files, so they are easier to change and more flexible.
2 parents 37d223f + 47b0143 commit 618694e

File tree

9 files changed

+48
-7
lines changed

9 files changed

+48
-7
lines changed

RELEASE_NOTES.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,26 @@
66

77
## Upgrading
88

9-
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
9+
- The `nox` default `pytest` session doesn't pass `-W=all -vv` to `pytest` anymore. You can use the `pyproject.toml` file to configure default options for `pytest`, for example:
10+
11+
```toml
12+
[tool.pytest.ini_options]
13+
addopts = "-W=all -Werror -Wdefault::DeprecationWarning -Wdefault::PendingDeprecationWarning -vv"
14+
```
1015

1116
### Cookiecutter template
1217

13-
<!-- Here upgrade steps for cookiecutter specifically -->
18+
All upgrading should be done via the migration script or regenerating the templates. But you might still need to adapt your code:
19+
20+
- `pytest` now uses `-Werror` by default (but still treat deprecations as normal warnings), so if your tests run with warnings, they will now be turned to errors, and you'll need to fix them.
1421

1522
## New Features
1623

1724
<!-- Here goes the main new features and examples or instructions on how to use them -->
1825

1926
### Cookiecutter template
2027

21-
<!-- Here new features for cookiecutter specifically -->
28+
- `pytest` now uses `-Werror -Wdefault::DeprecationWarning -Wdefault::PendingDeprecationWarning` by default. Deprecations are still treated as warnings, as when testing with the `pytest_min` session is normal to get deprecation warnings as we are using old versions of dependencies.
2229

2330
## Bug Fixes
2431

cookiecutter/migrate.py

+31
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,41 @@
2929

3030
def main() -> None:
3131
"""Run the migration steps."""
32+
add_default_pytest_options()
33+
3234
# Add a separation line like this one after each migration step.
3335
print("=" * 72)
3436

3537

38+
def add_default_pytest_options() -> None:
39+
"""Add default pytest options to pyproject.toml."""
40+
pyproject_toml = Path("pyproject.toml")
41+
pyproject_toml_content = pyproject_toml.read_text(encoding="utf-8")
42+
marker = "[tool.pytest.ini_options]\n"
43+
new_options = (
44+
"-W=all Werror -Wdefault::DeprecationWarning "
45+
"-Wdefault::PendingDeprecationWarning -vv"
46+
)
47+
48+
print(f"Adding default pytest options to {pyproject_toml}...")
49+
if pyproject_toml_content.find(marker) == -1:
50+
print(
51+
"Couldn't find the the {marker.strip()} marker in pyproject.toml, skipping update."
52+
)
53+
return
54+
55+
if pyproject_toml_content.find("\naddopts") >= 0:
56+
print("It looks like some options are already configured, skipping update.")
57+
manual_step(f"Please consider `{new_options}` if they are not there yet.")
58+
return
59+
60+
replace_file_contents_atomically(
61+
pyproject_toml,
62+
marker,
63+
marker + f'addopts = "{new_options}"\n',
64+
)
65+
66+
3667
def apply_patch(patch_content: str) -> None:
3768
"""Apply a patch using the patch utility."""
3869
subprocess.run(["patch", "-p1"], input=patch_content.encode(), check=True)

cookiecutter/{{cookiecutter.github_repo_name}}/pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ disable = [
203203

204204
[tool.pytest.ini_options]
205205
{%- if cookiecutter.type != "api" %}
206+
addopts = "-W=all -Werror -Wdefault::DeprecationWarning -Wdefault::PendingDeprecationWarning -vv"
206207
testpaths = ["tests", "src"]
207208
asyncio_mode = "auto"
208209
asyncio_default_fixture_loop_scope = "function"

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ module = [
205205
ignore_missing_imports = true
206206

207207
[tool.pytest.ini_options]
208+
addopts = "-W=all -Werror -Wdefault::DeprecationWarning -Wdefault::PendingDeprecationWarning -vv"
208209
testpaths = ["src", "tests"]
209210
markers = [
210211
"integration: integration tests (deselect with '-m \"not integration\"')",

src/frequenz/repo/config/nox/default.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@
3636
"--check",
3737
],
3838
mypy=[],
39-
pytest=[
40-
"-W=all",
41-
"-vv",
42-
],
39+
pytest=[],
4340
)
4441
"""Default command-line options for all types of repositories."""
4542

tests_golden/integration/test_cookiecutter_generation/actor/frequenz-actor-test/pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ disable = [
153153
]
154154

155155
[tool.pytest.ini_options]
156+
addopts = "-W=all -Werror -Wdefault::DeprecationWarning -Wdefault::PendingDeprecationWarning -vv"
156157
testpaths = ["tests", "src"]
157158
asyncio_mode = "auto"
158159
asyncio_default_fixture_loop_scope = "function"

tests_golden/integration/test_cookiecutter_generation/app/frequenz-app-test/pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ disable = [
152152
]
153153

154154
[tool.pytest.ini_options]
155+
addopts = "-W=all -Werror -Wdefault::DeprecationWarning -Wdefault::PendingDeprecationWarning -vv"
155156
testpaths = ["tests", "src"]
156157
asyncio_mode = "auto"
157158
asyncio_default_fixture_loop_scope = "function"

tests_golden/integration/test_cookiecutter_generation/lib/frequenz-test-python/pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ disable = [
149149
]
150150

151151
[tool.pytest.ini_options]
152+
addopts = "-W=all -Werror -Wdefault::DeprecationWarning -Wdefault::PendingDeprecationWarning -vv"
152153
testpaths = ["tests", "src"]
153154
asyncio_mode = "auto"
154155
asyncio_default_fixture_loop_scope = "function"

tests_golden/integration/test_cookiecutter_generation/model/frequenz-model-test/pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ disable = [
153153
]
154154

155155
[tool.pytest.ini_options]
156+
addopts = "-W=all -Werror -Wdefault::DeprecationWarning -Wdefault::PendingDeprecationWarning -vv"
156157
testpaths = ["tests", "src"]
157158
asyncio_mode = "auto"
158159
asyncio_default_fixture_loop_scope = "function"

0 commit comments

Comments
 (0)