Contents

Importing test cases from Excel® into a test harness

This example shows how to programmatically import test cases from an Excel® file into an existing SystemTest™ TEST-file.

This example requires the following products to run:

  • Simulink®

  • Stateflow®

Simulink Model Overview

The Simulink model used in this example is a modified version of the fuel rate controller with one root-level Inport block using a bus object, where each signal in the bus represents:

  • Throttle angle

  • Speed

  • Exhaust gas (EGO)

  • Manifold absolute pressure (MAP)

demosystest_fuelctrl_bus_model

Step 1: Read data from an Excel® file

For this example, we will import a test case from a single worksheet in a Excel file using the function XLSREAD. This worksheet contains 5 columns of data where the first column is the time vector and the each of the remaining 4 columns is a test signal. The signal names are:

  • throttle

  • engineSpeed

  • EGO

  • MAP

xlsFileName = 'st_fuelctrl_testcases.xls';
sheetName = 'Test Case 1';
[data headers] = xlsread(xlsFileName, sheetName);

% Time is the first column
time = data(:, 1);

% The remaining columns contain the signal data
allSignalsData = data(:, 2:end);

% The column headers contain the signal names
allSignalNames = headers(2:end);

Step 2: Create SystemTest test case object

Next we will create a test case object and provide a name for the test case. This name is useful for identifying the test case in the Test Case Editor, the test results object, and in the test execution report.

testCaseName = sprintf('Excel Sheet "%s"', sheetName);
xlsTestCase = systest.TestCase(testCaseName);

% Update the test case description
xlsTestCase.Properties.Description = sprintf('This Test Case was imported from the sheet "%s" in the file "%s".', xlsFileName, sheetName);

Now, let's look at the resulting test case object.

xlsTestCase
xlsTestCase = 

  systest.TestCase 

   Properties:
               Name: 'Excel Sheet "Test Case 1"'
           Duration: 0 seconds
       DurationMode: 'LongestSignal'

   Signal Names:   

   Description: 

    This Test Case was imported from the sheet "st_fuelctrl_testcases.xls" in the file "Test Case 1".

  <a href="matlab:methods('systest.TestCase')">Methods</a>

Step 3: Create a signal object for each column of the spreadsheet and add it to a test case object

Next we will create a signal object for each test signal in the worksheet. Each signal object is added to a structure where the name of the field is the name of the signal and the value of the field is the signal object. This structure will be added to the test case object.

% Go through each column of data and create a systest.signals.Signal
% object
for signalIdx = 1:length(allSignalNames)

    % Extract the time and data for each signal
    timeValues = time;
    dataValues = allSignalsData(:, signalIdx);

    % Create a signal with custom segment
    signal = systest.signals.Signal('Custom', {'Time', timeValues, 'Data', dataValues});

    % Get the signal name
    signalFullName = allSignalNames{signalIdx};

    % Assign the bus signal to the structure
    busStruct.(signalFullName) = signal;
end

% Assign the bus structure to the test case object
xlsTestCase.input_bus = busStruct;
Looking at the test case object, we now see that "input_bus" has been
added to the test case.
xlsTestCase
xlsTestCase = 

  systest.TestCase 

   Properties:
               Name: 'Excel Sheet "Test Case 1"'
           Duration: 0.08 seconds
       DurationMode: 'LongestSignal'

   Signal Names:   
      input_bus: [1x1 struct]

   Description: 

    This Test Case was imported from the sheet "st_fuelctrl_testcases.xls" in the file "Test Case 1".

  <a href="matlab:methods('systest.TestCase')">Methods</a>

Step 4: Create a SystemTest TEST-File and add the test case to it

We can automatically generate a preconfigured SystemTest TEST-File for our model from the MATLAB command line. For more information, see the example Creating test harness for a model using buses at the root-level inports

testFileName = 'demosystest_fuelctrl_harness.test';
modelName = 'demosystest_fuelctrl_bus_model.slx';
systest.createHarness(testFileName, modelName);

Now we are ready to append the test case to the TEST-file.

% Load existing test cases from the TEST-file
testCases = stLoadTestCases(testFileName);

% Append the Excel test case
testCases = [testCases xlsTestCase];

% Save the test cases to the TEST-file
stSaveTestCases(testFileName,testCases);
The appended test cases will be run in the second iteration of the test.
testCases
testCases = 

    A [1x2 systest.TestCase] array 

    SignalNames: input_bus.throttle, input_bus.engineSpeed, input_bus.EGO, input_bus.MAP

    Name                           Duration      DurationMode       
    ----------------------------------------------------------------
    TestCase1                      10            LongestSignal      
    Excel Sheet "Test Case 1"      0.08          LongestSignal      
Was this topic helpful?