Loop Through Each Element Inside a Container Variable and Output to CSV File in TestStand

Updated Oct 22, 2024

Environment

Software

  • TestStand

In TestStand, a container variable cannot be indexed based on its placement within the container but one may want to loop through all of the elements inside the container for a specific step in the sequence, such as outputting to a CSV file. In order to achieve this, follow the steps below to programmatically loop through a container variable. For this example, it will be looping through a container of string arrays. 

  1. Get the number of elements inside the container to set as the Number of Loops for the For Loop
    1. Create a number-type local variable to store the number of elements
    2. Place a Statement step in the sequence
    3. For the Expression in the step settings, use the GetNumSubProperties method to obtain the number of elements inside the specific containerimage.png
  2. Set up the For Loop step in the sequence for the iterations 
    1. Place the For Loop step in the sequence
    2. Set Number of Loops to the number-type local variable created in step 1.1
    3. Create and set number-type local variable for the Loop Variableimage.png
  3. Get the name of each element (for this example, an array) inside the container
    1. Create a string-type variable to store the names of the arrays
    2. Place a Statement step in the sequence
    3. For the Expression in the step settings, use the GetNthSubPropertyName method to obtain each name
    4. Concatenate the name obtained in step 3.3 with the container name and store in string-type variable created in step 3.1
  4. Get each element (for this example, a string datatype) from the array
    1. In the same Statement step created in step 3, use GetValVariant method and the string-type variable for the names of the arrays created in step 3 as the parameter
  5. Concatenate the elements obtained and separate them with a comma
    1. Use the Str function to concatenate the elements obtained in step 4
  6. Output the concatenated elements to a CSV file using the WriteLine method
    1. In order to output to a CSV file, place New CSV Output Stream Step In the sequence to set up and select the CSV file to log the output, the Output Record Stream Reference, and other configurations
    2. In the same Statement step used for step 3 to 5, use the WriteLine method with "AsCSVFileOutputRecordStream" as the object, and the concatenated string created in step 5 as the lineString parameter

For Steps 3 to 6: image.png

When running the above sequence, you should be able to see that lines have been appended to the CSV file with the string elements that each array contains inside of the "output" container. 
image.png