Calling Scripting Languages from NI TestStand

Publish Date: Aug 01, 2018 | 10 Ratings | 3.30 out of 5 | Print | 2 Customer Reviews | Submit your review

Overview

The ability to re-use existing code while migrating to a new test software framework can save developers both time and money.  Both NI TestStand test management software and NI LabVIEW give users the ability to directly call and integrate with code written in a variety of languages such as Perl, Python and Tcl.

Python is supported in TestStand through a Python Step Types for TestStand add-on available in versions 2014-2017. These steps offer an integrated configuration-based experience for integrating Python code modules into your sequences.

Alternatively, TestStand has the ability to call your scripts written in languages such as Perl, Python and Tcl using the Call Executable step type.  In particular, TestStand can pass data in and out of scripts as well as access the script’s error information.

  • Passing Data to Scripts:
    Developers can pass data from TestStand to their scripts either through command line arguments or directly through the Standard Input stream. Using the Standard Input stream allows developers to reuse scripts that traditionally took user input from the keyboard during run-time, and run it in an automated fashion from TestStand.
  • Accessing Output in TestStand:
    Once the execution of the script is complete, anything that the script wrote to the Standard Output can be accessed from the TestStand sequence. This will enable developers to base the status of a step on the result of a script.
  • Accessing Script Errors in TestStand:
    If an error occurred during execution of the script, the error information is also available to TestStand through the Standard Error stream. This enables developers to handle the error appropriately.

What this means is that developers can effectively utilize the full power of their scripts by taking advantage of the hooks that TestStand provides to the executable’s Standard Input (stdin), Standard Output (stdout) and Standard Error (stderr).

This article is Part 2 of a three-part series on calling scripting languages from TestStand with a focus on the needs of engineers and scientists.

Table of Contents

  1. Python Step Types for TestStand
  2. Calling a Simple Script from TestStand 
  3. Accessing Script Outputs from TestStand: Standard Output
  4. Passing Data to Scripts from TestStand: Command Line
  5. Passing Data to Scripts from TestStand: Standard Input
  6. Evaluating Script Results: Pass/Fail Test using Status Expression
  7. Evaluating Script Results: Numeric Limit Test using <None> Adaptor
  8. Handling Script Errors in TestStand
  9. Next Steps

1. Python Step Types for TestStand

If you are looking to call Python scripts from TestStand, National Instruments now offers Python Step Types for TestStand. These step types are supported for use in TestStand 2014-2017, earlier versions of TestStand can continue to call Python scripts from the Call Executable step as described starting in Chapter 2 of this article.

The step types allow Python scripts to be incorporated into new or existing test systems. The step types are built on the same design principles as other custom step types, allowing them to integrate into existing versions of TestStand. After installing the TestStand Python Step Types you will see a new set of steps offering the familiar String Value Test, Pass/Fail Test, Numeric and Multi-Numeric Limit Test Steps for use with calling your Python scripts.

Supported Features:

  • Execute Python scripts on disk by performing the following:
    • Call a function defined in a module
    • Get/Set the attributes defined in a module
    • Create a class instance
    • Call a member function or static function defined in a class
    • Get/Set member attributes or static attributes defined in the class
  •  Execute Python scripts in Python versions 2.7 or 3.6
  •  Execute Python scripts out-of-process in the CPython Interpreter
  •  Convert data between Python and TestStand variables
  •  Store and reuse Python objects in a TestStand object reference variable

You can also use multiple Python interpreter session to perform:

  • Executing Python scripts in parallel
  • Use Python versions 2.7 and 3.6 simultaneously in TestStand

These steps provide a more familiar way to interface with Python scripts compared to the Call Executable step. When dropping a step you will be presented with a configuration dialog that allows you to control interpreter options, function selection, and data passing. As a result you will not need to do the work to add command-line arguments and parse standard output to pass data between TestStand and Python.

Download the Python Step Types for TestStand to learn more and try out examples of this method. 

If you are using other scripting languages, or want to interface with Python through the command-line with the Call Executable Step, continue reading below.

Back to Top

2. Calling a Simple Script from TestStand 

Getting Started

If you are new to scripting or don’t have an interpreter installed, we recommend reading Part 1 of this series:
Introduction to Scripting

Also, if you are looking to call scripts from LabVIEW, instead of NI TestStand, you can use the System Exec VI. For a tutorial, refer to:
Tutorial: Call Perl and Python Scripts from LabVIEW

In order to call a script from TestStand you will have to have the following:

  • A script to call (we will use examples in Perl, Python and Tcl)
  • An interpreter for the scripting language you choose
  • The NI TestStand Sequence Editor (or an Operator Interface that allows you to modify sequences)

If you are currently part of the National Instruments Standard Service Program, you can also access the interactive training module on calling scripts from TestStand here:
On Demand Training: TestStand Modules

The Script: HelloWorld

The script used in this example is a simple HelloWorld script that will simply write “Hello World!” to the Standard Output stream.
Note: You can download any of the script files by clicking on their filename above the code snippets.

Perl: HelloWorld.pl
#!/usr/bin/perl -w
use strict;

print "Hello World!\n";

 

Python: HelloWorld.py
#!/usr/bin/env python

print "Hello World!"

 

Tcl: HelloWorld.tcl
#!/bin/sh
# -*- tcl -*-

# The next line is executed by /bin/sh, but not tcl \
exec tclsh "$0" ${1+"$@"}

puts "Hello World!";

Calling the HelloWorld Script from TestStand

The process of calling a script from TestStand is very similar to calling it from the Windows command line. In fact, all you have to do is use the Call Executable step and specify the path and file for the script you want to execute. (The reason this works is because we associated the script file extension with the appropriate interpreter. If you haven’t associated the script file with a particular interpreter, you will have to specify the interpreter as the executable and pass in the script file as a command line argument).

  1. Open the NI TestStand Sequence Editor and a new sequence file.
  2. Add a Call Executable step and rename it “Call HelloWorld Script”.
  3. On the Step Settings pane, switch to the Call Settings tab.
  4. Specify the name of the script you want to call in the File PathName
    Perl: HelloWorld.pl
    Python: HelloWorld.py
    Tcl: HelloWorld.tcl
  5. Save the sequence file as CallingHelloWorld.seq in the same folder as the script.
  6. Run your sequence file. Your report should say that the test passed and that the Call Executable script had an ExitCode of 0, which indicates success.

Note: If you haven’t associated the script file’s extension with the appropriate interpreter, you will have to call the interpreter directly from the Call Executable step and pass in the script as a command line argument. See the following screenshot for an example:

 

Back to Top

3. Accessing Script Outputs from TestStand: Standard Output

Most programming languages define a standard output stream that programs can write their output to. While this standard output stream (stdout) defaults to the terminal (monitor), NI TestStand gives you the ability to access the standard output stream from the Call Executable Step type.

In the following example, you will learn how to use the previous Hello World example and modify it so that you can write the output of the script to your TestStand report.

Accessing the Standard Output Stream from TestStand

We will use the same HelloWorld script and continue from the same sequence CallingHelloWorld sequence that you created in the previous section, Calling a Simple Script from TestStand.

  1. Open the sequence file CallingHelloWorld.seq.
  2. Highlight the “Call HelloWorld Script” step.
  3. On the Step Setting pane, switch to the Standard Output/Error tab.
  4. On the Output Destination combo box, pick Store in Variable/Property.
    Leave the default value, i.e., Step.StdOutput.Text.
  5. On the Step Settings pane, switch to the Properties tab and select the Additional Results category.
  6. Click the Add Result from List button and select Standard Output. This will include the stdout stream in the report.
  7. Save the sequence file as CallingHelloWorldWithStdOut.seq in the same folder as the script.
  8. Run your sequence file. Your report should look like the following screenshot:

Back to Top

4. Passing Data to Scripts from TestStand: Command Line

Most programming languages allow you to pass data to them when you launch them through command line arguments – extra parameters that you type in after the name of the script on the command line. TestStand’s Call Executable step allows you to pass parameters to your scripts in this method.

The Script: AddNumbersCommandLine

This is a fairly basic script that will accept a series of numbers as command line arguments and writes their sum to the standard output stream.

Perl: AddNumbersCommandLine.pl
#!/usr/bin/perl –w
use strict;

#Command Line Arguements are stored in list @ARGV
my $numArgs = $#ARGV + 1;
my $sum = 0;

#Iterate through each element and add to the sum
foreach my $currentNumber (@ARGV)
{
    $sum += $currentNumber;
}

print "Sum: ", $sum, "\n";

 

Python: AddNumbersCommandLine.py
#!/usr/bin/env python
import sys

#Command Line Arguements are stored in list argv
numArgs = len(sys.argv) - 1

sum = 0

#Iterate through each element and add to the sum
for n in range (1, len(sys.argv)):
    sum = sum + int(sys.argv[n])

print "Sum:", sum, "\n"

 

Tcl: AddNumbersCommandLine.tcl
#!/bin/sh
# -*- tcl -*-
# The next line is executed by /bin/sh, but not tcl \
exec tclsh "$0" ${1+"$@"}

#Command Line Arguements are stored in list argv
set numArgs $argc
set sum 0

#Iterate through each element and add to the sum
foreach currentNumber $argv {
    set sum [expr $sum + $currentNumber]
}

puts "Sum: $sum\n"

 

Passing Command Line Arguments from TestStand

All you need to do in order to pass Command Line Arguments from TestStand to a script is to include the parameters you want to pass in the Argument Expression field of the Call Executable step.

  1. Create a new sequence file.
  2. Add a Call Executable step and name it “Call AddNumbersCommandLine Script”.
  3. On the Step Settings pane, switch to the Call Settings tab.
  4. Point the File Pathname field to the appropriate AddNumbersCommandLine script.
  5. For the Argument Expression field, enter in a series of numbers enclosed in double-quotes. For example:
    “2 5 8”
  6. Switch to the Standard Output tab and store the output in the Variable/Property Step.StdOutput.Text.
  7. On the Step Settings pane, switch to the Properties tab and select the Additional Results category.
  8. Click the Add Result from List button and select Arguments. This will include the command line arguments in the report.
  9. Click the Add Result from List button and select Standard Output. This will include the stdout stream in the report.
  10. Save the sequence file as PassingDataToScriptsCommandLine.seq in the same folder as the script.
  11. Run your sequence file. Your report should look like the following screenshot:

Back to Top

5. Passing Data to Scripts from TestStand: Standard Input

Most programming languages also define a standard user input stream that programs can read from during execution. While this standard input stream (stdin) defaults to the keyboard, NI TestStand gives you the ability to access the standard input stream from your sequence. This gives developers more flexibility as your scripts can read these values during run-time rather than right when they begin execution.

The Script: AddNumbersStdIn

Perl: AddNumbersStdIn.pl
#!/usr/bin/perl -w
use strict;

print "Enter numbers (separate with commas): ";

#Get input as a string
my $numbersAsString = <STDIN>;
#Parse the numbers into a list
my @numbers = split(/, | |,/,$numbersAsString);

my $sum = 0;

#Iterate through the list and add to the sum
foreach my $currentNumber (@numbers)
{
    $sum += $currentNumber;
}

print "Sum: ", $sum, "\n";

 

Python: AddNumbersStdIn.py
#!/usr/bin/env python

#Get input as a list
numbers = raw_input('Enter numbers (separate with commas): ')

sum = 0

#Iterate through the list and add to the sum
for currentNumber in numbers:
    sum = sum + currentNumber

print "Sum:", sum, "\n"

 

Tcl: AddNumbersStdIn.tcl
#!/bin/sh
# -*- tcl -*-
# The next line is executed by /bin/sh, but not tcl \
exec tclsh "$0" ${1+"$@"}

puts "Enter numbers (separate with commas): "
#Get input as a string
gets stdin numbersAsString
#Parse the numbers into a list
set numbers [split $numbersAsString ,]

set sum 0

#Iterate through the list and add to the sum
foreach currentNumber $numbers {
    set sum [expr $sum + $currentNumber]
}

puts "Sum: $sum\n"

 

Passing Data through the Standard Input from TestStand

All you need to do in order access the standard input stream from TestStand is to enable an Input Method on the Standard Input tab of the Call Executable step.

  1. Create a new sequence file.
  2. Add a Call Executable step and name it “Call AddNumbersStdIn Script”.
  3. On the Step Settings pane, switch to the Call Settings tab.
  4. Point the File Pathname field to the appropriate AddNumbersStdIn script.
  5. On the Step Settings pane, switch to the Standard Input tab.
  6. Switch the Input Method combo box to String, and enter in a series of numbers separated by commas. For example:
    2, 5, 8
  7. Switch to the Standard Output tab and store the output in the Variable/Property Step.StdOutput.Text.
  8. On the Step Settings pane, switch to the Properties tab and select the Additional Results category.
  9. Click the Add Result from List button and select Standard Input. This will include the stdin stream in the report.
  10. Click the Add Result from List button and select Standard Output. This will include the stdout stream in the report.
  11. Save the sequence file as PassingDataToScriptsStdIn.seq in the same folder as the script.
  12. Run your sequence file. Your report should look like the following screenshot:

 

Back to Top

6. Evaluating Script Results: Pass/Fail Test using Status Expression

The earlier topics discussed how to get the output of a script into TestStand using the Standard Output stream. Once you have this data in your sequence, NI TestStand gives you the ability to base the result of your sequence based on this output. One way to do this is to modify the Status Expression of the Call Executable step.

The Script: IsVoltageInRange

This simple script takes in a range as an input (minimum and maximum) through the standard input stream. It then compares a simulated voltage which is hardcoded to 5V to the range and prints “Result: True” or “Result: False” to the standard output.

Perl: IsVoltageInRange.pl
#!/usr/bin/perl -w
use strict;

#Simulated Voltage
my $voltage = 5;

#Get Range
print "Enter range (separate with commas): ";
my $numbersAsString = <STDIN>;
my @numbers = split(/, | |,/,$numbersAsString);

my $min = $numbers[0];
my $max = $numbers[1];

if ($voltage >= $min && $voltage <= $max)
{
    print "Result: True\n"
}
else
{
    print "Result: False\n"
}

 

Python: IsVoltageInRange.py
#!/usr/bin/env python

#Simulated Voltage
voltage = 5

#Get Range
numbers = input('Enter range (separate with commas): ')

min = numbers[0]
max = numbers[1]

if voltage > min and voltage <= max:
    print "Result: True\n"
else:
    print "Result: False\n"

 

Tcl: IsVoltageInRange.tcl
#!/bin/sh
# -*- tcl -*-
# The next line is executed by /bin/sh, but not tcl \
exec tclsh "$0" ${1+"$@"}

#Simulated Voltage
set voltage 5

#Get Range
puts "Enter range (separate with commas): "
gets stdin numbersAsString
set numbers [split $numbersAsString ,]

set min [lindex $numbers 0]
set max [lindex $numbers 1]

if {$voltage >= $min && $voltage <= $max} {
    puts "Result: True\n"
} else {
    puts "Result: False\n"
}

 

Evaluating Output in TestStand using Status Expression

Since you know that the script is going to return either “Result: True” or “Result: False”, you can use a status expression to parse for this string in the standard output stream and set the step status based on the output.

  1. Create a new sequence file.
  2. Add a Call Executable step and name it “Pass/Fail Test: IsVoltageInRange (Pass)”.
  3. On the Step Settings pane, switch to the Call Settings tab.
  4. Point the File Pathname field to the appropriate IsVoltageInRange script.
  5. On the Step Settings pane, switch to the Standard Input tab.
  6. Switch the Input Method combo box to String, and enter in a range that will pass separated by commas. For example:
    2, 8
  7. Switch to the Standard Output tab and store the output in the Variable/Property Step.StdOutput.Text.
  8. Create a Boolean local variable named Locals.isVoltageInRage.
  9. On the Step Settings pane, switch to the Properties tab and select the Expressions category.
  10. Enter the following Post-Expression to parse the standard output:
    Locals.isVoltageInRange =
       (Find(Step.StdOutput.Text, "Result: True") > 0) ? True : False
  11. Enter the following Status-Expression to set the result of the step:
    Step.Result.Status = Locals.isVoltageInRange ? "Passed" : "Failed"
  12. On the Additional Results category, enable  Standard Input and Standard Output using the Add Result From List button. This will include the stdin and stdout stream in the report.
  13. Click the Add Custom Result button and enter in “isVoltageInRange” as the Name and “Locals.isVoltageInRange” as the Value to Log. Make sure the Type reads Boolean.
  14. Now create a copy of the step and insert it right after the original step. Name the copy “Pass/Fail Test: IsVoltageInRange (Fail)”.
    Modify the Standard Input in this step to cause a fail condition. For example:
    3, 4
  15. Save the sequence file as PassFailTestStatusExpression.seq in the same folder as the script.
  16. Run your sequence file. Your report should look like the following screenshot:

 

Back to Top

7. Evaluating Script Results: Numeric Limit Test using <None> Adaptor

The earlier topics discussed how to get the output of a script into TestStand using the Standard Output stream. Once you have this data in your sequence, NI TestStand gives you the ability to base the result of your sequence based on this output. One way to do this is to use Test Step with a <None> Adaptor after the script to evaluate the script’s output.

The Script: GetVoltage

This script simply creates simulated voltage reading which is hardcoded to 5V to the range and prints “Voltage: <voltage>” to the standard output.

Perl: GetVoltage.pl
#!/usr/bin/perl -w
use strict;

#Simulated Voltage
my $voltage = 5;

print "Voltage: " , $voltage, "\n"

 

Python: GetVoltage.py
#!/usr/bin/env python

#Simulated Voltage
voltage = 5

print "Voltage:", voltage, "\n"

 

Tcl: GetVoltage.tcl
#!/bin/sh
# -*- tcl -*-
# The next line is executed by /bin/sh, but not tcl \
exec tclsh "$0" ${1+"$@"}

#Simulated voltage
set voltage 5

puts "Voltage: $voltage\n"

 

Evaluating Output in TestStand using <None> Adaptor

Since you know that your script is going to return either the voltage in the format “Voltage: <voltage>, you can use a status expression to parse for this string in the standard output stream and set a local variable to this value. Then, you can use a Numeric Limit Test with a <None> Adaptor to test the output.

  1. Create a new sequence file.
  2. Add a Call Executable step and name it “GetVoltage”.
  3. On the Step Settings pane, switch to the Call Settings tab.
  4. Point the File Pathname field to the appropriate GetVoltage script.
  5. On the Step Settings pane, switch to the Standard Input tab.
  6. Switch to the Standard Output tab and store the output in the Variable/Property Step.StdOutput.Text.
  7. Create a numeric local variable named Locals.voltage.
  8. On the Step Settings pane, switch to the Properties tab and select the Expressions category.
  9. Enter the following Post-Expression to parse the standard output:
    Locals.voltage =
    Val(
         Mid(
               Step.StdOutput.Text,
               Find(Step.StdOutput.Text, "Voltage: ") + Len("Voltage: "),
               Find(Step.StdOutput.Text, "\n", Find(Step.StdOutput.Text, "Voltage: "))
                  - (Find(Step.StdOutput.Text, "Voltage: ")) + Len("Voltage")
               )
         )
  10. On the Additional Results category, enable  Standard Output using the Add Result From List button. This will include the stdout stream in the report.
  11. Click the Add Custom Result button and enter in “Voltage” as the Name and Locals.voltage as the Value to Log. Make sure the Type reads Number.
  12. Now pick <None> from the Adaptor dropdown list and a Numeric Limit Test step. Name the step “Numeric Limit Test: IsVoltageInRange (Pass)”.
  13. Set the Data Source for this step to Locals.voltage.
  14. Set the Limits of the step to a range that covers 5 so that the test will pass. For example:
    Low: 3
    High: 8
  15. Now create a copy of the two steps and insert them right after the original steps. Name the copy of the Numeric Limit Test “Numeric Limit Test: IsVoltageInRange (Fail)”.
    Modify the Limits on this step to cause a fail condition. For example:
    Low: 3
    High: 4
  16. Save the sequence file as NumericLimitTestNoneAdaptor.seq in the same folder as the script.
  17. Run your sequence file. Your report should look like the following screenshot:

 

Back to Top

8. Handling Script Errors in TestStand

Finally, NI TestStand also gives you access to the Standard Error (stderr) stream. Anytime an error occurs in your Perl, Python or Tcl scripts, they will write out to this stream. Using this, you can handle any errors eloquently in your TestStand sequence.

The Script: AddNumbersWithErrorHandling

This is a modified version of our AddNumbersStdIn script that will check to make sure that all the inputs were numeric values; otherwise it will throw an error, write to the stderr stream and exit with ExitCode -1.

Perl: AddNumbersWithErrorHandling.pl
#!/usr/bin/perl -w
use strict;

print "Enter numbers (separate with commas): ";
my $numbersAsString = <STDIN>;
my @numbers = split(/, | |,/,$numbersAsString);

my $sum = 0;
foreach my $currentNumber (@numbers)
{
    die("Input is not a number!, stopped") unless ($currentNumber =~ /^\d+$/);
    $sum += $currentNumber ;
}

print "Sum: ", $sum, "\n";

 

Python: AddNumbersWithErrorHandling.py
#!/usr/bin/env python
import sys

try:
    numbers = input('Enter numbers (separate with commas): ')
except:
    print >> sys.stderr, 'Input is not a number!'
    sys.exit(-1)

sum = 0
for currentNumber in numbers:
    sum = sum + currentNumber

print "Sum:", sum, "\n"

 

Tcl: AddNumbersWithErrorHandling.tcl
#!/bin/sh
# -*- tcl -*-
# The next line is executed by /bin/sh, but not tcl \
exec tclsh "$0" ${1+"$@"}

puts "Enter numbers (separate with commas): "
gets stdin numbersAsString

set numbers [split $numbersAsString ,]

set sum 0
foreach currentNumber $numbers {
    if ![string is integer -strict $currentNumber] {
        #error "Input is not a number!"
        puts stderr "Input is not a number!"
        return -1
    }
    set sum [expr $sum + $currentNumber]
}

puts "Sum: $sum\n"

 

Handling Script Errors in TestStand

TestStand makes it easy not only to access the Standard Error stream in our sequence, but also makes it easy to make decisions based on the contents of this stream and eloquently handle any errors.

  1. Create a new sequence file.
  2. Add a Call Executable step and name it “Call AddNumbers Script (Correct Input)”.
  3. On the Step Settings pane, switch to the Call Settings tab.
  4. Point the File Pathname field to the appropriate AddNumbersWithErrorHandling script.
  5. On the Step Settings pane, switch to the Standard Input tab.
  6. Switch the Input Method combo box to String, and enter in a series of numbers separated by commas. For example:
    2, 5, 8
  7. Switch to the Standard Output tab and store the output in the Variable/Property Step.StdOutput.Text.
  8. Enable the Standard Error by switching the Error Destination dropdown to Store in Value/Property. Leave the default destination, Step.StdError.Text.
  9. Check the Set Error.Msg to Standard Error Test checkbox.
  10. Switch the If Standard Error is Non-Empty dropdown to Set Step Status to Error.
  11. On the Step Settings pane, switch to the Properties tab and select the Additional Results category.
  12. Click the Add Result from List button and add Standard Input, Standard Output and Standard Error.
  13. Now create a copy of the step and insert it right after the original step. Name the copy “Call AddNumbers Script (Incorrect Input)”.
    Modify the Standard Input in this step to cause an error condition. For example:
    2, Five, 8
  14. Save the sequence file as HandlingScriptErrors.seq in the same folder as the script.
  15. Run your sequence file. Your report should look like the following screenshot:

Back to Top

9. Next Steps

Now that you know how to integrate simple scripts into TestStand sequences, the next article will show you how to create more complex scripts that can call DLLs, perform data acquisition in NI-DAQmx and communicate with instruments using NI-VISA.
Advanced Scripting in Perl, Python and Tcl

If you are currently enrolled in the Standard Service Program, you can also access the interactive training module on calling scripts from TestStand here:
On Demand Training: TestStand Modules

Back to Top

Customer Reviews
2 Reviews | Submit your review

AddNumbersStdIn.py  - Sep 5, 2010

I'm haveing difficulty getting the AddNumbersStdIn.py script working. I'm expecting the DOS window to come up, and prompt me, but it dosen't. And why are we putting the values in on thenStandard Input option? I'm a bit confused with this option. All the other stuff seems to work, good tutorial

Probably a Correction in the Document  - Feb 3, 2010

Under the section Evaluating Output in TestStand using <None> Adaptor there is step number 5, On the Step Settings pane, switch to the Standard Input tab. I think this step is probably not necessary.

Bookmark & Share



Ratings

Rate this document

Answered Your Question?
Yes No

Submit