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
+25-22Lines changed: 25 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -32,12 +32,12 @@ pip install -U pytest
32
32
# expected_result = pytest.approx(78.3, abs=0.01)
33
33
```
34
34
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`.
36
36
- 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:
- 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.
80
80
- Each test is now a function of a class which is derived from the class `unittest.TestCase`.
81
81
- 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:
83
83
84
84
```bash
85
85
python -m unittest
86
86
```
87
87
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.
90
90
-`setUp` function is executed before all the tests. Similar a clean up function `tearDown` exists.
91
91
- The intention is to group together sets of similar tests in an instant of `unittest.TestCase` and have multiple such instances.
92
92
- 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
123
123
124
124
## tox
125
125
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`
- 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.
143
146
- 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.
144
147
- Observe that tox starts a virtual environment, installs the dependency (here `pytest`) and runs `pytest`.
0 commit comments