Skip to content

Commit 64af987

Browse files
authored
Update readme.md
1 parent 984bb94 commit 64af987

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

Pytest/readme.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,25 @@ platform win32 -- Python 3.9.1, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 -- c:\pro
1717
cachedir: .pytest_cache
1818
rootdir: C:\Users\TirthajyotiSarkar\Documents\Python Notebooks\Pytest
1919
plugins: anyio-2.0.2
20-
collected 7 items
21-
22-
test_linear_model.py::test_model_return_object PASSED [ 14%]
23-
test_linear_model.py::test_model_return_vals PASSED [ 28%]
24-
test_linear_model.py::test_model_save_load PASSED [ 42%]
25-
test_linear_model.py::test_loaded_model_works PASSED [ 57%]
26-
test_linear_model.py::test_model_works_data_range PASSED [ 71%]
27-
test_linear_model.py::test_noise_impact PASSED [ 85%]
28-
test_linear_model.py::test_wrong_input_raises_assertion PASSED [100%]
20+
collected 9 items
21+
22+
test_linear_model.py::test_model_return_object PASSED [ 11%]
23+
test_linear_model.py::test_model_return_vals PASSED [ 22%]
24+
test_linear_model.py::test_model_save_load PASSED [ 33%]
25+
test_linear_model.py::test_loaded_model_works PASSED [ 44%]
26+
test_linear_model.py::test_model_works_data_range_sign_change PASSED [ 55%]
27+
test_linear_model.py::test_noise_impact PASSED [ 66%]
28+
test_linear_model.py::test_additive_invariance PASSED [ 77%]
29+
test_linear_model.py::test_wrong_input_raises_assertion PASSED [ 88%]
30+
test_linear_model.py::test_raised_exception PASSED [100%]
2931
3032
========================================================================================================= warnings summary =========================================================================================================
3133
..\..\..\..\..\..\program files\python39\lib\site-packages\win32\lib\pywintypes.py:2
3234
c:\program files\python39\lib\site-packages\win32\lib\pywintypes.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
3335
import imp, sys, os
3436
3537
-- Docs: https://docs.pytest.org/en/stable/warnings.html
36-
=================================================================================================== 7 passed, 1 warning in 1.03s ===================================================================================================
38+
=================================================================================================== 9 passed, 1 warning in 0.96s ===================================================================================================
3739
```
3840

3941
### What does it mean?
@@ -44,10 +46,11 @@ test_linear_model.py::test_wrong_input_raises_assertion PASSED
4446

4547
### Notes on the test module
4648

47-
- Note, how the `test_linear_model.py` contains 7 functions with names starting with `test...`. Those contain the actual test code. It also has a couple of data constructor functions whose names do not start with `test...` and they are ignored by Pytest.
49+
- Note, how the `test_linear_model.py` contains 9 functions with names starting with `test...`. Those contain the actual test code. It also has a couple of data constructor functions whose names do not start with `test...` and they are ignored by Pytest.
4850

49-
- Note that we need to import a bunch of libraries to test all kind of things e.g. we imported libraries like `joblib`, `os`, `sklearn`, `numpy`, and of course, the `train_linear_model` function from the `linear_model` module.
51+
- Note that we need to import a variety of libraries to test all kind of things e.g. we imported libraries like `joblib`, `os`, `sklearn`, `numpy`, and of course, the `train_linear_model` function from the `linear_model` module.
5052
- Note the **clear and distinctive names** for the testing functions e.g. `test_model_return_object()` which only checks the returned object from the `train_linear_model` function, or the `test_model_save_load()` which checks whether the saved model can be loaded properly (but does not try to make predictions or anything). **Always write short and crisp test functions with a singular focus**.
5153
- For checking the predictions i.e. whether the trained model really works or not, we have the `test_loaded_model_works()` function which uses a fixed data generator with no noise (as compared to other cases, where we can use a random data generator with random noise). It passes on the fixed `X` and `y` data, loads the trained model, checks if the R^2 ([regression coefficient](https://www.geeksforgeeks.org/python-coefficient-of-determination-r2-score/)) scores are perfectly equal to 1.0 (true for a fixed dataset with no noise) and then compare the model predictions with the original ground truth `y` vector. Note, how it uses a **special Numpy testing function `np.testing.assert_allclose` instead of the regular `assert` statement**. This is to avoid any potential numerical precision issues associated with the model data i.e. Numpy arrays and the prediction algorithm involving linear algebra operations.
5254
- Take a look at the `random_data_constructor` and `fixed_data_constructor` functions too to see how they are designed and used in the test code. The `random_data_constructor` even takes a `noise_mag` argument which is used to control the magnitude of noise to test the expected behavior of a linear regression algorithm. Refer to the `test_noise_impact` function for this.
5355
- Finally, take a look at the `test_wrong_input_raises_assertion` function which tests **if the original modeling function raises the correct Exception and messages** when fed with various types of wrong input arguments. Always remember that **Pytest (or any testing module for that matter) is not to be used for error-handling** (e.g. a wrong input type). The developer (ML sofware engineer in this case) must write the necessary error-checking and handling code. In this example, we write a bunch of `assert` statements in the original `train_linear_model` function and wrap them around a whole `try...except` block. The test code only checks if we are returning the `AssertionType` error for those wrong input cases and if the correct error message is being printed for the end user.
56+
- The `test_raised_exception` function tests the rise of other exception types (i.e. different from the `AssertionError` that could be raised by the `assert` statements in the function module) during runtime using a special Pytest feature - [the `pytest.raises` context manager](https://docs.pytest.org/en/reorganize-docs/new-docs/user/pytest_raises.html).

0 commit comments

Comments
 (0)