functiontests not able to work properly in a for loop

1 次查看(过去 30 天)
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 个评论
Jan
Jan 2021-9-13
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"?

请先登录,再进行评论。

回答(1 个)

Abhas
Abhas 2024-5-20
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

类别

Help CenterFile Exchange 中查找有关 Testing Frameworks 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by