FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browser

Testing an Insight Python model

Description

Files for testing the Insight-Python example "Portfolio Optimization" in the Xpress distribution using pytest. The full app forms part of the Insight developer kit. Assuming this has been extracted into the Xpress installation directory XPRESSDIR, you can find the full app in XPRESSDIR/examples/insight/basic_apps/python/portfolio_optimization

Requires pytest to be installed within the Python environment

Instructions for using these files:
  1. Copy the application.py file into the python_source folder within the app's root directory, replacing the existing one.
  2. Create a new folder named test in the app's root directory, and place the test_application.py file into that folder.
  3. Run the test_application.py file from your IDE or run "pytest" in the command line from the test folder.


Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
application.py[download]
test_application.py[download]
shares.csv[download]





test_application.py

# Example file to set up parametrized unit tests for Xpress Insight - Portfolio Optimization example
# Copyright (c) 2020-2024 Fair Isaac Corporation. All rights reserved.

import os
import sys

import pytest
import xpressinsight as xi

sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'python_source'))
from application import InsightApp


@pytest.mark.parametrize(
    ('input_value', 'expected_output'),
    (
            (0.1, 0.0),
            (0.2, 13.42),
            (0.3, 14.24),
    )
)
def test_app(input_value, expected_output, tmp_path):
    """ Parametrized testing """

    app = xi.create_app(InsightApp, app_work_dir=str(tmp_path))
    app.call_exec_mode('LOAD')

    app.MaxPerShare = input_value
    app.data_connector.save_input()
    app.call_exec_mode('RUN')

    assert app.TotalReturn == pytest.approx(expected_output)


if __name__ == "__main__":
    pytest.main()

Back to examples browser