Skip to content

Latest commit

 

History

History
121 lines (92 loc) · 5.55 KB

File metadata and controls

121 lines (92 loc) · 5.55 KB
title Example 3: Iterative Tests in MeVisLab With Screenshots
date 2022-06-15 08:56:33 +0200
status OK
draft false
weight 765
tags
Advanced
Tutorial
Testing
Python
Automated Tests
Iterative Test
Screenshot
menu
main
identifier title weight parent
testingexample3
Writing an Iterative Test in MeVisLab
795
testing

Example 3: Iterative Tests in MeVisLab

{{<youtube "1JidUyfz0xU">}}

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

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

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.

{{< highlight filename="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: {{< highlight filename="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: {{< highlight filename="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
  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.

{{}} You can use many other ASSERT* possibilities, just try using the MATE autocompletion and play around with them. ASSERT* functions throw an exception in 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 ho your test continues.

For details, see {{< docuLinks "/Resources/Documentation/Publish/SDK/TestCenterReference/namespaceTestSupport_1_1Macros.html" "TestCenter Reference" >}} {{}}

Run Your Iterative Test

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

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: {{< highlight filename="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

Summary

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