Skip to content

Commit a5d7314

Browse files
committed
Rework tox and coverage
1 parent 540f0f9 commit a5d7314

2 files changed

Lines changed: 13 additions & 19 deletions

File tree

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
requires = ["tox>=4"]
2-
env_list = ["testing"]
2+
env_list = ["pytest_testing", "unittest"]
33

4-
[env.testing]
4+
[env.pytest_testing]
55
description = "Run pytest"
66
deps = ["pytest>=8"]
77
commands = [["pytest"]]
8+
9+
[env.unittest]
10+
description = "Run unittest"
11+
commands = [["python", "-m", "unittest", "test_operations_unittests"]]

05_testing_and_ci/python_testing_demo.md

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Notes for Demos of Python Testing Frameworks
22

3-
Example code is in [05_testing_and_ci/examples/python_testing](https://github.com/Simulation-Software-Engineering/Lecture-Material/blob/main/05_testing_and_ci/examples/python_testing)
3+
Example code is in [05_testing_and_ci/examples/python_testing](examples/python_testing/).
44

55
## Software Code Used
66

@@ -44,6 +44,7 @@ tests/
4444
- Base class `unittest.TestCase` is used to create a test suite.
4545
- Each test is now a function of a class which is derived from the class `unittest.TestCase`.
4646
- The same tests as for `pytest` are implemented using `unittest` in the file `test_operations_unittests.py`. The tests are functions of a class named `TestOperations` which tests our mathematical operations. The class `TestOperations` is derived from `unittest.TestCase`.
47+
- unittest discovers tests based on identifiers. A [test discovery](https://docs.python.org/3/library/unittest.html#test-discovery) mechanism is followed.
4748
- unittest can be run as a Python module: `python -m unittest`.
4849
- unittest.TestCase offers functions like `assertEqual`, `assertAlmostEqual`, `assertTrue`, and more ([see unittest.TestCase documentation](https://docs.python.org/3/library/unittest.html#unittest.TestCase)) for use instead of the usual assertion statements. These statements ensure that test runner to accumulate all test results and generate a test report.
4950
- `unittest.main()` provides an option to run the tests from a command-line interface and also from a file.
@@ -54,13 +55,14 @@ tests/
5455

5556
## coverage
5657

57-
- Installing coverage using pip: `pip install coverage`.
58-
- Testing frameworks can be run via coverage. Lets take our first example and run pytest via coverage:
58+
- Install coverage using pip: `pip install coverage`.
59+
- Testing frameworks can be run via coverage. Run pytest via coverage:
5960

6061
```bash
6162
coverage run -m pytest
6263
```
6364

65+
- The `-m` flag tells coverage to run `pytest` module and measure test coverage. This flag would not exist if a Python file was directly being run.
6466
- coverage does not generate any output immediately as it would interfere with the test output.
6567
- Code coverage information is stored in a file `.coverage` in the working directory. This information can be viewed using:
6668

@@ -80,19 +82,7 @@ coverage html
8082

8183
- Environment orchestrator to setup and execute various tools for a project.
8284
- `tox` creates virtual environments to run each tools in.
83-
- `tox.toml` file:
84-
85-
```toml
86-
requires = ["tox>=4"]
87-
env_list = ["testing"]
88-
89-
[env.testing]
90-
description = "Run pytest"
91-
deps = ["pytest>=8"]
92-
commands = [["pytest"]]
93-
```
94-
85+
- `tox.toml` file consists of two environments, one to run pytest and one to run unittest.
9586
- Global settings defined under section at the top of the `tox.toml` file.
9687
- Start tox by running the command `tox` in the directory where the `tox.toml` exists.
97-
- tox takes more time the first time it is run as it creates the necessary virtual environments. Virtual environment setup can be found in the `.tox` repository.
98-
- Observe that tox starts a virtual environment, installs the dependency (here `pytest`) and runs `pytest`.
88+
- First execution of tox is slow because it creates the necessary virtual environments. Virtual environment setups are in the `.tox` repository.

0 commit comments

Comments
 (0)