Skip to content

Commit e6323dd

Browse files
committed
Reworking Python testing demo content based on a trial
1 parent 971254c commit e6323dd

File tree

5 files changed

+20
-18
lines changed

5 files changed

+20
-18
lines changed

05_testing_and_ci/examples/python_testing/operations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ def main():
5454
data = [5, 3, 14, 27, 4, 9]
5555

5656
maximum = find_max(data)
57-
print("Maximum = {}".format(maximum))
57+
print("Maximum of {} is {}".format(data, maximum))
5858

5959
mean = find_mean(data)
60-
print("Average = {}".format(mean))
60+
print("Average of {} is {}".format(data, mean))
6161

6262

6363
if __name__ == "__main__":

05_testing_and_ci/examples/python_testing/test_operations_unittests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Tests for mathematical operations functions.
33
"""
44
from operations import find_max, find_mean
5+
import unittest
56
from unittest import TestCase
67
import csv
78

@@ -78,3 +79,7 @@ def test_regression_mean(self):
7879

7980
# Test
8081
self.assertAlmostEqual(actual_mean, reference_mean[0], 2)
82+
83+
if __name__ == "__main__":
84+
# Run the tests
85+
unittest.main()

05_testing_and_ci/examples/python_testing/tox.ini

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
requires = ["tox>=4"]
2+
env_list = ["testing"]
3+
4+
[env.testing]
5+
description = "Run pytest"
6+
deps = ["pytest>=8"]
7+
commands = [["pytest"]]

05_testing_and_ci/python_testing_demo.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pip install -U pytest
2424
- All tests can be run using the command-line tool called `pytest`. Just type `pytest` in the working directory and hit ENTER.
2525
- If pytest is installed in some other way, you might need to run it like `python -m pytest`.
2626
- One test is expected to fail. Reading the error message we understand that the failure occurs because floating-point variable comparison is not handled correctly.
27-
- We need to tell pytest that while comparing two floating-point variables the value needs to be correct only up to a certain tolerance limit. To do this the expected mean value needs to be changed by uncommenting the line in the following part of the code:
27+
- We need to tell pytest that while comparing two floating-point variables the value needs to be correct only up to a certain tolerance limit. To do this, the expected mean value needs to be changed by uncommenting the line in the following part of the code:
2828

2929
```python
3030
# Expected result
@@ -73,6 +73,7 @@ tests/
7373
```
7474

7575
- Putting the tests in a folder `tests/` does not affect the behavior of pytest. When pytest is run from the original directory, the tests are found and run.
76+
- **Note**: revert to the old directory structure before proceeding to the next section.
7677

7778
## unittest
7879

@@ -125,20 +126,16 @@ coverage html
125126

126127
- Environment orchestrator to setup and execute various tools for a project.
127128
- `tox` creates virtual environments to run each tools in.
128-
- Order of preference for files that tox tries to read: `pyproject.toml`, `tox.toml`, `setup.cfg`
129129
- `tox.toml` file:
130130

131131
```toml
132132
requires = ["tox>=4"]
133133
env_list = ["testing"]
134134

135-
[env_run_base]
136-
description = "run unit tests"
137-
deps = [
138-
"pytest>=8",
139-
"pytest-sugar"
140-
]
141-
commands = [["pytest", { replace = "posargs", default = ["tests"], extend = true }]]
135+
[env.testing]
136+
description = "Run pytest"
137+
deps = ["pytest>=8"]
138+
commands = [["pytest"]]
142139
```
143140

144141
- Global settings defined under section at the top of the `tox.toml` file.

0 commit comments

Comments
 (0)