How to clear a TestCase class from memory?

13 visualizaciones (últimos 30 días)
Jim Svensson
Jim Svensson el 2 de Dic. de 2020
Comentada: Jim Svensson el 12 de Abr. de 2021
I have an class based test case as follows (simplified):
classdef my_test < matlab.unittest.TestCase
properties (TestParameter)
test_params = some_package.get_test_parameters();
end
methods (Test)
function basic_test(tc, test_params)
end
end
end
The data returned by get_test_parameters() is used by other test cases and in other places as well.
The problem is that if I change the data returned by that function and rerun the test case the test_params are not updated because Matlab only initialize it one time when it load the my_test class into memory. Fine, but I need to make sure that when I run the test it uses the latest data from the get_test_params() function. What is the best way to do this? The running of various test cases is done in another top level function.
Some options that work but are not ideal:
  • Issue "clear classes". This clears all classes and other things like global variables and persistent variables. I don't really want that. And why is "clear classes" clearing anything else but classes? What is even more stupid is that "clear ALL" does NOT clear ALL. I don't like functions that lie about what they do.
  • Issue a clear my_test. But my_test is also actually inside a package and "clear" is again weird. Even if it is inside a package you should use "clear my_class", If I have multiple classes with the same name but in different packages you cannot control which class is cleared it seems. Futhermore the same syntax is used to clear a variable, so if I have a variable called my_test that will be cleared also.
In the top level file for running test cases I am creating an array of test cases with
matlab.unittest.TestSuite.fromPackage(package_of_test_cases, 'IncludingSubpackages', true)
and run them all. I want a good way to make sure that all the classes in those packages are cleared and reloaded before the test cases are run. Any ideas?

Respuestas (2)

Preethi Pandian
Preethi Pandian el 9 de Abr. de 2021
Hi Jim,
So I believe you have a function get_test-parameters() and you would want your testCases to retrieve the latest value from the function.
Test fixtures might help to solve the situation you are currently facing. Test fixtures are setup and teardown code that sets up the pretest state of the system and returns it to the original state after running the test.
Perhaps you could try using TestMethodSetup that would run before executing each testCase and retrieve the latest value of your get_test_parameters() functions.
Your test could change to something like this:
classdef my_test < matlab.unittest.TestCase
properties (TestParameter)
test_params;
end
methods(TestMethodSetup)
test_params = some_package.get_test_parameters();
end
methods (Test)
function basic_test(tc, test_params)
end
end
end
Also, you could use TestMethodTeardown or TestClassTeardown to clear any data after execution of each testcase or test class respectively.
You can refer here for more information on Test fixtures:
Hope this helps,
Regards,
Preethi

Steven Lord
Steven Lord el 9 de Abr. de 2021

Categorías

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

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by