Skip to content

Commit 96d551e

Browse files
authored
Update readme.md
1 parent 032dca9 commit 96d551e

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

Pytest/readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ test_linear_model.py::test_wrong_input_raises_assertion PASSED
4545
- 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.
4646

4747
- 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.
48-
- 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).
49-
- 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$ 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.
48+
- 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**.
49+
- 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.
5050
- 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.
51+
- 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.

0 commit comments

Comments
 (0)