Skip to content

Commit f9612e8

Browse files
committed
Rework Python testing demo and content slides
1 parent ea60b17 commit f9612e8

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

05_testing_and_ci/python_testing_demo.md

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ pip install -U pytest
3232
# expected_result = pytest.approx(78.3, abs=0.01)
3333
```
3434

35-
- **Comparing floating point variables** needs to be handled in functions like `find_average` and is done using `pytest.approx(value, abs)`. The `abs` value is the tolerance up to which the floating-point value will be checked, that is `78.33 +/- 0.01`.
35+
- **Comparing floating point variables** needs to be handled in functions like `find_mean` and is done using `pytest.approx(value, abs)`. The `abs` value is the tolerance up to which the floating-point value will be checked, that is `78.33 +/- 0.01`.
3636
- Even if one test fails, pytest runs all the tests and gives a report on the failing test. The assertion failure report generated my pytest is also more detailed than the usual Python assertion report. When the test fails, the following is observed:
3737

3838
```bash
39-
=============================================== FAILURES ====================================================
40-
____________________________________________ test_find_mean _________________________________________________
39+
========================================== FAILURES ===============================================
40+
_______________________________________ test_find_mean ____________________________________________
4141

4242
def test_find_mean():
4343
"""
@@ -76,17 +76,17 @@ tests/
7676

7777
## unittest
7878

79-
- Base class `unittest.TestCase` is used to create a test suite consisting of all the tests of a software.
79+
- Base class `unittest.TestCase` is used to create a test suite.
8080
- Each test is now a function of a class which is derived from the class `unittest.TestCase`.
8181
- 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`.
82-
- unittest can be run by:
82+
- unittest can be run as a Python module:
8383

8484
```bash
8585
python -m unittest
8686
```
8787

88-
- unittest.TestCase offers functions like `assertEqual`, `assertAlmostEqual`, `assertTrue`, etc. for use instead of the usual assertion statements. These statements help the test runner to accumulate all test results and generate a test report.
89-
- `unittest.main()` provides an option to run the tests from a command-line interface.
88+
- 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.
89+
- `unittest.main()` provides an option to run the tests from a command-line interface and also from a file.
9090
- `setUp` function is executed before all the tests. Similar a clean up function `tearDown` exists.
9191
- The intention is to group together sets of similar tests in an instant of `unittest.TestCase` and have multiple such instances.
9292
- Decorators such as `@unittest.skip`, `@unittest.skipIf`, `@unittest.expectedFailure` can be used to gain flexibility over working of tests.
@@ -123,22 +123,25 @@ coverage html
123123

124124
## tox
125125

126-
- Automation for Python testing (and much more)
127-
- Virtual environments are created for each task, and tox takes care of installing dependencies and the package itself inside of the environment.
128-
- Order of preference for files that tox tries to read: `pyproject.toml`, `tox.ini`, `setup.cfg`
129-
- `tox.ini` file
130-
131-
```ini
132-
[tox]
133-
envlist = my_env
134-
skipsdist = true
135-
136-
[testenv]
137-
deps = pytest
138-
commands = pytest
126+
- Environment orchestrator to setup and execute various tools for a project.
127+
- `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`
129+
- `tox.toml` file:
130+
131+
```toml
132+
requires = ["tox>=4"]
133+
env_list = ["testing"]
134+
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 }]]
139142
```
140143

141-
- Global settings defined under section `[tox]` in the INI file.
142-
- Start tox by running the command `tox` in the directory where the `tox.ini` exists.
144+
- Global settings defined under section at the top of the `tox.toml` file.
145+
- Start tox by running the command `tox` in the directory where the `tox.toml` exists.
143146
- 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.
144147
- Observe that tox starts a virtual environment, installs the dependency (here `pytest`) and runs `pytest`.

05_testing_and_ci/python_testing_slides.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ slideOptions:
4646

4747
- Python framework specifically designed to run, monitor and automate unit tests.
4848
- Many features like test automation, sharing of setup and shutdown of tests, etc.
49-
- Using the base class `unittest.TestCase` to create a test suite.
49+
- Use the base class `unittest.TestCase` to create a test suite.
5050
- Command-line interface: `python -m unittest test_module1 test_module2 ...`.
5151

5252
---
@@ -69,19 +69,27 @@ slideOptions:
6969

7070
## tox
7171

72-
- Automation for Python testing, building and distribution
73-
- Creates virtual environments for each process
74-
- Depending on the command, dependencies are installed, tests are run, packaging is done, etc.
75-
- tox command line tool reads and runs files like `pyproject.toml`, `tox.ini`, and `setup.cfg`
76-
- More information in the [tox wiki](https://tox.wiki/en/4.0.15/index.html).
72+
- Environment orchestrator to setup and execute various tools for a project.
73+
- Creates virtual environments for each process.
74+
- Processes include testing, linting, building, documentation generation, and more.
75+
- Configuration via `tox.toml` or `tox.ini` file.
7776

7877
---
7978

8079
## tox Demo
8180

8281
---
8382

84-
## Further reading
83+
## Other Testing Frameworks
84+
85+
- [nose](https://pypi.org/project/nose2/) is an extension to `unittest` with added plugins.
86+
- [testify](https://pypi.org/project/testify/) based on unittest and nose with additional features.
87+
- [robotframework](https://pypi.org/project/robotframework/) is a generic automation framework.
88+
89+
---
90+
91+
## Further Reading
8592

8693
- [pytest documentation](https://docs.pytest.org/en/6.2.x/)
8794
- [unittest documentation](https://docs.python.org/3/library/unittest.html)
95+
- [tox user guide](https://tox.wiki/en/4.23.2/user_guide.html#user-guide)

0 commit comments

Comments
 (0)