Borrar filtros
Borrar filtros

functiontests not able to work properly in a for loop

1 visualización (últimos 30 días)
Diego Luca de Tena
Diego Luca de Tena el 13 de Sept. de 2021
Respondida: Abhas el 20 de Mayo de 2024
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 comentario
Jan
Jan el 13 de Sept. de 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"?

Iniciar sesión para comentar.

Respuestas (1)

Abhas
Abhas el 20 de Mayo de 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

Categorías

Más información sobre Testing Frameworks en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by