Unit Testing in LabVIEW

Adam Metcalf – Control Software Solutions


As software is developed there is a need to ensure that the building blocks (units) of an application perform as expected and that bugs are not introduced. Where bugs are introduced it is necessary to detect them as early as possible. At Control Software Solutions, we achieve this by unit testing.

What is Unit Testing?

Unit testing is a process where the smallest testable parts of an application are tested to ensure that they perform as expected.

To successfully implement unit testing it is important that you create code that is broken down into testable units. The more modular your code, the easier it will be to test as units.

Consider the example shown in figure 1. Although this could be tested, since multiple calculations are performed in the same subVI it would be difficult to located any bugs:

Figure 1

This can be very quickly turned into 2 subVIs which are very easy to test, as shown by figure 2:

Figure 2

Benefits of unit testing

There are many obvious benefits to unit testing; for example, it allows you to find problems early and reduces the chance of bugs being introduced in your code. However, there are other benefits which might be overlooked:

  1. A programmer who writes unit tests is likely to create VIs that are inherently testable, and a VI that is testable is more likely to be readable and loosely coupled.
  2. Once unit tests have been written, regression testing becomes very easy. This means that if you make changes to some code, running your unit tests checks everything still works as expected, and if not highlights where bugs have been introduced.
  3. Unit tests can also function as code documentation, providing other developers with examples of the intended use of your VIs (You can think of them as little examples).
  4. Unit testing allows you to demonstrate requirement coverage, which is especially important if you are working in a regulated industry.

We have created a selection of brief tutorial videos. The first provides an introduction to unit testing

What is the Unit Test Framework?

The Unit Test Framework is a toolkit for LabVIEW that provides a way of writing, performing and automating unit tests.

Figure 3

Watch our YouTube video: How to Create a Simple LabVIEW Unit Test

User Defined Tests with the LabVIEW Unit Test Framework.

Whilst there are different ways to write unit tests using the Unit Test Framework, at Control Software Solutions we prefer to use User Defined Tests. Using User Defined Tests allows us to write test cases using VIs rather than through the properties of the unit test itself. This offers several advantages:

  1. Easy and intuitive integration of any setup and teardown conditions which the VI under test requires (for example, setting up a FGV, opening a file reference etc).
  2. The tests are much easier to read for a developer who is already familiar with LabVIEW.
  3. The tests can be much more complex (including parallel testing with multiple test conditions).
  4. Provides an example of how the VI under test is intended to be used.

The process for creating a User Defined Test is described in or YouTube video How to Create User Defined Tests

Test-Driven Development.

At Control Software Solutions, we practice Test-Driven Development. This involves writing a test to satisfy the requirements of a VI before you write any code inside the VI. The first time you run the test, it should fail (it doesn’t do anything yet!). You then write enough code inside the VI to pass the test. This means that you only write enough code to meet the requirements, which can be a significant time-saver! The process is shown by figure 4:


Figure 4

Since you can re-run all the unit tests in the project any changes in your code (including when you refactor existing code) should not introduce bugs.

Obviously, there is some overhead to writing, running and maintaining all these unit tests, and initially it can seem slightly more time consuming, however we have found it that saves a great deal of time that may otherwise be spent chasing bugs.

CSS Unit Test Framework Helper

It’s no secret that creating User Defined Tests can be time consuming. This is especially true once you start adding layers of complexity, for example testing multiple test cases in parallel as shown in figure 5:

Figure 5

The problem with the process shown in figure 4 is that you will simply get a ‘pass’ or a ‘fail’. If the test fails, you will get no further information (for example, which specific test case failed?). You can use normal debugging techniques to find out, but this is time consuming and really limits the automation that we are trying to achieve by unit testing.

At Control Software Solutions, we have made a toolkit which eases this process, shown by figure 6:

Figure 6

This allows very complex test cases to be very quickly built up using a selection of helper VIs.

The CSS Unit Test Helper also allows comprehensive reporting. If an individual component of a test fails, the whole test fails. However, the HTML report produced will quickly allow you to identify which particular component failed, as shown by figure 7:

Figure 7

Using the CSS Unit Test Helper significantly decreases the programming time required for creating user defined tests whilst increasing the information obtained from reporting.

Further Information

For more information on any of the topics you have read in this article, or for more information on LabVIEW programming contact us:


Leave a Reply

Your email address will not be published. Required fields are marked *