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!