Skip to content

Commit ea78a0c

Browse files
authored
Merge pull request #7 from ferreteleco/feature/python-ci-tests
Adds python CI Unit Testing action template
2 parents d4804aa + 044072c commit ea78a0c

File tree

7 files changed

+139
-13
lines changed

7 files changed

+139
-13
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
2424

2525
-
2626

27+
## [**1.1.0**] - 2023-03-13
28+
29+
### Added
30+
31+
- Cookiecutter action for CI Unit testing in python (Poetry, pytest based).
32+
33+
### Changed
34+
35+
- Improved post gen. hooks, now tells to look for the checklist.
36+
- Improved existing documentation to reflect changes.
37+
38+
### Fixed
39+
40+
- Minor errors in C++ CI UT template.
41+
2742
## [**1.0.0**] - 2023-03-12
2843

2944
### Added

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This repository can be used for adding Github Actions and templates to an existi
44

55
## Version
66

7-
Current version is 1.0.0 and was set according to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
Current version is 1.1.0 and was set according to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

99
Project's version should be updated, when applicable:
1010

@@ -33,7 +33,7 @@ $ cookiecutter cookie-github
3333
```
3434

3535
NOTE: alternatively, you can use [Cookieninja](https://github.com/cookieninja-generator/cookieninja),
36-
forked and more updated version of Cookiecutter with backward compatibility.
36+
forked and more updated version of Cookiecutter with backward compatibility.
3737

3838
## Variables
3939

@@ -45,9 +45,11 @@ you will be prompted to fill in the following values:
4545
- **add_PR_template:** this flags controls wether or not to add a PR template in the generated
4646
folder.
4747
- **add_ci_action_unit_tests_runner:** this variable controls whether or not to create an action to
48-
run unit tests in the repository. The available options are:
48+
run unit tests in the repository. The available choices are:
4949
- none (no action created).
5050
- cpp, which creates an action for Catch2 based unit tests, built using CMake.
51+
- python, which creates an action for Python code unit tests, executed with pytest over a
52+
project built with Poetry.
5153

5254
## Contributing
5355

cookiecutter.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
true,
88
false
99
],
10-
"add_ci_action_unit_tests_runner": ["none", "cpp"],
10+
"add_ci_action_unit_tests_runner": [
11+
"none",
12+
"cpp",
13+
"python"
14+
],
1115
"__github_folder": ".github"
1216
}

hooks/post_gen_project.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def add_ci_action_unit_tests_runner():
3939
ci_test_workflow_files_path = Path("_", "workflows", target_ci_tests_action)
4040
copytree(ci_test_workflow_files_path, destination)
4141

42+
print("\n\n#################################################################")
43+
print("# Please check gen. checklist in order to adjust CI UT basic action #")
44+
print("#################################################################\n\n")
45+
4246
else:
4347
LOG.info("Skipping CI action for running unit tests file generation (%s selected) ...", target_ci_tests_action)
4448

{{ cookiecutter.__github_folder }}/_/workflows/cpp/ci-tests-cpp.yml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ env:
2020
##############################################################################
2121

2222
jobs:
23-
run_experiment:
23+
run_unit_tests:
2424
############################################################################
2525
# Please fill in with the Runner(s) in which to execute this action
26-
############################################################################
27-
runs-on: [ubuntu-22.04]
26+
# Ref. https://docs.github.com/en/actions/hosting-your-own-runners/using-self-hosted-runners-in-a-workflow
27+
# Ref. https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners ############################################################################
28+
runs-on: ubuntu-22.04
2829
############################################################################
2930

3031
container:
31-
image: $TARGET_DOCKER_IMAGE
32+
image: {{ '${{ env.TARGET_DOCKER_IMAGE }}' }}
3233

3334
##########################################################################
3435
# Please uncomment and fill the following if the image comes from a private repository
@@ -39,24 +40,34 @@ jobs:
3940
# password: {{ '${{ secrets.RepositoryPass }}' }}
4041
##########################################################################
4142

43+
############################################################################
44+
# Minimum permissions required by EnricoMi/publish-unit-test-result-action@v2
45+
# Ref. https://github.com/EnricoMi/publish-unit-test-result-action#support-fork-repositories-and-dependabot-branches
46+
############################################################################
47+
permissions:
48+
contents: read
49+
issues: read
50+
checks: write
51+
pull-requests: write
52+
4253
steps:
43-
- uses: actions/checkout@v2
54+
- uses: actions/checkout@v3
4455

4556
- name: Build tests
4657
working-directory: .
4758
run: |
4859
cmake -S . -B build
49-
cmake --build build -t $TEST_TARGET_NAME
60+
cmake --build build -t {{ '${{ env.TEST_TARGET_NAME }}' }}
5061
5162
- name: Run tests
5263
working-directory: .
5364
run: |
54-
./build/test/$TEST_TARGET_NAME --reporter junit -o ci-result-junit.xml
65+
./build/test/{{ '${{ env.TEST_TARGET_NAME }}' }} --reporter junit -o junit/ci-result-junit.xml
5566
5667
- name: Publish Unit Test Results
57-
uses: EnricoMi/publish-unit-test-result-action@v1
68+
uses: EnricoMi/publish-unit-test-result-action@v2
5869
if: success()
5970
with:
60-
files: ci-result-junit.xml
71+
files: junit/ci-result-junit.xml
6172
action_fail: true
6273
action_fail_on_inconclusive: true
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: ci-tests-python
2+
on:
3+
push:
4+
pull_request:
5+
branches:
6+
- master
7+
8+
env:
9+
##############################################################################
10+
# Please fill in:
11+
# (Ref. https://docs.github.com/en/github-ae@latest/actions/learn-github-actions/variables)
12+
##############################################################################
13+
# Target python version intended to use to run tests into. Defaults to 3.10.2
14+
##############################################################################
15+
TARGET_PYTHON_VERSION: 3.10.2
16+
##############################################################################
17+
# Target folder where tests reside. Defaults to "tests"
18+
##############################################################################
19+
TESTS_DIRECTORY: tests
20+
##############################################################################
21+
22+
jobs:
23+
run_unit_tests:
24+
############################################################################
25+
# Please fill in with the Runner(s) in which to execute this action
26+
# Ref. https://docs.github.com/en/actions/hosting-your-own-runners/using-self-hosted-runners-in-a-workflow
27+
# Ref. https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners
28+
############################################################################
29+
runs-on: ubuntu-20.04
30+
############################################################################
31+
32+
############################################################################
33+
# Minimum permissions required by EnricoMi/publish-unit-test-result-action@v2
34+
# Ref. https://github.com/EnricoMi/publish-unit-test-result-action#support-fork-repositories-and-dependabot-branches
35+
############################################################################
36+
permissions:
37+
contents: read
38+
issues: read
39+
checks: write
40+
pull-requests: write
41+
42+
steps:
43+
- uses: actions/checkout@v3
44+
45+
- name: Setup Python
46+
uses: actions/setup-python@v4
47+
with:
48+
python-version: {{ '${{ env.TARGET_PYTHON_VERSION }}' }}
49+
50+
- name: Install poetry
51+
run: |
52+
python -m pip install pipx && python -m pipx install poetry
53+
54+
- name: Configure poetry
55+
run: |
56+
poetry config virtualenvs.in-project true
57+
58+
- name: Cache the virtualenv
59+
uses: actions/cache@v3
60+
with:
61+
path: ./.venv
62+
key: {{ '${{ runner.os }}' }}-venv-{{ '${{ hashFiles(\'**/poetry.lock\') }}' }}
63+
64+
- name: Install dependencies
65+
run: |
66+
poetry install
67+
68+
- name: Run tests
69+
run: |
70+
poetry run python -m pytest {{ '${{ env.TESTS_DIRECTORY }}' }} --doctest-modules --junit-xml=junit/ci-result-junit.xml
71+
72+
- name: Publish Unit Test Results
73+
uses: EnricoMi/publish-unit-test-result-action@v2
74+
if: success()
75+
with:
76+
files: junit/ci-result-junit.xml
77+
action_fail: true
78+
action_fail_on_inconclusive: true
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Unit Testing GH Action configuration checklist (python)
2+
3+
Be sure to properly fine-tune the generated action (ci-tests-python.yml) before committing
4+
it to the remote repository.
5+
6+
## Checklist
7+
8+
- [ ] The intended branch and triggers are correct (default: *on push / PR to master branch*).
9+
- [ ] The target python version has been configured (defaults to *3.10.2*).
10+
- [ ] The target test directory where tests live has been defined (defaults to *tests*).
11+
- [ ] The target runner(s) in which to run the CI Unit Testing workflow have been configured
12+
(defaults to *ubuntu-22.04*, hosted by GH).

0 commit comments

Comments
 (0)