Beginner Tutorial Testing Python Automated Tests

Example 1: Writing a simple test case in MeVisLab

Introduction

In this example, you will learn how to write an automated test for a simple network using the DicomImport, MinMaxScan and View3D modules. Afterwards, you will be able to write test cases for any other module and network yourself.

Steps to do

Creating the network to be used for testing

Add the following modules to your workspace and connect them as seen below:

Testcase network

Testcase network

Save your network as NetworkTestCase.mlab.

Test creation

Open the MeVisLab TestCaseManager via menu [ File → Run TestCaseManager ]. The following window will appear.

TestCaseManager window

TestCaseManager window

Change to the Test Creation tab and enter details of your test case as seen below. Make sure to have a package available already.

Select your saved NetworkTestCase.mlab file.

Test Creation window

Test Creation window

Click Create. The MeVisLab text editor MATE will automatically open and display the Python file of your test. Add the below listed code to the Python file.

NetworkTestCase.py

from mevis import *
from TestSupport import Base, Fields, Logging
from TestSupport.Macros import *

filePath="C:/Program Files/MeVisLab3.6.0/Packages/MeVisLab/Resources/DemoData/BrainT1Dicom" 

def OpenFiles():
  ctx.field("DicomImport.inputMode").value = "Directory"
  ctx.field("DicomImport.source").value = filePath
  ctx.field("DicomImport.triggerImport").touch()
  MLAB.processEvents()
  while not ctx.field("DicomImport.ready").value:  
    MLAB.sleep(1)
    Base.ignoreWarningAndError(MLAB.processEvents)
  ctx.field("DicomImport.selectNextItem").touch()
  MLAB.log("Files imported from: "+ctx.field("DicomImport.source").value)
  
def TEST_DicomImport():
  expectedValue=1.0
  OpenFiles() 
  currentValue=ctx.field("DicomImport.progress").value
  ASSERT_FLOAT_EQ(expectedValue,currentValue)

The filePath variable defines the absolute path to the DICOM files that will be given to source field of the DicomImport module in the second step of the OpenFiles function.

The OpenFiles function first defines the DicomImport field inputMode to be a Directory. If you want to open single files, set this field’s value to Files. Then the source field is set to your previously defined filePath. After clicking triggerImport, the DicomImport module needs some time to load all images in the directory and process the DICOM tree. We have to wait until the field ready is TRUE. While the import is not ready yet, we wait for 1 millisecond at a time and check again. MLAB.processEvents() lets MeVisLab continue execution while waiting for the DicomImport to be ready.

When calling the function TEST_DicomImport, an expected value of 1.0 is defined. Then, the DICOM files are opened.

When ready is true, the test touches the selectNextItem trigger, so that the first images of the patient are selected and shown. The source directory will be written on the console as an additional log message for informative purposes.

The value of our DicomImports progress field is saved as the currentValue variable and compared to the expectedValue variable by calling ASSERT_FLOAT_EQ(expectedValue,currentValue) to determine if the DICOM import has finished (currentValue and expectedValue are equal) or not.

Run your test case

Open the TestCase Manager und run your test by selecting your test case and clicking on the Play button in the bottom right corner.

Run Test Case

Run Test Case

After execution, the ReportViewer will open automatically displaying your test’s results.

ReportViewer

ReportViewer

Writing a test for global macro modules

Please observe that field access through Python scripting works differently for global macros. Instead of accessing a field directly by calling their respective module, the module itself needs to be accessed as part of the global macro first.

NetworkTestCase.py

  ...
  # Testing a network file
  ctx.field("DicomImport.inputMode").value = "Directory"
  # Testing a macro module
  ctx.field("<MACRO_MODULE_NAME>.DicomImport.inputMode").value = "Directory"

Imagine unpeeled nuts in a bag as a concept - the field as a nut, their module as their nutshell and the bag as the global macro.

Exercise

Create a global macro module and implement the following test objectives for both (network and macro module):

  • Check, if the file exists.
  • Check, if the max value of file is greater than zero.
  • Check, if the View3D-Input and DicomImport-output have the same data.

Summary

  • MeVisLab provides a TestCenter for writing automated tests in Python.
  • Tests can be executed on networks and macro modules.
  • The test results are shown in a ReportViewer.