Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 36 ยท Debugging and Testing

Unit Testing and the VI Analyzer

Test LabVIEW code with unit test VIs, the Unit Test Framework and the VI Analyzer: arrange, act, assert, test-friendly design, simulated instruments, regression tests and static checks.

Debugging finds a bug once. A test finds it every time it returns. LabVIEW has two complementary quality tools: unit tests that run VIs with known inputs and compare outputs, and the VI Analyzer that inspects VIs statically for style and performance problems. Both are available in recent LabVIEW editions (the Unit Test Framework in Professional-level packages, and open-source options like the JKI VI Tester or the Caraya framework from VI Package Manager).

LabVIEW is not available on this site; the lesson describes each tool's usage and demonstrates the testing ideas in Python.

What makes code testable

A test needs three things: a way to give the unit input, a way to read its output, and no hidden dependencies. In LabVIEW terms:

  • Put logic into subVIs with clear connector panes, not into one giant diagram that reads controls directly.
  • Pass data on wires, not through globals and front-panel local variables.
  • Keep hardware access at the edges (a thin driver layer) so the logic can be tested with simulated data. Chapter 5's VISA wrapper idea helps: the same VI can call a real session or a simulation.
  • Make VIs deterministic: pass in timestamps and random seeds rather than reading the clock inside.

Test structure: arrange, act, assert

Every unit test follows the same three steps:

  1. Arrange: prepare inputs and expected values.
  2. Act: call the VI under test.
  3. Assert: compare actual with expected, using tolerance for floating-point numbers.

In the Unit Test Framework (Tools > Unit Test Framework > Create Unit Tests, or right-click a VI in the Project), LabVIEW generates a test VI with one input and one expected output per terminal; you fill in test cases in a table, each with a name, inputs and expected outputs. Run with the Run Tests button; the results list pass, fail and error per case and can be saved as a report. In JKI VI Tester you create a test case class, add a Setup, TearDown and test methods whose names start with test; the failure is raised by Assert VIs with a message.

A test in Python

The code below tests a scaling function the way a unit test would. Note the tolerance and the edge-case test.

Python Unit tests with tolerance (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Testing stateful and I/O code

  • State machines: test each state as its own subVI (input data cluster in, output data cluster and next state out). Test the transitions by checking the next-state value.
  • Instrument code: replace VISA with a simulation (a VI that reads from a table instead of the bus). In LabVIEW you can implement the VISA wrapper as a LabVIEW class or use a Boolean Simulate input to choose real or fake.
  • Error paths: feed an error in and assert the output passes it through; feed bad values and assert the right error code.
  • Regression tests: capture a known-good output (a waveform from a recorded file) and compare it later within tolerance.
Python A simulated instrument for testing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The VI Analyzer

The VI Analyzer (Tools > VI Analyzer > Analyze VIs and Show Results) is a static checker: it loads VIs without running them and reports violations of rules. Rule categories:

Category Examples
Block Diagram Wires under objects, wire bends, backward wires, unused code
Documentation Missing VI description, missing control descriptions, labels not meaningful
Front Panel Overlapping controls, default values not set, clipped text
General Missing error cluster, broken VIs, naming
Performance Arrays built in loops, unnecessary coercion, large copies
SubVIs Connector pane pattern, icon not customised, unassigned terminals

You choose a task: pick VIs or a project, select the rules (or a saved configuration), run, then click each result to jump to the offending item. Save a configuration (.cfg) with the rules your team follows and run it from the LabVIEW command line or a build script so a continuous integration server fails the build when new violations appear. There is also a programmatic API (VI Analyzer Toolkit VIs) to run the analysis from code.

Where tests live in a project

Keep a Tests folder in the project that mirrors the source folders, name test VIs Test_<VIName>.vi, and make running all tests a single click or command. Add a test whenever you fix a bug, and run the suite before each commit.

A test that is not run regularly decays. Tie tests to your build: run unit tests, then the VI Analyzer, then build the executable.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the three steps of a typical unit test?

    Prepare input and expectation, call the code, compare the result.
  2. 2. Why compare floating-point results with a tolerance?

    Small rounding differences are normal, so tests allow a defined margin.
  3. 3. How do you test logic that normally reads a real instrument?

    A swappable I/O layer lets tests feed known data.
  4. 4. What does the VI Analyzer do?

    It inspects VIs without running them and lists rule violations.

Summary

  • Make code testable with subVIs, wires instead of globals and a swappable hardware layer.
  • Unit tests arrange, act and assert, with tolerance for floats and cases for errors.
  • Use the Unit Test Framework or JKI VI Tester; simulate instruments.
  • The VI Analyzer is a static rule checker you can automate in the build.

Next: Building Executables and Installers