Chapter 2. Design

Table of Contents

2.1. Test Cases
2.1.1. Defining a Test Case
2.1.2. Implementing the Test Functions
2.1.3. setUp and tearDown Functions
2.1.4. Logging and Status Detection
2.1.5. Handling Changes to the Environment
2.1.6. Test Case Suites
2.2. Master-Slave Approach
2.3. TestSupport Python Package
2.4. Handling Input/Output Data
2.4.1. Handling Ground Truth Data
2.4.2. Handling Result Data
2.4.3. Result Handling

In the following chapter, the design of the TestCenter is discussed.

2.1. Test Cases

The test cases are the key elements of the testing framework. To add a new test case, two steps are necessary:

  • Define the basic parameters of the test case, such as test type, name, and script file. With this information, the TestCenter recognizes the test case.

  • Define the actual test, for example, by adding the instructions that should be executed to the script file defined above.

2.1.1. Defining a Test Case

Test cases use the MeVisLab infrastructure. Similar to the database for modules, there is a database for test cases. This database is filled with entries by searching for .def files in the TestCases subdirectories of all packages. Each .def file can contain the definition of multiple test cases.

The two test types are defined by test case definitions like the following:

FunctionalTestCase FunctionalExample {
    scriptFile = $(LOCAL)/FunctionalExample.py
}

GenericTestCase GenericExample {
    scriptFile = $(LOCAL)/GenericExample.py
}
                    

The given example creates a functional and a generic test case. The provided information is collected in an entry that can be accessed using the testCaseInfo method of the TestCaseDatabase.

The scriptFile key specifies which file contains the actual test functions (see Section 2.1.2, “Implementing the Test Functions”). The script files are used to load the actual test cases, which behave like macro modules in the MeVisLab context. This approach has the benefit that a network named after the test case and located in the same folder (for example, $(LOCAL)/GenericExample.mlab) is also loaded as part of the context.

Table 2.1. Keys for Test Case Definition

Key Description
author A comma-separated list of author names. For functional tests, the author names can be used to send email notifications for failed automatic tests.
comment A short description of what the test case tests.
timeout The time in seconds the test case should take at most. If it takes longer, it is terminated. The default value is given in the TestCenter configuration file, see Chapter 5, TestCenter Configuration File.
scriptFile The file containing the actual test specification.
testGroups A comma-separated list of group names the test belongs to. This information can be used to run the test cases of only one group (for example, all test cases of the examples group). It can also be used to exclude tests belonging to a specific group from being tested. There are two special test groups: automatic and manual. All test cases that do not belong to the manual group are part of the automatic group by default. Test cases that for some reason cannot be used in automatic testing should be part of the manual group.
dataDirectory The path to a directory containing the ground truth data for the test case. The default value is "$(LOCAL)/data". This directory should be used as if it were read-only. See Section 2.4.1, “Handling Ground Truth Data” for further information.
showTestFunctionSortingLiterals If set to True/Yes/1, the sorting literals (the string between TEST and the first underscore before the actual test name) are shown in the function list in the TestCaseManager and in the resulting HTML report for easy reference.
preferredRenderer An optional key that indicates what type of renderer should be used for testing. Possible values are software and hardware. If the key is set to software, the test case is run using software rendering. If it is set to hardware, hardware rendering is used for the test case, if available. If software rendering has been forced via the environment variable MLAB_FORCE_MESA, a warning about conflicting settings is logged. If the key is not set, the rendering uses the system default.

All test cases must reside in a package's TestCases directory. It is recommended to use a substructure similar to the modules directory (for example, TestCases/FunctionalTests/[module_type]/[module_name]).

The location of generic tests is important, as the set of modules they test depends on the package they are in.

  • In general, a generic test runs on all modules in that package.

  • If the generic test is located in a group's Foundation package, all modules of that group are tested.

  • Generic tests located in the MeVis/Foundation package are executed for every module available.

2.1.2. Implementing the Test Functions

Test cases are scripted in Python. Each test case consists of an arbitrary number of test functions. These functions each have their own result state. The worst result state of a test case's functions determines the overall result state of the test case, see Section 2.1.4, “Logging and Status Detection”.

Similar to the Google test framework, test functions are given a special prefix like TEST.

There are three major types of test functions that can be used: simple, iterative, and field-value test functions. In addition, multiple simple test functions can be combined into a group. The following sections describe the available test function prefixes and types.

2.1.2.1. Simple Functions

TEST: The simple test function is a single function that runs once.

2.1.2.2. Advanced Functions

For the more advanced test function types, the concept of virtual test functions was introduced. A simple test function maps to a single virtual test function, just as each iteration of an iterative test function maps to one. The virtual functions are named after the non-virtual parent function. For simple test functions, both names match. For field-value tests, the name of the field-value test case executed in the test function is appended. The iterative tests are more complicated: In the case of returning a list, the index of the parameter is appended to the function name. If a dictionary is returned, the keys are appended to the name, and the values of the keys are used as parameters.

The following advanced test types are available:

  • ITERATIVETEST: Iterative test functions run a function for every specified input. They return a pair consisting of the inputs to iterate over and the function object called. The following examples call the function actualTestFunction three times, with the parameters being ("first", 1), ("second", 2), and ("third", 3), respectively.

    # Generates TestFromList_0, TestFromList_1, and TestFromList_2
    def ITERATIVETEST_TestFromList():
        return [("first", 1), ("second", 2), ("third", 3)], actualTestFunction
    
    # Generates TestFromDict_Apple, TestFromDict_Banana, and TestFromDict_Cherry
    def ITERATIVETEST_TestFromDict():
        return {"Apple": ("first", 1), "Banana": ("second", 2), "Cherry": ("third", 3)}, actualTestFunction
    
    def actualTestFunction(firstParameter, secondParameter):
        pass
                                    

    Iterative test functions are useful if the same function is applied to different input data, for example, input values or names of input images.

  • FIELDVALUETEST: Field-value test functions integrate the field-value test concept into the TestCenter. The path to the XML file containing the field-value test specification is returned, along with an optional list of contained field-value test cases. The following example executes all field-value test cases defined in the fieldvaluetestcase.xml file located in $(LOCAL)/data.

    def FIELDVALUETEST_ExampleTestFunction():
        return "$(LOCAL)/data/fieldvaluetestcase.xml"
                                    

    The field-value test case concept was introduced to allow easy parameterization in the network and to verify the values of different fields.

  • GROUP: This test function combines multiple simple test functions into a named group. These functions just return the function objects of the simple test cases that should be members of the group. There is no technical reason for having these test groups, but they improve the understanding of complex test cases with many test functions.

  • UNITTEST: This test function wraps existing Python tests implemented with the unittest module. It returns a TestSuite that can contain test functions and other TestSuites. For each unit test function, a test function is added to a group with the name of the wrapper function. This test function is named after the test suite and the unit test function.

    from backend import getSuite
    
    def UNITTEST_ExampleUnitTestWrapper():
        return getSuite()
                                    

All function names follow the PREFIX[order]_[name] syntax. The [order] substring can be an arbitrary sequence of numbers that defines the order of the test functions. This is only needed if the execution order matters, which it should not for independent tests. The [name] part is later used to identify the method and should therefore be selected with care.

2.1.3. setUp and tearDown Functions

Two special functions are available that can be used to maintain global information for a test case: setUpTestCase and tearDownTestCase. They are called before the execution of the first test function and after the last, respectively. The outputs of setUpTestCase and tearDownTestCase are appended to the first and last test function, respectively.

As in other unit-testing frameworks, it is also possible to define setUp and tearDown functions that are called before and after every test function, respectively. The first setUp is called after setUpTestCase, and the last tearDown is called before tearDownTestCase.

2.1.4. Logging and Status Detection

A testing framework must determine whether a test has failed or succeeded.

The MeVisLab debug console shows messages with a type. All sources of output are mapped to this console: standard output, standard error, and other special methods provided inside MeVisLab for logging. Based on this information, the TestCenter determines the current test function status.

In addition to these status types, there are the TestCenter-specific types “Timeout ” and “Crashed”. They are used for situations where testing fails. For more information on failure handling, see Section 2.2, “Master-Slave Approach”.

The test case status is determined by the worst outcome of the execution of its test functions. In addition, the TestCenter collects all messages printed in the debug console to display them in the reports for detailed introspection into what happened.

The following example shows a simple test function that verifies a condition and prints an error message if the condition is not met. This results in the test function having the status “Error”.

import mevis

def TEST_Simple_Test():
    if not condition:
        mevis.MLAB.logError("The condition wasn't met!")
    else:
        mevis.MLAB.log("Everything is fine.")
                    

As the mechanism leads to messages being collected from resources besides the test functions, it detects all possible problems occurring during testing. For example, if an ML module fails and prints messages of type “ML Error”, this results in the test function being marked as failed.

Messages coming from the TestCenter itself may look different from messages from modules. This way, irrelevant parts of the messages can be filtered out more easily. The distinction is made using special logging methods inside the TestCenter that are part of the TestSupport Python package (see Section 2.3, “TestSupport Python Package”). For example:

from TestSupport import Logging

def TEST_Simple_Test():
    if not condition:
        Logging.error("The condition wasn't met!")
    else:
        Logging.info("Everything is fine.")
                    

[Tip] Tip

Other examples of info output methods are print("The variable has changed.") and MLAB.log("The variable has changed."), but using the Logging Python module makes it easy to distinguish the test as the message source.

2.1.5. Handling Changes to the Environment

During a test, the environment is usually changed. For example, a test function sets field values and touches triggers to start a computation based on the set values. This becomes a problem when the next test function is influenced by these changes.

  • Unit testing frameworks prevent this problem by reloading the environment for every single test function. In the case of the TestCenter, this is not an option, as loading MeVisLab and the test network might require significant time and resources.

  • However, test functions also cannot rely on the state of previous test functions as there is a mechanism to detect crashes and retry the corresponding test function. This second attempt would have a different state than expected, as the previous functions are not run again.

  • Manually resetting all values a computation relies on would be possible, but it is also a daunting task.

In the TestCenter, the problem is solved by the TestSupport.Fields Python module. It provides methods to set field values in a way that makes resetting the original values possible.

The foundation is the ChangeSet concept (see the TestCenterReference, ChangeSet.py). It relies on the idea that a subset of input field values is set to parameterize the following computation. Resetting the input values to their original values should be sufficient to reset the environment to an expected state. However, this is only a heuristic.

[Note] Note

The ChangeSets only work for field value changes made using the corresponding methods. They have no effect on indirect changes, for example, changes due to computations started by touching trigger buttons.

The TestCenter itself maintains a stack of such ChangeSets to split the test case execution into logical blocks. A first ChangeSet is pushed while loading a test case and popped when it is destroyed. When running a test function, a second ChangeSet is pushed before and popped afterward.

[Tip] Tip

You can also use the stack to push your own ChangeSets (see the pushChangeSet and popChangeSet methods of the TestSupport.Base Python module, see Section 2.3, “TestSupport Python Package”). Make sure to clean up the stack afterward; otherwise, error messages are printed.

The output values could also be input values of the next computation. However, this requires special handling, such as manually resetting the values, to circumvent the ChangeSet concept.

2.1.6. Test Case Suites

Test cases can be grouped into test case suites. This allows running several related tests via the TestCaseManager. Test case suites can be declared in any .def file below the TestCases subdirectory of any package. Each .def file may contain the definition of multiple test case suites and test cases.

The following code shows how to define a test case suite:

TestCaseSuite ExampleTestCaseSuite {
    testCases = "ExampleTest1, ExampleTest2, ..."
}
                    

The available test case suites are listed in the TestCaseManager and can be selected and executed. The result report shows an overview of all executed tests and links to the individual tests.