Blog Post

FICO® Xpress Optimization Blog
4 MIN READ

Test mode for Xpress Insight Python apps

FICO's avatar
FICO
Community Manager
2 years ago
Testing a program is a major part of the development phase of any software application. From unit tests to integration and user interface testing, each of the test types plays an important role in ensuring the quality and reliability of the software. They are typically used at different stages of the development lifecycle, depending on the specific goals and requirements of the project.

View the video tutorial on “Test mode for Insight-Python apps”


Unit testing allows developers to verify each part of their software and are performed at the very beginning of the development lifecycle, when changes are usually faster and less costly. By testing individual components, developers validate their functionality and ensure that the code behaves as intended. Over time, consistent and automated unit testing leads to a more refined and robust codebase, which ultimately translates to satisfied users and stakeholders.

If you are using Xpress Insight as a user-friendly interface for building and deploying analytical models and easily deploy them as powerful business applications, you may want to be able to test parts of the model behavior outside of the Insight Server. Mosel already provides an ad-hoc framework for testing Insight applications without deploying it to the server. But how to do this for Python-based Insight apps? In this article we will show how you can set up test mode for your Insight-Python application. You can also find a tutorial in the online documentation.

Note: In this example, we use the Portfolio Optimization application distributed with the Insight developer kit (IDK) of Xpress Insight that comes with the FICO Xpress community license. You can find the app in:
IDK\examples\insight\basic_apps\python\portfolio_optimization

Testing an Insight-Python model “offline”
To set up a test environment for Insight-Python apps by using the xpressinsight.create_app function within the main function of your Python source file. The function accepts an InsightApp argument, which is a subclass of the main class AppBase that allows the creation of Insight application objects. By default, the xi.create_app function first deletes the temporary application working directory app_work_dir, if it exists, and then initializes a new one before copying the contents of the model_resources folder to the new temporary directory:
if __name__ == "__main__":   
    app = xi.create_app(InsightApp)
    app.call_exec_modes(["LOAD","RUN"])
Using the xpressinsight native property named data_connector, you can easily simulate how your program will behave when your Python code is run outside of Insight (“offline mode”).  By using its save_input() method, you can apply modified entity values in between execution modes:
if __name__ == "__main__":
    app = xi.create_app(InsightApp)
    app.call_exec_mode("LOAD")

    # Simulate several scenarios
    for value in range(1,11):
        app.MaxHighRisk = value/10
        app.data_connector.save_input()
        app.call_exec_mode('RUN')
The above code snippet modifies the value assigned to the input entity MaxHighRiskfrom 0.33 (default) to 0.2, and then calls the RUN execution mode a second time, printing the outputs for both runs. This allows the developer to confirm that the model behaves as expected, i.e. that the expected return decreases when the share of the budget that can be allocated to high-risk shares becomes more restricted.



The test environment also allows you to evaluate several scenarios of values assigned to Insight entities in a single run. The code snippet below simulates 10 scenarios consecutively by incrementing the value of the entity MaxHighRiskfrom 0.1 to 1, in steps of 0.1, and by calling the RUN execution mode after each increment:
if __name__ == "__main__":
    app = xi.create_app(InsightApp)
    app.call_exec_mode("LOAD")

    # Simulate several scenarios
    for value in range(1,11):
        app.MaxHighRisk = value/10
        app.data_connector.save_input()
        app.call_exec_mode('RUN')
View the video tutorial on “Test mode for Insight-Python apps”
When developing an Insight app, it is important to ensure that a test mode is not activated to run with your application within the Xpress Insight server. Test code should only be used in an external, non-production environment that does not get deployed to the Insight server, and therefore must be set up in a separate folder that does not get distributed with the app. Moreover, as your Python model grows in complexity and size, you might need to implement automated and parametrized unit testing in order to maintain control over its functionality. The open-source 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.

Stay tuned to learn more about leveraging pytest for unit testing Insight apps. Until then, you are more than welcome to try the community edition of FICO Xpress Insight and implement this tutorial on top of the Portfolio Optimization example app available in the Insight developer kit.
Updated 2 years ago
Version 3.0
No CommentsBe the first to comment