Skip to content

Commit d6eb229

Browse files
author
Florian Maas
authored
Added pytest-cov and codecov support (#50)
* moved composite actions from workflows to actions * Added pytest-cov and codecov support * Modified docs accordingly
1 parent 355d8ac commit d6eb229

File tree

13 files changed

+78
-2
lines changed

13 files changed

+78
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ repository to generate the file structure for a Python project that uses
2424
- Linting with [flake8](https://flake8.pycqa.org/en/latest/)
2525
- Publishing to [Pypi](https://pypi.org) or [Artifactory](https://jfrog.com/artifactory) by creating a new release on GitHub
2626
- Testing with [pytest](https://docs.pytest.org/en/7.1.x/)
27+
- Test coverage with [codecov](https://about.codecov.io/)
2728
- Documentation with [MkDocs](https://www.mkdocs.org/)
2829
- Static type checking with [mypy](https://mypy.readthedocs.io/en/stable/)
2930
- Compatibility testing for multiple versions of Python with [Tox](https://tox.wiki/en/latest/)

codecov.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Badge color changes from red to green between 70% and 100%
2+
# PR pipeline fails if codecov falls with 1%
3+
4+
coverage:
5+
range: 70..100
6+
round: down
7+
precision: 1
8+
status:
9+
project:
10+
default:
11+
target: auto
12+
threshold: 1%
13+
14+
# Ignoring Paths
15+
# --------------
16+
# which folders/files to ignore
17+
ignore:
18+
- "foo/bar.py"

cookiecutter.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"mkdocs": ["y", "n"],
1111
"tox": ["y","n"],
1212
"mypy" : ["y","n"],
13+
"codecov" : ["y","n"],
1314
"dockerfile" : ["y","n"],
1415
"open_source_license": ["MIT license", "BSD license", "ISC license", "Apache Software License 2.0", "GNU General Public License v3", "Not open source"]
1516
}

docs/features/codecov.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Test coverage with codecov
2+
3+
If `codecov` is set to `"y"`, `pytest-cov` is added as a development dependency,
4+
and `make test` will run the tests and output a coverage report as `coverage.xml`.
5+
If `include_github_actions` is set to `"y"`, coverage tests with [codecov](https://about.codecov.io/) are added to the CI/CD pipeline.
6+
Additionally, a `codecov.yaml` file is created, with the following defaults:
7+
8+
``` yaml
9+
# Badge color changes from red to green between 70% and 100%
10+
# PR pipeline fails if codecov falls with 1%
11+
12+
coverage:
13+
range: 70..100
14+
round: down
15+
precision: 1
16+
status:
17+
project:
18+
default:
19+
target: auto
20+
threshold: 1%
21+
22+
# Ignoring Paths
23+
# --------------
24+
# which folders/files to ignore
25+
ignore:
26+
- "foo/bar.py"
27+
```

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ A project generated with ``cookiecutter-poetry`` supports the following features
1818
- Linting with [flake8](https://flake8.pycqa.org/en/latest/)
1919
- Publishing to [Pypi](https://pypi.org) or [Artifactory](https://jfrog.com/artifactory) by creating a new release on GitHub
2020
- Testing with [pytest](https://docs.pytest.org/en/7.1.x/)
21+
- Test coverage with [codecov](https://about.codecov.io/)
2122
- Documentation with [MkDocs](https://www.mkdocs.org/)
2223
- Static type checking with [mypy](https://mypy.readthedocs.io/en/stable/)
2324
- Compatibility testing for multiple versions of Python with [Tox](https://tox.wiki/en/latest/)

docs/prompt_arguments.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ compatibility with multiple versions of Python.
6565

6666
`"y"` or `"n"`. Adds automatic static type checking with [mypy](https://mypy.readthedocs.io/en/stable/).
6767

68+
**mypy**
69+
70+
`"y"` or `"n"`. Adds test coverage with [codecov](https://about.codecov.io/).
71+
72+
6873
**dockerfile**
6974

7075
`"y"` or `"n"`. Adds a simple [Dockerfile](https://docker.com).

hooks/post_gen_project.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ def remove_dir(filepath: str) -> None:
2323

2424
if "{{cookiecutter.dockerfile}}" != "y":
2525
remove_file("Dockerfile")
26+
27+
if "{{cookiecutter.codecov}}" != "y":
28+
remove_file("codecov.yaml")

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ nav:
1414
- Dependency management with Poetry: features/poetry.md
1515
- Publishing to PyPi or Artifactory: features/publishing.md
1616
- Testing with Pytest: features/pytest.md
17+
- Test coverage with codecov: features/codecov.md
1718
- Documentation with MkDocs: features/mkdocs.md
1819
- Compatibility testing with Tox: features/tox.md
1920
- Static type checking with mypy: features/mypy.md

tests/test_cookiecutter.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_using_pytest(cookies, tmp_path):
4141

4242
# Install the poetry environment and run the tests.
4343
with run_within_dir(str(result.project_path)):
44-
subprocess.check_call(shlex.split("poetry install --no-interaction --no-root")) == 0
44+
subprocess.check_call(shlex.split("poetry install --no-interaction")) == 0
4545
subprocess.check_call(shlex.split("poetry run make test")) == 0
4646

4747

@@ -121,3 +121,9 @@ def test_not_dockerfile(cookies, tmp_path):
121121
with run_within_dir(tmp_path):
122122
result = cookies.bake(extra_context={"dockerfile": "n"})
123123
assert not os.path.isfile(f"{result.project_path}/Dockerfile")
124+
125+
126+
def test_not_codecov(cookies, tmp_path):
127+
with run_within_dir(tmp_path):
128+
result = cookies.bake(extra_context={"codecov": "n"})
129+
assert not os.path.isfile(f"{result.project_path}/codecov.yaml")

{{cookiecutter.project_name}}/.github/actions/run-checks/action.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ runs:
1717
source .venv/bin/activate
1818
make test
1919
shell: bash
20-
20+
21+
{%- if cookiecutter.codecov == "y" %}
22+
23+
- name: Upload coverage reports to Codecov with GitHub Action
24+
uses: codecov/codecov-action@v3
25+
{%- endif %}

0 commit comments

Comments
 (0)