Main Content

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 theDocPolynomTestexample test presented in thematlab.unittest.qualifications.Verifiableexample. For the function-based test file use theaxesPropertiesTestexample test presented inWrite Test Using Setup and Teardown Functions.

Run All Tests in Class or Function

Use therunmethod of theTestCaseclass to directly run tests contained in a single test file. When running tests directly, you do not need to explicitly create aTestarray.

% 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 usingruntestsor 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 therunmethod. For example, only run the test,testMultiplication, from theDocPolynomTestfile.

results1 = run(DocPolynomTest,'testMultiplication');

Function-based test files return an array ofTestobjects instead of a singleTestCaseobject. You can run a particular test by indexing into the array. However, you must examine theNamefield in the test array to ensure you run the correct test. For example, only run the test,surfaceColorTest, from theaxesPropertiesTestfile.

t2 = axesPropertiesTest;% Test object from function-based testt2(:).Name
ans = axesPropertiesTest/testDefaultXLim ans = axesPropertiesTest/surfaceColorTest

ThesurfaceColorTesttest corresponds to the second element in the array.

Only run thesurfaceColorTesttest.

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作为输入的runtestsfunction.

runtests(pwd);

Alternatively, you can explicitly createTestarrays and use therunmethod to run them.

Run Test Suites from Test Array

You can explicitly createTestarrays and use therunmethod in theTestSuiteclass to run them. Using this approach, you explicitly defineTestSuiteobjects and, therefore, can examine the contents. Theruntestsfunction does not return theTestSuiteobject.

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. Therunmethod of theTestRunnerclass operates on aTestSuiteobject.

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

|||

Related Topics