Skip to content

Commit 025e931

Browse files
authored
Add option for tests (#447)
* wip add tests * stub tests * Simple failing tests and make test command * Do not run tests inside template
1 parent e6119d3 commit 025e931

File tree

7 files changed

+88
-5
lines changed

7 files changed

+88
-5
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ clean-test: ## remove test and coverage artifacts
5454
rm -f .coverage
5555
rm -fr htmlcov/
5656
rm -fr .pytest_cache
57-
57+
5858
dist: clean ## builds source and wheel package
5959
python -m build
6060
ls -l dist
@@ -71,7 +71,7 @@ docs-serve:
7171
### TESTS
7272

7373
test: _prep
74-
pytest -vvv --durations=0
74+
pytest -vvv --durations=0 tests
7575

7676
test-fastest: _prep
7777
pytest -vvv -FFF

ccds-help.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,36 @@
216216
}
217217
]
218218
},
219+
{
220+
"field": "testing_framework",
221+
"help": {
222+
"description": "Framework used for testing your code.",
223+
"more_information": ""
224+
},
225+
"choices": [
226+
{
227+
"choice": "none",
228+
"help": {
229+
"description": "No testing framework.",
230+
"more_information": ""
231+
}
232+
},
233+
{
234+
"choice": "pytest",
235+
"help": {
236+
"description": "Use the pytest framework for testing.",
237+
"more_information": "[Docs](https://docs.pytest.org/en/latest/)"
238+
}
239+
},
240+
{
241+
"choice": "unittest",
242+
"help": {
243+
"description": "Use Python's built-in testing framework.",
244+
"more_information": "[Docs](https://docs.python.org/3/library/unittest.html)"
245+
}
246+
}
247+
]
248+
},
219249
{
220250
"field": "linting_and_formatting",
221251
"help": {

ccds.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@
2828
"none",
2929
"basic"
3030
],
31+
"testing_framework": [
32+
"none",
33+
"pytest",
34+
"unittest"
35+
],
3136
"linting_and_formatting": [
3237
"ruff",
3338
"flake8+black+isort"
3439
],
3540
"open_source_license": ["No license file", "MIT", "BSD-3-Clause"],
3641
"docs": ["mkdocs", "none"],
3742
"include_code_scaffold": ["Yes", "No"]
38-
}
43+
}

hooks/post_gen_project.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@
4545
"python-dotenv",
4646
]
4747

48+
# Select testing framework
49+
tests_path = Path("tests")
50+
51+
# {% if cookiecutter.testing_framework == "pytest" %}
52+
packages_to_install += ["pytest"]
53+
# {% endif %}
54+
55+
# {% if cookiecutter.testing_framework == "none" %}
56+
shutil.rmtree(tests_path)
57+
58+
# {% else %}
59+
tests_subpath = tests_path / "{{ cookiecutter.testing_framework }}"
60+
for obj in tests_subpath.iterdir():
61+
shutil.move(str(obj), str(tests_path))
62+
63+
# Remove all remaining tests templates
64+
for tests_template in tests_path.iterdir():
65+
if tests_template.is_dir() and not tests_template.name == "tests":
66+
shutil.rmtree(tests_template)
67+
# {% endif %}
68+
4869
# Use the selected documentation package specified in the config,
4970
# or none if none selected
5071
docs_path = Path("docs")

{{ cookiecutter.repo_name }}/Makefile

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ requirements:
3131
conda env update --name $(PROJECT_NAME) --file environment.yml --prune
3232
{% elif "Pipfile" == cookiecutter.dependency_file -%}
3333
pipenv install
34-
{% endif %}
34+
{% endif %}
3535
{% endif %}
3636

3737

@@ -68,6 +68,17 @@ format:
6868
black {{ cookiecutter.module_name }}
6969
{% endif %}
7070

71+
{% if cookiecutter.testing_framework != 'none' %}
72+
## Run tests
73+
.PHONY: test
74+
test:
75+
{%- if cookiecutter.testing_framework == 'unittest' %}
76+
python -m unittest discover -s tests
77+
{%- elif cookiecutter.testing_framework == 'pytest' %}
78+
python -m pytest tests
79+
{%- endif -%}
80+
{%- endif -%}
81+
7182
{% if not cookiecutter.dataset_storage.none %}
7283
## Download Data from storage system
7384
.PHONY: sync_data_down
@@ -118,7 +129,7 @@ create_environment:
118129
@echo ">>> New uv virtual environment created. Activate with:"
119130
@echo ">>> Windows: .\\\\.venv\\\\Scripts\\\\activate"
120131
@echo ">>> Unix/macOS: source ./.venv/bin/activate"
121-
{% endif %}
132+
{% endif %}
122133
{% endif %}
123134

124135

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import pytest
2+
3+
4+
def test_code_is_tested():
5+
assert False
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import unittest
2+
3+
4+
class TestCodeIsTested(unittest.TestCase):
5+
6+
def test_code_is_tested(self):
7+
self.assertTrue(False)
8+
9+
10+
if __name__ == '__main__':
11+
unittest.main()

0 commit comments

Comments
 (0)