Run set of tests
results = runtests
runs all the tests in
your current folder, and returns the results as a TestResult
object.
results = runtests(
runs a set
of tests with additional options specified by one or more tests
,Name,Value
)Name,Value
pair arguments.
Create a folder myExample
in your current working folder, and change into that folder.
In the myExample
folder, create a test script, typeTest.m
.
%% Test double class exp = 'double'; act = ones; assert(isa(act,exp)) %% Test single class exp = 'single'; act = ones('single'); assert(isa(act,exp)) %% Test uint16 class exp = 'uint16'; act = ones('uint16'); assert(isa(act,exp))
In the myExample
folder, create a test script, sizeValueTest.m
.
%% Test size exp = [7 13]; act = ones([7 13]); assert(isequal(size(act),exp)) %% Test values act = ones(42); assert(unique(act) == 1)
Run all tests in the current folder.
runtests
Running sizeValueTest .. Done sizeValueTest __________ Running typeTest ... Done typeTest __________ ans = 1x5 TestResult array with properties: Name Passed Failed Incomplete Duration Details Totals: 5 Passed, 0 Failed, 0 Incomplete. 0.038077 seconds testing time.
MATLAB® ran 5 tests. There are 2 passing tests from sizeValueTest
and 3 passing tests from typeTest
.
Create the test file shown below, and save it as runtestsExampleTest.m
on your MATLAB path.
function tests = runtestsExampleTest tests = functiontests(localfunctions); function testFunctionOne(testCase)
Run the tests.
results = runtests('runtestsExampleTest.m');
Running runtestsExampleTest . Done runtestsExampleTest __________
If it doesn't already exist, create the test file, runtestsExampleTest.m
,
in the example above.
Create a subdirectory, tmpTest
, and,
in that directory, create the following runtestsExampleSubFolderTest.m
file.
function tests = runtestsExampleSubFolderTest tests = functiontests(localfunctions); function testFunctionTwo(testCase)
Run the tests from the directory above tmpTest
by
setting 'IncludeSubfolders'
to true.
results = runtests(pwd,'IncludeSubfolders',true);
Running runtestsExampleTest . Done runtestsExampleTest __________ Running runtestsExampleSubFolderTest . Done runtestsExampleSubFolderTest __________
runtests
ran the tests in both the current
directory and the subdirectory.
If you do not specify the 'IncludeSubfolders'
property
for the runtests
function, it does not run the
test in the subdirectory.
results = runtests(pwd);
Running runtestsExampleTest . Done runtestsExampleTest __________
Create the following test file, and save it
as runInParallelTest.m
on your MATLAB® path.
function tests = runInParallelTest tests = functiontests(localfunctions); function testA(testCase) verifyEqual(testCase,5,5); function testB(testCase) verifyTrue(testCase,logical(1)); function testC(testCase) verifySubstring(testCase,'SomeLongText','Long'); function testD(testCase) verifySize(testCase,ones(2,5,3),[2 5 3]); function testE(testCase) verifyGreaterThan(testCase,3,2); function testF(testCase) verifyEmpty(testCase,{},'Cell array is not empty.'); function testG(testCase) verifyMatches(testCase,'Some Text','Some [Tt]ext');
Run the tests in parallel. Running tests in parallel requires the Parallel Computing Toolbox™. The testing framework might vary the order and number of groups or which tests it includes in each group.
results = runtests('runInParallelTest','UseParallel',true);
Split tests into 7 groups and running them on 4 workers. ---------------- Finished Group 2 ---------------- Running runInParallelTest . Done runInParallelTest __________ ---------------- Finished Group 3 ---------------- Running runInParallelTest . Done runInParallelTest __________ ---------------- Finished Group 1 ---------------- Running runInParallelTest . Done runInParallelTest __________ ---------------- Finished Group 4 ---------------- Running runInParallelTest . Done runInParallelTest __________ ---------------- Finished Group 6 ---------------- Running runInParallelTest . Done runInParallelTest __________ ---------------- Finished Group 5 ---------------- Running runInParallelTest . Done runInParallelTest __________ ---------------- Finished Group 7 ---------------- Running runInParallelTest . Done runInParallelTest __________
In your working folder, create testZeros.m
. This class contains four test methods.
classdef testZeros < matlab.unittest.TestCase properties (TestParameter) type = {'single','double','uint16'}; outSize = struct('s2d',[3 3], 's3d',[2 5 4]); end methods (Test) function testClass(testCase, type, outSize) testCase.verifyClass(zeros(outSize,type), type); end function testSize(testCase, outSize) testCase.verifySize(zeros(outSize), outSize); end function testDefaultClass(testCase) testCase.verifyClass(zeros, 'double'); end function testDefaultSize(testCase) testCase.verifySize(zeros, [1 1]); end function testDefaultValue(testCase) testCase.verifyEqual(zeros,0); end end end
The full test suite has 11 test elements: 6 from the testClass
method, 2 from the testSize
method, and 1 each from the testDefaultClass
, testDefaultSize
, and testDefaultValue
methods.
At the command prompt, run the test elements that use the outSize
parameter property.
runtests('testZeros','ParameterProperty','outSize')
Running testZeros ........ Done testZeros __________ ans = 1×8 TestResult array with properties: Name Passed Failed Incomplete Duration Details Totals: 8 Passed, 0 Failed, 0 Incomplete. 0.12125 seconds testing time.
The runtests
function executed eight tests that use the outSize
parameter property: six from the testClass
method and two from the testSize
method.
At the command prompt, run the test elements that use the single
parameter name.
runtests('testZeros','ParameterName','single')
Running testZeros .. Done testZeros __________ ans = 1×2 TestResult array with properties: Name Passed Failed Incomplete Duration Details Totals: 2 Passed, 0 Failed, 0 Incomplete. 0.025001 seconds testing time.
The runtests
function executed the two tests from the testClass
method that use the outSize
parameter name.
tests
— Array of testsSuite of tests specified as a character vector or cell array of character vectors. Each character vector in the cell array can contain the name of a test file, a test class, a test suite element name, a package containing your test classes, or a folder containing your test files.
Example: runtests('ATestFile.m')
Example: runtests('ATestFile/aTest')
Example: runtests('mypackage.MyTestClass')
Example: runtests(pwd)
Example: runtests({'mypackage.MyTestClass','ATestFile.m',pwd,'mypackage.subpackage'})
Specify optional comma-separated pairs of Name,Value
arguments.
Name
is the argument
name and Value
is the corresponding
value. Name
must appear
inside single quotes (' '
).
You can specify several name and value pair
arguments in any order as Name1,Value1,...,NameN,ValueN
.
runtests(tests,'Name','productA_*')
runs
test elements with a name that starts with 'productA_'
.'UseParallel'
— Indicator to run tests in parallelfalse
(default) | true
| 0
| 1
Indicator to run tests in parallel, specified as false
or true
(0
or 1
).
By default runtests
runs tests in serial. If you
set UseParallel
to true
, the runtests
function
divides the test suite into separate groups and runs the groups in
parallel provided that:
The Parallel Computing Toolbox is installed.
An open parallel pool exists or automatic pool creation is enabled in the Parallel Preferences.
Otherwise, runtests
runs tests in
serial regardless of the value for UseParallel
.
Note: Running tests in parallel requires the Parallel Computing Toolbox. The testing framework might vary the order and number of groups or which tests it includes in each group. |
Data Types: logical
'Name'
— Name of suite elementName of the suite element, specified as a character vector.
This argument filters TestSuite
array elements. For
the testing framework to run a test, the Name
property
of the test element must match the specified name. Use the wildcard
character, *
, to match any number of characters.
Use the question mark character, ?
, to match a
single character.
'IncludeSubfolders'
— Indicator to run tests in subfoldersfalse
(default) | true
| 0
| 1
Indicator to run tests in subfolders, specified as false
or true
(0
or 1
).
By default the framework runs tests in the specified folders, but
not in their subfolders.
Data Types: logical
'IncludeSubpackages'
— Indicator to run tests in subpackagesfalse
(default) | true
| 0
| 1
Indicator to run tests in subpackages, specified as false
or true
(0
or 1
).
By default the framework runs tests in the specified packages, but
not in their subpackages.
Data Types: logical
'ParameterProperty'
— Name of parameterization propertyName of a parameterization property used by the test suite element,
specified as a character vector. This argument filters TestSuite
array
elements. Use the wildcard character *
to match
any number of characters. Use the question mark character ?
to
match to a single character.
'ParameterName'
— Name of parameterName of a parameter used by the test suite element, specified
as a character vector. This argument filters TestSuite
array
elements. Use the wildcard character *
to match
any number of characters. Use the question mark character ?
to
match a single character.
'BaseFolder'
— Name of base folderName of the base folder that contains the file defining the
test class, function, or script, specified as a character vector.
This argument filters TestSuite
array elements. For
a test element to be included in the suite, the test element must
be contained in the specified base folder. Use the wildcard character *
to
match any number of characters. Use the question mark character ?
to
match a single character. For test files defined in packages, the
base folder is the parent of the top-level package folder.
'Tag'
— Name of test element tagName of test element tag, specified as a character vector. This
argument filters TestSuite
array elements. Use the
wildcard character *
to match any number of characters.
Use the question mark character ?
to match a single
character.
functiontests
| matlab.unittest.TestResult
| matlab.unittest.TestRunner
| matlab.unittest.TestSuite