Example 3: Iterative Tests in MeVisLab
Introduction
In this example, you are writing an iterative test. Iterative test functions run a function for every specified input. They return a tuple consisting of the inputs iterated over and the function object called. The iterative test functions are useful if the same function should be applied to different input data. These could be input values, names of input images, etc.
Steps to Do
Creating the Network to be Used for Testing
Add a LocalImage and a DicomTagViewer module to your workspace and connect them.
Test Case Creation
Open the panel of the DicomTagViewer and set
Save the network.
Start the TestCaseManager and create a new test case called IterativeTestCase as seen in Example 1: Writing a Simple Ttest Case in MeVisLab.
Defining the Test Data
In the TestCaseManager, open the test case Python file via
Add a list for test data to be used as input and a prefix for the path of the test data as seen below.
IterativeTestCase.py
from mevis import *
from TestSupport import Base, Fields, ScreenShot, Logging
from TestSupport.Macros import *
patientPathPrefix = "$(DemoDataPath)/BrainMultiModal/"
testData = { "ProbandT1":("ProbandT1.dcm", "439.9624938965"),
"ProbandT2":("ProbandT2.dcm", "234.91")}
The above list contains an identifier for the test case (ProbandT1/2), the file names, and a number value. The number value is the value of the DICOM tag (0028,1050) WindowCenter for each file.
Create Your Iterative Test Function
Add the Python function to your .script file:
IterativeTestCase.py
def ITERATIVETEST_TestWindowCenter():
return testData, testPatient
This function defines that
IterativeTestCase.py
def testPatient(path, windowCenter):
ctx.field("LocalImage.name").value = patientPathPrefix + path
tree = ctx.field("LocalImage.outImage").getDicomTree()
importValue = str(tree.getTag("WindowCenter").value())
dicomValue = str(ctx.field("DicomTagViewer.tagValue0").value)
ASSERT_EQ(windowCenter, importValue, "Checking expected WindowCenter value against DICOM tree value.")
ASSERT_EQ(windowCenter, dicomValue, "Checking expected WindowCenter value against DicomTagViewer value.")
- Initially, the path and filename for the module
LocalImageare set. The data is loaded automatically, because the module has theAutoLoad flag enabled by default. - Then, the DICOM tree of the loaded file is used to get the WindowCenter value (
importValue ). - The previously defined value of the
DicomTagVieweris set asdicomValue . - The final test functions ASSERT_EQ evaluate if the given values are equal.
You can use many other ASSERT* possibilities, just try using the MATE autocompletion and play around with them. ASSERT* functions throw an exception in the case expected and actul values do not fit. Your test execution stops in this case.
You can also use EXPECT* functions. They return true or false and you can decide yourself how your test continues.
For details, see TestCenter Reference
Run Your Iterative Test
Open the TestCaseManager and select your package and test case. You will see two test functions on the right side.
The identifiers of your test functions are shown as defined in the list (ProbandT1/2). The TestWindowCenter now runs for each entry in the list and calls the function
Adding Screenshots to Your TestReport
Now, extend your network by adding a View2D module and connect it with the LocalImage module. Add the following lines to the end of your function
IterativeTestCase.py
def testPatient(path, windowCenter):
...
Fields.setValue("View2D.startSlice", 0)
result = ScreenShot.createOffscreenScreenShot("View2D.self", "screentest.png")
Logging.showImage("My screenshot", result)
Logging.showFile("Link to screenshot file", result)
Your ReportViewer now shows a screenshot of the image in the View2D.
Summary
- Iterative tests allow you to run the same test function on multiple input entries.
- It is possible to add screenshots to test cases.








