functiontests not able to work properly in a for loop
9 ビュー (過去 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
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
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:
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Testing Frameworks についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!