Testing
Classes inheriting from TorchApp
can be tested automatically by inheriting from the TorchAppTestCase
.
This creates a number of automated tests designed to be used in conjunction with pytest
.
The expected results of the tests are definied in yaml files in a directory named expected
next to the scripts with the testing code
and in a subdirectory with the class name which is an instance of TorchAppTestCase
. Each test has its own subdirectory there.
There can be multiple expected files for testing the same method to test different combinations of arguments.
The format for the yaml files is:
params: {...}
output: "..."
The params gives a list of keyword arguments for the TorchApp method being tested and the output is some data in a format required by the testing function. For example the expected output for the model method is the string representation of the model definied by the params.
If you want to change the expected output for a test then run pytest with the -s
option. This will find failing tests and prompt the user to ask if the expected file should be regenerated.
One way to automatically create expectation files is to add blank files and prompt for adding the expected result.
The resulting file should be read to make sure that it is correct.
Logistic Regression App Example
For instance, the LogisticRegression app included as an example in this project is tested as follows in tests/test_logistic.py
.
from torchapp.testing import TorchAppTestCase
from torchapp.examples.logistic_regression import LogisticRegressionApp
class TestLogisticRegressionApp(TorchAppTestCase):
app_class = LogisticRegressionApp
The expected values for the tests are in tests/expected/TestLogisticRegressionApp
.
The file for testing the model is in tests/expected/TestLogisticRegressionApp/test_model/model.yaml
:
params: {}
output: Linear(in_features=1, out_features=1, bias=True)
TorchAppTestCase Reference
- class torchapp.testing.TorchAppTestCase
Automated tests for TorchApp classes
- get_app() torchapp.apps.TorchApp
Returns an instance of the app for this test case.
It instantiates an object from app_class. Override app_class or this method so the correct app is returned from calling this method.
- get_expected_dir() pathlib.Path
Returns the path to the directory where the expected files.
It creates the directory if it doesn’t already exist.
- perform_subtests(interactive: bool, name: str)
Performs a number of subtests for a method on the app.
- Parameters
interactive (bool) – Whether or not the user should be prompted for creating or regenerating expected files.
name (str) – The name of the method to be tested with the string test_ prepended to it.
- test_model(interactive: bool)
Tests the method of a TorchApp to create a pytorch model.
The expected output is the string representation of the model created.
- Parameters
interactive (bool) – Whether or not failed tests should prompt the user to regenerate the expected files.