MATLAB: Matlab.unittest.TestCase Test Cases in Multiple Files

MATLABmatlab.unittest.testcase

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).

Best Answer

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.