Setting Up Unit Tests Using the Pytest Framework
As your Python model grows in complexity and size, you might need to implement automated testing in order to maintain its functionality. The Python package pytest
provides a framework for reusing testing functions and objects as part of a test routine, which can help keep your testing logic organized and maintainable.
Pytest
is backward-compatible with the
unittest
package.
pytest
package installed.
To set up unit tests for Xpress Insight applications using pytest
, follow these steps:
- In your project root directory, create a new test subdirectory.
This folder will be the location for unit tests. This folder will not be distributed when you deploy the app.
- In the test subdirectory, create a new Python test file.
The name of this file must start with the prefix test_ (for example, test_application.py).
- In the header of the new file, import the
pytest
andxpressinsight
packages:import pytest import xpressinsight as xi
- Make your Python modules visible to the test code.
Import the native
os
andsys
packages and then append the python_source directory to the list of directories the interpreter will search for modules:import os import sys sys.path.append(os.path.join(os.path.dirname(__file__),'..','python_source')) from application import InsightApp
- Move the
__main__
function from your application Python file to the new test_application.py file and redefine it as a standard function.The new function name must start with test_, and it must accept atmp_path
parameter, which specifies the name of a temporary directory thatpytest
will create when the test runs. This temporary directory is unique to the test invocation and therefore does not conflict with the Xpress Insight temporary working directory work_dir. (You can set the value of thistmp_path
parameter using thexpressinsight.create_app
argumentapp_work_dir
.)def test_app(tmp_path: pathlib.Path): """ Unit testing """ app = xi.create_app(InsightApp, app_work_dir=str(tmp_path)) app.call_exec_mode('LOAD') app.InputEntity = 0.2 app.data_connector.save_input() app.call_exec_mode('RUN')
- At the end of the
test_
function, write anassert
statement as a Boolean expression that tests whether some outcome matches the expectations for a given value or set of values.This assertion must evaluate toTrue
for the test to be successful. For an equality assertion, you can wrap the expected value in thepytest.approx()
function to match a result value that lies within a relative tolerance (the default tolerance is1e-6
(1 part in 1 million). For example, this assertion tests for an entity result value that is equal to13.42
, within the default tolerance:assert app.ResultEntity == pytest.approx(13.42)
- Set the main function of the new test_application.py file to invoke
pytest.main()
to run the tests directly from your IDE.You can run test_application.py with the new test directory as the active working directory:if __name__ == "__main__": pytest.main()
Alternatively, you can run pytest from the test directory at a terminal prompt.pytest
automatically discovers all tests in files whose names start withtest_
and runs all functions that start withtest_
. Use the -v flag to show the result ( PASSED or FAILED) from each test, and the -s flag to show the run log for each test:pytest -v -s
You can also run the tests in parallel by specifying the -n flag:
where num is the number of parallel workers to use. If you are settingpytest -n num
app_work_dir
as thetmp_path
, each new test runs in a separate subdirectory in your temporary working directory. The last 3 temporary subdirectories are kept, with older subdirectories deleted after eachpytest
run.
© 2001-2025 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.