matlab.unittest.TestCase Test Cases in Multiple Files
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Steven Lord
2018-1-31
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 个评论
更多回答(1 个)
Jeremy Ward
2018-1-31
2 个评论
Steven Lord
2018-1-31
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Results, Reporting, and Test File Management 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!