Run Tests for Various Workflows
Set Up Example Tests
To explore different ways to run tests, create a class-based test and a function-based test in your current working folder. For the class-based test file use theDocPolynomTest
example test presented in thematlab.unittest.qualifications.Verifiable
example. For the function-based test file use theaxesPropertiesTest
example test presented inWrite Test Using Setup and Teardown Functions.
Run All Tests in Class or Function
Use therun
method of theTestCase
class to directly run tests contained in a single test file. When running tests directly, you do not need to explicitly create aTest
array.
% Directly run a single file of class-based testsresults1 = run(DocPolynomTest);% Directly run a single file of function-based testsresults2 = run(axesPropertiesTest);
You can also assign the test file output to a variable and run the tests using the functional form or dot notation.
% Create Test or TestCase objectst1 = DocPolynomTest;% TestCase object from class-based testt2 = axesPropertiesTest;% Test object from function-based test% Run tests using functional formresults1 = run(t1); results2 = run(t2);% Run tests using dot notationresults1 = t1.run; results2 = t2.run;
Alternatively, you can run tests contained in a single file by usingruntests
or from the Editor.
Run Single Test in Class or Function
Run a single test from within a class-based test file by specifying the test method as an input argument to therun
method. For example, only run the test,testMultiplication
, from theDocPolynomTest
file.
results1 = run(DocPolynomTest,'testMultiplication');
Function-based test files return an array ofTest
objects instead of a singleTestCase
object. You can run a particular test by indexing into the array. However, you must examine theName
field in the test array to ensure you run the correct test. For example, only run the test,surfaceColorTest
, from theaxesPropertiesTest
file.
t2 = axesPropertiesTest;% Test object from function-based testt2(:).Name
ans = axesPropertiesTest/testDefaultXLim ans = axesPropertiesTest/surfaceColorTest
ThesurfaceColorTest
test corresponds to the second element in the array.
Only run thesurfaceColorTest
test.
results2 = t2(2).run;% or results2 = run(t2(2));
Alternatively, you can run a single test from the Editor.
Run Test Suites by Name
You can run a group, or suite, of tests together. To run the test suite usingruntests
, the suite is defined as a cell array of character vectors representing a test file, a test class, a package that contains tests or a folder that contains tests.
suite = {'axesPropertiesTest','DocPolynomTest'}; runtests(suite);
Run all tests in the current folder using thepwd
作为输入的runtests
function.
runtests(pwd);
Alternatively, you can explicitly createTest
arrays and use therun
method to run them.
Run Test Suites from Test Array
You can explicitly createTest
arrays and use therun
method in theTestSuite
class to run them. Using this approach, you explicitly defineTestSuite
objects and, therefore, can examine the contents. Theruntests
function does not return theTestSuite
object.
importmatlab.unittest.TestSuites1 = TestSuite.fromClass(?DocPolynomTest); s2 = TestSuite.fromFile('axesPropertiesTest.m');% generate test suite and then runfullSuite = [s1 s2]; result = run(fullSuite);
Since the suite is explicitly defined, it is easy for you to perform further analysis on the suite, such as rerunning failed tests.
failedTests = fullSuite([result.Failed]); result2 = run(failedTests);
Run Tests with Customized Test Runner
You can specialize the test running by defining a custom test runner and adding plugins. Therun
method of theTestRunner
class operates on aTestSuite
object.
importmatlab.unittest.TestRunnerimportmatlab.unittest.TestSuiteimportmatlab.unittest.plugins.TestRunProgressPlugin% Generate TestSuite.s1 = TestSuite.fromClass(?DocPolynomTest); s2 = TestSuite.fromFile('axesPropertiesTest.m'); suite = [s1 s2];%测试运行器创建沉默。runner = TestRunner.withNoPlugins;% Add plugin to display test progress.runner.addPlugin(TestRunProgressPlugin.withVerbosity(2))% Run tests using customized runner.result = run(runner,[suite]);
See Also
runtests
|run (TestCase)
|run (TestSuite)
|run (TestRunner)