Skip to content

Commit 1c78348

Browse files
committed
πŸ”§ Template: Add basic tests
Add a minimal `tests` folder with a small test that relies on a simple fixture defined in the `conftest.py`. Since a user of the template package typically doesn't want to keep these files when updating their template, we exclude the `tests` directory when the `update` operation is used. We also set two Ruff codes to ignore when linting the package for the modules in the `tests` directory: * `INP001`/`implicit-namespace-package`: We don't need `__init__.py` files in the tests. * `S101`/`assert`: Assert statements are fine in tests.
1 parent b878126 commit 1c78348

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

β€Žcopier.ymlβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ doc_deploy:
2727
_subdirectory: template
2828
_message_after_copy: |
2929
The `{{package_name}}` package has been created successfully!
30+
_exclude:
31+
- "{% if _copier_operation == 'update' -%}tests{% endif %}"

β€Žtemplate/docs/developer.md.jinjaβ€Ž

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Or run them via:
3434

3535
hatch run precommit:install
3636

37-
From the extensive [Ruff ruleset](https://docs.astral.sh/ruff/rules/) that Hatch uses, we ignore the following:
37+
From the extensive [Ruff ruleset](https://docs.astral.sh/ruff/rules/) that Hatch uses, we ignore the following globally:
3838

3939
| Code | Rule | Rationale / Note |
4040
| --------- | ------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
@@ -44,3 +44,10 @@ From the extensive [Ruff ruleset](https://docs.astral.sh/ruff/rules/) that Hatch
4444
| `PLR2004` | [magic-value-comparison](https://docs.astral.sh/ruff/rules/magic-value-comparison/) | We have a lot of β€œmagic values” to compare with in scientific code; naming them all would reduce readability for little benefit. |
4545
| `FBT002` | [boolean-default-value-positional-argument](https://docs.astral.sh/ruff/rules/boolean-default-value-positional-argument/) | We understand the concept, but adhering to this rule is not a small change in syntax; disable for now. |
4646
| `TID252` | [relative-imports](https://docs.astral.sh/ruff/rules/relative-imports/) | We don’t mind relative imports; as long as you don’t go up a level, they’re more readable (less verbose). |
47+
48+
And the following rules for the files in the `tests` directory:
49+
50+
| Code | Rule | Rationale / Note |
51+
| --------- | ------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
52+
| `INP001` | [implicit-namespace-package](https://docs.astral.sh/ruff/rules/implicit-namespace-package/) | When tests are not part of the package, there is no need for `__init__.py` files. |
53+
| `S101` | [assert](https://docs.astral.sh/ruff/rules/assert/) | Asserts should not be used in production environments, but are fine for tests. |

β€Žtemplate/pyproject.toml.jinjaβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ lint.ignore = [
5858
"FBT002", # https://docs.astral.sh/ruff/rules/boolean-default-value-positional-argument/
5959
"TID252", # https://docs.astral.sh/ruff/rules/relative-imports/
6060
]
61+
[tool.ruff.lint.per-file-ignores]
62+
"tests/**/*.py" = ["INP001", "S101"]

β€Žtemplate/tests/conftest.pyβ€Ž

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Project-wide pytest fixtures & hooks.
2+
3+
Docs: https://docs.pytest.org/en/stable/how-to/fixtures.html#conftest-py-sharing-fixtures-across-multiple-files
4+
"""
5+
import pytest
6+
7+
8+
@pytest.fixture
9+
def readability_counts() -> bool:
10+
return True
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
def test_example(readability_counts):
3+
assert readability_counts is True

0 commit comments

Comments
Β (0)