To Automate Simulink Test Harness on Jenkins

4 views (last 30 days)
Shijil
Shijil on 30 Sep 2024
Commented: Shijil on 14 Oct 2024
I am looking for a way to automate Simulink Test harness with a Spreadsheet based test-vectors.
There's a folder named Test in Gerrit repo consisting .slx [Test Harness file] and .xlsx [Excel based test vector file], if there's any change pushed into the same, it should trigger a jenkins build and show the result of the .xlsx run on top of .slx harness file as a report.
Any suggestions would be helpful, thanks

Answers (1)

nick
nick on 10 Oct 2024
Hi Shilji,
I understand that you want to automate Simulink Test harness with spreadhseet based vectors. To automate the Simulink Test Harness with a spreadsheet-based test vector in Jenkins, you can follow these steps:
Install Required Plugins in Jenkins
Create a New Job in Jenkins
Source Code Management:
  • Select Git and configure your repository URL and credentials.
Additional Behaviors:
  • Set up the following pattern to ignore builds if only HTML files have been committed to the SCM :
Test/.*\.html
Build Triggers:
  • Since you are using Gerrit, you can set up a webhook in Gerrit to trigger the Jenkins job on code push. You can refer to the following documentation about 'Gerrit Trigger Plugin' : https://plugins.jenkins.io/gerrit-trigger/
Build Step:
  • Add a build step to execute a MATLAB script that programatically reads values from spreadsheet and runs the test harness with the following code:
run("runHarness") % Change the name to your MATLAB script
Here, 'runHarness' is a .M file containing code to read data from an .XLSX file, create variables in the base workspace, and run the test harness. The test harness takes values from the base workspace for computation.
Here’s an example MATLAB script for simulating an internal test harness for the ‘Product’ block:
% Load the Simulink model
model = 'TestModel'; %Model Name
load_system(model);
% Get the handle of the subsystem or block for which the test harness is created
blockPath = [model, '/TestSubsystem'];
blockHandle = getSimulinkBlockHandle([model, '/TestSubsystem']);
% Read data from XLSX
table= readtable('Book1.xlsx');
A = table{:,1}
B = table{:,2}
% Open the test harness
harnessName = 'TestSubsystemHarness';
sltest.harness.open(blockHandle, harnessName);
simOut=sim(harnessName);
sltest.harness.close(blockHandle);
% Retrieve and compare results
actualResults = simOut.yout{1}.Values.Data;
Expected = A(1) .* B(1);
testPassed = isequal(actualResults, Expected);
% Generate a report
reportFile = 'TestReport.html';
file=fullfile('Report', reportFile)
fid = fopen(file, 'w');
fprintf(fid, '<html><body>\n');
fprintf(fid, '<h1>Test Report</h1>\n');
if testPassed
fprintf(fid, '<p>Test passed: Results match </p>\n');
else
fprintf(fid, '<p>Test failed: Results do not match</p>\n');
end
fprintf(fid, '<p>%s</p>\n', Expected);
fprintf(fid, '</body></html>\n');
fprintf(fid, '</body></html>\n');
fclose(fid)
Post-Build Actions:
  • You can execute a .BAT or .SH file depending on the OS of the Jenkins environment to push the generated report to the repository.
Here’s an example batch script:
@echo off
git add Report/TestReport.html %git add <Location of the TestReport>
git commit -m "Add Simulink test report"
git push -f origin HEAD:main
Hope this helps!
  1 Comment
Shijil
Shijil on 14 Oct 2024
Thanks for your response and time @nick. I would try your suggestions and update the outcome.

Sign in to comment.

Categories

Find more on Results, Reporting, and Test File Management in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!