Related Content

Contact Information

Viewpoint Systems, Inc.
800 West Metro Park
Rochester, NY 14623
Phone: 585.475.9555
Fax: 585.475.9645

Viewpoint Data Management, LLC.
800 West Metro Park
Rochester, NY 14623
Phone: 585.475.9555
Fax: 585.475.9645

round top

bulletViewpoint News, February 2009


Test Driven Development in LabVIEW: Part 3

by Benjamin Hysell - bhysell@viewpointusa.com

In our last article we explored using TestStand as our test execution framework for doing test driven development (TDD) in LabVIEW. This method however has one major drawback; you need to acquire a copy of TestStand and install it on every machine you are doing development on.

For large corporations who may already have ample licenses to TestStand this may not be an issue, however for the rest of us TestStand can impose an additional development cost to any project.

In this article I will explore JKI’s VI Tester for LabVIEW, a free, standards driven unit testing framework.

JKI’s VI Tester can be found as part of the VI Package Manager (http://jkisoft.com/vipm/ ). VI Tester’s homepage (http://forums.jkisoft.com/index.php?showtopic=985/) provides ample instructions from basic introduction to the framework, to installation, and finally test creation that will get most anyone up and testing VIs in no time. VI Tester also has an active support forum (http://forums.jkisoft.com/index.php?showforum=51 ) where users can ask questions about the product, provide feedback, and suggest improvements for future iterations.

Do take note on the requirements page (http://forums.jkisoft.com/index.php?showtopic=1000 ) VI Tester does require LabVIEW 8.2 or greater, so if you are stuck supporting legacy applications and you still desire to add TDD to your project currently your only option is to use TestStand.

For the remainder of this article I am going to assume you have successfully downloaded and installed VI Tester before continuing, and if you haven’t go ahead and set it up, don’t worry, I’ll wait.

All setup and running? Great, let’s take a look at how we can use VI Tester to do TDD in LabVIEW. For an apples to apples comparison to my TestStand article I am going to use the same problem:

Design a VI that will tell us given three integers if they form a right triangle.

Since we have already done this project using TestStand I am going to leave out all of the theory and choosing the first test steps that I did in part 2. Let’s get right down to business and take the VI we left off with in the last article shown in Figure 1. and Figure 2.

Your browser may not support display of this image.

Figure 1 Front Panel Right Triangle Checker

Your browser may not support display of this image.

Figure 2 Block Diagram Right Triangle Checker

I’ve taken my ‘Right Triangle Checker’ and added it to a new project called aptly ‘Triangle Checker’. Time to start adding some tests! In the project explorer I am going to select ‘Tools/VI Tester/New/Test Case…’. This will bring up a file save dialog box asking me where I want to save my new test case and what I want to call it. For this project I’m going to call my test case ‘Tests’ and save the test case in the same directory as the code I am currently developing. The resulting project explorer can be seen in Figure 3.

Your browser may not support display of this image.

Figure 3 Test Case Added to Project Explorer

VI Tester has added a new class to our project where we are going to place all of our test code. Notice the three Vis VI Tester created:

setUp.vi – All code that needs to be run before each test VI executes should reside here

tearDown.vi – All code used to clean up after each test VI is placed here

testExample.vi – A template VI that provides a starting point in creating tests

We are not going to dive into the set up and tear down VIs this article, refer to the support forms for further information on how you can use them in your testing, (http://forums.jkisoft.com/index.php?showtopic=978 )

To create our first test we are going to take a look at the testExample.vi as seen in Figures 4 and 5.

Your browser may not support display of this image.

Figure 4 Front Panel Test VI


Your browser may not support display of this image.

Figure 5 Block Diagram Test VI

Figure 5 has a new VI that is provided by VI Tester that will test if two items provided to it are equal. For a complete rundown of all of the VIs provided by VI Tester check out the videos on VI Tester’s website towards the bottom of the page (http://forums.jkisoft.com/index.php?showtopic=979&st=0 )

Our first test is going to be:

  • Test for negative values for the sides of a triangle returns ‘false’

To do this we need to create new test VIs for the test case. The easiest method is to create a copy of the current template VI and add our VI we want to test. This is described in detail on VI Tester’s website at the top of the page (http://forums.jkisoft.com/index.php?showtopic=979&st=0)

The short version:

  • Double click on testExample.vi
  • File/Save As/Select ‘Open Additional Copy’
  • Check ‘Add copy to Tests.lvclass’, hit ‘Continue’
  • Save the new VI as ‘test for negative values.vi
  • NOTE: the word ‘test’ must be the first word in the VI’s name, without it the testing framework cannot tell it is a test VI.

    Now that we have a new test VI we are going to modify it to test our ‘Right Triangle Checker’ for negative input.

    • Place the ‘Right Triangle Checker.vi’ on the block diagram for ‘Test for negative values.vi
    • Wire in three values for a, b, and c…make sure one of them is negative
    • Right click on the ‘passIfEqual.vi’ and replace it with ‘failIf.vi’.
    • Wire in the result from ‘Right Triangle Checker.vi’ into the Boolean input for ‘failIf.vi’
    • Save and close.

We just created our first test case using VI Tester!

If you followed all of the steps I outlined above you should have a block diagram like Figure 6.

Your browser may not support display of this image.

Figure 6 Check for Negative Input Test VI

VI Tester excels over a similar TestStand implementation in several aspects:

  • VI Tester provides a very nice framework to quickly create test VIs given the starting template.
  • Tests are added to the project and thus live with the developed code.
  • We don’t need multiple applications opened to perform testing, we never have to leave LabVIEW to do TDD
  • Out of the box VI Tester has VIs defined to hook into the testing framework to validate and report results to the test runner. In TestStand we are left to roll our own set of VIs to perform the same tasks and organizing the code within the project and a nice set of VIs to help manage the testing process, unlike TestStand where we are left to roll a lot of our own code.
  • VI Tester is free!

Where VI Tester really takes off is when we start actually running the test, let’s run our first test now.

Navigate back to the project explorer. Select from the menu bar ‘Tools/VI Tester/Test VIs…’, this will bring up the test runner as shown in Figure 7.

Your browser may not support display of this image.

Figure 7 VI Tester Test Runner

A full run down of the features/description of the functions can be found at http://forums.jkisoft.com/index.php?showtopic=974.

For our purposes we are going to focus on the green ‘Play’ button at the top of the runner, this will kick off running all of the tests.

After the run we have immediate feedback on the status of each test as seen in Figure 8.

Your browser may not support display of this image.

Figure 8 Results of Running our Tests

We can see that we ran two tests (one real test, the other example test we haven’t removed yet). Let’s add three more tests:

  • A valid triangle
  • A invalid triangle
  • 0 as one of the sides of the triangle
  • A valid triangle with sides inputted out of order

I am leaving the process of creating each one of those tests up to the reader. However if you are following along with the same block diagram as I have for my ‘Right Triangle Checker.vi’ by the time you implement and run the last test you are going to have Figure 9.

Your browser may not support display of this image.

Figure 9 Test Failure

If you take a look back at Figure 2 the block diagram for our VI under test you’ll see that if we provide the sides in the order of 5, 4, 3 our VI will fail even though 5, 4, 3 are valid sides of a right triangle. We can quickly see from Figure 9 which one of our tests has failed and a big red bar at the top of the screen to bring to our attention one or more of our tests have failed.

Once we refactor our VI under test, we can quickly re-run all of our tests to validate that any changes to the VI don’t cause errors in any of our other tests. This is the key to TDD: having a full set of regression tests that we can run quickly and ensure any changes we make to our code at any time can be fully tested against a suite of tests and validate that every change we make to the code still produces functioning code.