Skip to content

Add option for tests #447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ clean-test: ## remove test and coverage artifacts
rm -f .coverage
rm -fr htmlcov/
rm -fr .pytest_cache

dist: clean ## builds source and wheel package
python -m build
ls -l dist
Expand All @@ -71,7 +71,7 @@ docs-serve:
### TESTS

test: _prep
pytest -vvv --durations=0
pytest -vvv --durations=0 tests

test-fastest: _prep
pytest -vvv -FFF
Expand Down
30 changes: 30 additions & 0 deletions ccds-help.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,36 @@
}
]
},
{
"field": "testing_framework",
"help": {
"description": "Framework used for testing your code.",
"more_information": ""
},
"choices": [
{
"choice": "none",
"help": {
"description": "No testing framework.",
"more_information": ""
}
},
{
"choice": "pytest",
"help": {
"description": "Use the pytest framework for testing.",
"more_information": "[Docs](https://docs.pytest.org/en/latest/)"
}
},
{
"choice": "unittest",
"help": {
"description": "Use Python's built-in testing framework.",
"more_information": "[Docs](https://docs.python.org/3/library/unittest.html)"
}
}
]
},
{
"field": "linting_and_formatting",
"help": {
Expand Down
7 changes: 6 additions & 1 deletion ccds.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@
"none",
"basic"
],
"testing_framework": [
"none",
"pytest",
"unittest"
],
"linting_and_formatting": [
"ruff",
"flake8+black+isort"
],
"open_source_license": ["No license file", "MIT", "BSD-3-Clause"],
"docs": ["mkdocs", "none"],
"include_code_scaffold": ["Yes", "No"]
}
}
21 changes: 21 additions & 0 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@
"python-dotenv",
]

# Select testing framework
tests_path = Path("tests")

# {% if cookiecutter.testing_framework == "pytest" %}
packages_to_install += ["pytest"]
# {% endif %}

# {% if cookiecutter.testing_framework == "none" %}
shutil.rmtree(tests_path)

# {% else %}
tests_subpath = tests_path / "{{ cookiecutter.testing_framework }}"
for obj in tests_subpath.iterdir():
shutil.move(str(obj), str(tests_path))

# Remove all remaining tests templates
for tests_template in tests_path.iterdir():
if tests_template.is_dir() and not tests_template.name == "tests":
shutil.rmtree(tests_template)
# {% endif %}

# Use the selected documentation package specified in the config,
# or none if none selected
docs_path = Path("docs")
Expand Down
15 changes: 13 additions & 2 deletions {{ cookiecutter.repo_name }}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ requirements:
conda env update --name $(PROJECT_NAME) --file environment.yml --prune
{% elif "Pipfile" == cookiecutter.dependency_file -%}
pipenv install
{% endif %}
{% endif %}
{% endif %}


Expand Down Expand Up @@ -68,6 +68,17 @@ format:
black {{ cookiecutter.module_name }}
{% endif %}

{% if cookiecutter.testing_framework != 'none' %}
## Run tests
.PHONY: test
test:
{%- if cookiecutter.testing_framework == 'unittest' %}
python -m unittest discover -s tests
{%- elif cookiecutter.testing_framework == 'pytest' %}
python -m pytest tests
{%- endif -%}
{%- endif -%}

{% if not cookiecutter.dataset_storage.none %}
## Download Data from storage system
.PHONY: sync_data_down
Expand Down Expand Up @@ -118,7 +129,7 @@ create_environment:
@echo ">>> New uv virtual environment created. Activate with:"
@echo ">>> Windows: .\\\\.venv\\\\Scripts\\\\activate"
@echo ">>> Unix/macOS: source ./.venv/bin/activate"
{% endif %}
{% endif %}
{% endif %}


Expand Down
5 changes: 5 additions & 0 deletions {{ cookiecutter.repo_name }}/tests/pytest/test_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pytest


def test_code_is_tested():
assert False
11 changes: 11 additions & 0 deletions {{ cookiecutter.repo_name }}/tests/unittest/test_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import unittest


class TestCodeIsTested(unittest.TestCase):

def test_code_is_tested(self):
self.assertTrue(False)


if __name__ == '__main__':
unittest.main()
Loading