フィルターのクリア

matlab.unittest.TestCase Test Cases in Multiple Files

5 ビュー (過去 30 日間)
Jeremy Ward
Jeremy Ward 2018 年 1 月 31 日
コメント済み: Jeremy Ward 2018 年 1 月 31 日
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).

採用された回答

Steven Lord
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.

その他の回答 (1 件)

Jeremy Ward
Jeremy Ward 2018 年 1 月 31 日
Thanks Steven! So it looks each test case (function) needs to be at least prototyped in the methods(Test) of the class definition file. That was the piece I was missing. I tried to put the test case functions in files in the @testClass... and had hoped Matlab would just find them. It didn't without the prototype in @testClass/testClass.m. Thanks again!
  2 件のコメント
Steven Lord
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.
Jeremy Ward
Jeremy Ward 2018 年 1 月 31 日
Great idea. Thanks much!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by