Modeling Guide

Creating Tests

When you create a new operator extending BaseOperator, you can also create unit tests using Python's unit testing framework.

We recommend that you create a folder called test in your project root (side-by-side with the folder "my_solution" in our sample hierarchy) and inside it use the same folder structure you used when creating the operator. For example, for the operator appendString, we will create the following folders:


And create a file named inside appendString folder. This file is shown below:

import unittest
from Queue import Queue
from import AppendString

class TestAppendString(unittest.TestCase):

    def testUpper(self):
        op ="1", op_id="anything")
        qin1 = Queue(maxsize=1)
        op.inqs["inString"] = qin1
        qout1 = Queue(maxsize=1)
        op.outqs["outString"] = qout1

        input_config = "config_test"
        op.config.stringToAppend = input_config
        op.config.method = "UPPERCASE"

        op.base_init()  # This will execute the _init method and also other things.

        op.start()  # Start the operator's thread.

            input_str = "input_test|"
            qin1.put(input_str)  # Send data in the 'inString' input port.
            ret = qout1.get()  # Gets data from the 'outString' output port.

            self.assertEqual((input_str + input_config).upper(), ret)  # Verify that the output is equal to the expected result.
            op.stop()  # stop the operator no matter what happens. Otherwise the test may hang forever.

You can make an script like the one below to run unittests for all python files starting with the prefix test:

PYSIX_PATH=<PYSIX_SUBENGINE_PATH>  # replace <PYSIX_SUBENGINE_PATH> by the path to your pysix_subengine folder.

SCRIPT_DIR=$(dirname "$0")
cd $SCRIPT_DIR  # changes working dir to the folder where the script is located (which should be the 'test' folder).

SUBENGINE_ROOT=$(realpath ../my_pysolution/vrep/vflow/subengines/com/sap/python27)  # Gets absolute path of python27 folder.

export PYTHONPATH=$PYTHONPATH:$PYSIX_PATH:$SUBENGINE_ROOT  # Append paths to pythonpath.

# Or call with python3.6 if your operators are written in python3.6
python2.7 -m unittest discover -s . -p "test*.py" -v

cd -

The script above should be placed inside the test directory you created.

You can check the official documentation of Python's unittest module by accessing this linkInformation published on non-SAP site.