matlab.unittest.TestCase Test Cases in Multiple Files
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Jeremy Ward
el 31 de En. de 2018
Comentada: Jeremy Ward
el 31 de En. de 2018
Is it possible to use the standard class folder structure and the matlab.unittest.TestCase infrastructure? The collection of all of my unit tests for a given DUT is 50k lines long. I desire to break each test into it's own file. But in the "Write Simple Test Case Using Classes" SolverTest.m example, when I make an @SolverTest directory, add the class def to that folder and put the testRealSolution function in it's own file in that directory, Matlab fails to find it when I run(testCase).
0 comentarios
Respuesta aceptada
Steven Lord
el 31 de En. de 2018
When you save a class file in a directory whose name starts with the @ symbol, all of the .m files in that folder are treated as methods of that class. You don't want your test file to be a method of the class it is testing, so it shouldn't be in that same @ directory.
Since you're writing your test as a MATLAB class in a classdef file, you could put it in its own @ directory (with the different methods as individual files in that directory.) You could instead create a unittest directory at the same level as the @SolverTest directory and have multiple classdef files, each one a subclass of matlab.unittest.TestCase testing a different part of your software under test, in that unittest directory.
As a simple example, I've attached a ZIP file with four files. Once you unzip it and navigate to the directory in which you extracted them, you should see three directories. You can run the tests in testClass (which exercise sut) as:
runtests testClass
If you add the directory in which you extracted those three subdirectories to your path (so MATLAB has access to the sut class) you can run the test in the unittest directory:
addpath(pwd)
cd unittest
runtests testClass2
I expect all the regular testing infrastructure (creating test suites using matlab.unittest.TestSuite, running them using a matlab.unittest.TestRunner, etc.) should work with either testClass or testClass2.
0 comentarios
Más respuestas (1)
Jeremy Ward
el 31 de En. de 2018
2 comentarios
Steven Lord
el 31 de En. de 2018
This page in the documentation goes into a little bit more detail about how to define class methods in separate files.
I recommend you consider the unittest directory with multiple classdef files. If you organize those test classes well, you may only need to run a subset of your test files when you modify a particular part of your software under test's functionality. For example, you could have testConstruction.m, testIndexing.m, testSum.m, etc. If you are only changing the sum overloaded method, you may not need to run the tests for the constructor or the indexing tests as they (shouldn't) be affected by changes to the sum method.
Ver también
Categorías
Más información sobre Results, Reporting, and Test File Management en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!