Initializing help system before first use

Parameterized Unit Testing Using Pytest

Manual setup of individual tests might not be suitable for real-world, large-scale applications where tests must run automatically on several files and functions. Parameterized testing with pytest makes it possible to automate the same tests with multiple sets of input data, eliminating the need for redundant test code.

For more information about parameterized testing, see the pytest documentation.

This procedure assumes that you have already set up unit testing using pytest. For more information, see Setting Up Unit Tests Using the Pytest Framework.
To set up parameterized testing, follow these steps:
  1. In your test_application.py file, after the package import statements, insert the decorator @pytest.mark.parametrize.
    Define the names and values or data structures of the parameters that you want to use to validate the test:
    @pytest.mark.parametrize(
        ('input_value', 'expected_output'),
        (
                (0.1, 0.0),
                (0.2, 13.42),
                (0.3, 14.24),
        )
    )
    
  2. Change the definition of the test_function function to accept the defined parameters in addition to tmp_path.
    Use these passed values in an assert statement. Note that you must use the same variable names as in the decorator:
    def test_app(input_value: float, expected_output: float, tmp_path: pathlib.Path):
        """ Parametrized testing """
    
        app = xi.create_app(InsightApp, app_work_dir=str(tmp_path))
        app.call_exec_mode('LOAD')
    
        app.InputEntity = input_value
        app.data_connector.save_input()
        app.call_exec_mode('RUN')
    
        assert app.ResultEntity == pytest.approx(expected_output)
    
  3. Run the test_applicationuse.py file in your IDE or terminal.
    You will see the results for all tests, including whether each test passed or failed and the run log of each individual instance.

© 2001-2024 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.