Advanced Tutorial Testing Python Automated Tests Iterative Test Screenshot

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 function object called and the inputs iterated over. 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.

Example Network

Example Network

Test case creation

Open the panel of the DicomTagViewer and set Tag Name to WindowCenter. The value of the DICOM tag from the current input image is automatically set as value.

Save the network.

Start MeVisLab TestCaseManager and create a new test case called IterativeTestCase as seen in Example 1: Writing a simple testcase in MeVisLab.

DicomTagViewer

DicomTagViewer

Defining the test data

In TestCaseManager open the test case Python file via Edit File.

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 testPatient shall be called for each entry available in the defined list testData. Define the function testPatient:

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.")

  1. Initially, the path and filename for the module LocalImage are set. The data is loaded automatically, because the module has the AutoLoad flag enabled by default.

    LocalImage

    LocalImage

  2. Then, the DICOM tree of the loaded file is used to get the WindowCenter value (importValue).
  3. The previously defined value of the DicomTagViewer is set as dicomValue.
  4. The final test functions ASSERT_EQ evaluate if the given values are equal.

Run your iterative test

Open MeVisLab TestCase Manager and select your package and test case. You will see 2 test functions on the right side.

Iterative Test

Iterative Test

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 testPatient for each entry using the given values.

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 testPatient:

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.

Screenshot in ReportViewer

Screenshot in ReportViewer

Summary

  • Iterative tests allow you to run the same test function on multiple input entries.
  • It is possible to add screenshots to test cases