Create suite of tests
suite = testsuite
creates a suite of tests
from your current folder, and returns the suite as a Test
array.
To run a test suite created with testsuite
,
use the run
method of matlab.unittest.TestSuite
, matlab.unittest.TestRunner
,
or matlab.perftest.TimeExperiment
.
suite = testsuite(
creates
a suite from a set of specified tests.tests
)
suite = testsuite(
creates
a suite 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, make it your current working folder, and create a
couple of tests.
In the myExample
folder, create a script-based
test, onesTest.m
.
%% Test double class expClass = 'double'; act = ones; assert(isa(act,expClass)) %% Test single class expClass = 'single'; act = ones('single'); assert(isa(act,expClass)) %% Test uint16 class expClass = 'uint16'; act = ones('uint16'); assert(isa(act,expClass)) %% Test size expSize = [7 13]; act = ones([7 13]); assert(isequal(size(act),expSize)) %% Test values act = ones(42); assert(unique(act) == 1)
In the myExample
folder, create a function-based
test, eyeTest.m
.
function tests = eyeTest tests = functiontests(localfunctions); function doubleClassTest(testCase) actValue = eye; verifyClass(testCase,actValue,'double') function singleClassTest(testCase) actValue = eye('single'); verifyClass(testCase,actValue,'single') function uint16ClassTest(testCase) actValue = eye('uint16'); verifyClass(testCase,actValue,'uint16') function sizeTest(testCase) expSize = [7 13]; actValue = eye(expSize); verifySize(testCase,actValue,expSize); function valueTest(testCase) actValue = eye(42); verifyEqual(testCase,unique(diag(actValue)),1) % diagonal are 1s verifyEqual(testCase,unique(triu(actValue,1)),0) % upper tri vals are 0 verifyEqual(testCase,unique(tril(actValue,-1)),0) % lower tri vals are 0
Create a test suite from all tests in the current folder.
suite = testsuite
suite = 1x10 Test array with properties: Name BaseFolder Parameterization SharedTestFixtures Tags Tests Include: 0 Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.
If onesTest
and eyesTest
are
the only tests in your folder, MATLAB® creates a suite of 10 tests.
View the names of the tests in suite
.
{suite.Name}'
ans = 'eyeTest/doubleClassTest' 'eyeTest/singleClassTest' 'eyeTest/uint16ClassTest' 'eyeTest/sizeTest' 'eyeTest/valueTest' 'onesTest/TestDoubleClass' 'onesTest/TestSingleClass' 'onesTest/TestUint16Class' 'onesTest/TestSize' 'onesTest/TestValues'
Create a test suite from all tests in eyeTest
.
suite2 = testsuite('eyeTest')
suite2 = 1x5 Test array with properties: Name BaseFolder Parameterization SharedTestFixtures Tags Tests Include: 0 Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.
In your working folder, create a class-based test, testZeros.m
. This class contains five 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.
Create a test suite from the test elements with test names that contain 'Default'
.
suite = testsuite('testZeros','Name','*Default*')
suite = 1×3 Test array with properties: Name BaseFolder Parameterization SharedTestFixtures Tags Tests Include: 0 Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.
Create a test suite from the test elements that use the outSize
parameter property.
suite = testsuite('testZeros','ParameterProperty','outSize')
suite = 1×8 Test array with properties: Name BaseFolder Parameterization SharedTestFixtures Tags Tests Include: 5 Unique Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.
The test suite contains eight tests that use the outSize
parameter property: six from the testClass
method and two from the testSize
method.
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: testsuite('ATestFile.m')
Example: testsuite('ATestFile/aTest')
Example: testsuite('mypackage.MyTestClass')
Example: testsuite(pwd)
Example: testsuite({'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
.
suite = testsuite(tests,'Name','productA_*')
creates
a test suite from tests
that have names starting
with 'productA_'
.'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 include a test in the suite, 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 include tests in subfoldersfalse
(default) | true
| 0
| 1
Indicator to include tests in subfolders in the suite, specified
as false
or true
(0
or 1
).
By default the framework creates a suite from tests in the specified
folders and not in their subfolders.
Data Types: logical
'IncludeSubpackages'
— Indicator to include tests in subpackagesfalse
(default) | true
| 0
| 1
Indicator to include tests in subpackages in the suite, specified
as false
or true
(0
or 1
).
By default the framework creates a suite from tests in the specified
package and not in their subpackages.
Data Types: logical
'ParameterProperty'
— Name of parameterization propertyName of a property that defines 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.
'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 folder that contains the file defining the test
class, function, or script, specified as a character vector. This
argument filters TestSuite
array elements. To include
a test element in the suite, the specified base folder must contain
that test element. 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 to
exactly one character.
If you do not need to create a test suite explicitly,
use runtests
or runperf
to create the suite implicitly
before running the tests.
An alternative way to create an explicit test suite
is to use the matlab.unittest.TestSuite
methods.
matlab.perftest.TimeExperiment.run
| matlab.unittest.TestRunner.run
| matlab.unittest.TestSuite
| matlab.unittest.TestSuite.run