functiontests not able to work properly in a for loop

2 visualizzazioni (ultimi 30 giorni)
Dear team,
I am trying to test a set of functions using functiontests each considering different datasets
For this, I have created a for loop that updates a global variable (timestamp) prior calling function tests.
Problem is that it only runs for the last element on the timestamp array.
Can you please provide guidance on how to overcome this issue?
Thanks
Diego
function tests = test_functionality
global timestamp;
addpath(genpath("../../../Source"));
timestamps_list = {'Xxx' 'Yyy'};
for i=1:size(timestamps_list,2)
timestamp = timestamps_list{i};
tests = functiontests(localfunctions);
end
end
  1 Commento
Jan
Jan il 13 Set 2021
Problem is that it only runs for the last element on the timestamp array.
timestamp is a scalar according to:
timestamp = timestamps_list{i};
So "the last" element of this array is "the only one" also. What else could happen?
What exactly is "it" in "it only runs"?

Accedi per commentare.

Risposte (1)

Abhas
Abhas il 20 Mag 2024
Hi Diego,
In the code, the loop is overwriting the 'tests' variable in each iteration, which results in only the last set of tests being returned. To store dynamically generated suite of tests, concatenate or collect these tests in an array that grows with each iteration of your loop.
Her's the MATLAB code to accumulate tests from all iterations into a single test suite:
function tests = test_functionality
global timestamp;
addpath(genpath("../../../Source"));
timestamps_list = {'Xxx', 'Yyy'};
tests = []; % Initialize an empty array to collect tests
for i = 1:length(timestamps_list)
timestamp = timestamps_list{i};
tempTests = functiontests(localfunctions);
tests = [tests, tempTests]; % Concatenate the new tests to the existing ones
end
end
The above code ensure that the 'test_functionality' function returns a suite of tests that includes tests generated for each timestamp of the 'timestamps_list', thereby overcoming the issue of only running tests for the last element.
You may refer to the following documentation links to have a better understanding of writing and dynamically storing tests:
  1. https://www.mathworks.com/help/matlab/matlab-unit-test-framework.html
  2. https://www.mathworks.com/help/matlab/math/creating-and-concatenating-matrices.html

Categorie

Scopri di più su Testing Frameworks in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by