You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 05_testing_and_ci/python_testing_demo.md
+7-17Lines changed: 7 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Notes for Demos of Python Testing Frameworks
2
2
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/).
4
4
5
5
## Software Code Used
6
6
@@ -44,6 +44,7 @@ tests/
44
44
- Base class `unittest.TestCase` is used to create a test suite.
45
45
- Each test is now a function of a class which is derived from the class `unittest.TestCase`.
46
46
- 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.
47
48
- unittest can be run as a Python module: `python -m unittest`.
48
49
- 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.
49
50
-`unittest.main()` provides an option to run the tests from a command-line interface and also from a file.
@@ -54,13 +55,14 @@ tests/
54
55
55
56
## coverage
56
57
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:
59
60
60
61
```bash
61
62
coverage run -m pytest
62
63
```
63
64
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.
64
66
- coverage does not generate any output immediately as it would interfere with the test output.
65
67
- Code coverage information is stored in a file `.coverage` in the working directory. This information can be viewed using:
66
68
@@ -80,19 +82,7 @@ coverage html
80
82
81
83
- Environment orchestrator to setup and execute various tools for a project.
82
84
-`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.
95
86
- Global settings defined under section at the top of the `tox.toml` file.
96
87
- 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