matlab.unittest.parameters.Parameter Class
Namespace: matlab.unittest.parameters
Base class for parameters
Description
The matlab.unittest.parameters.Parameter
class is the base class for parameters used in
parameterized testing. A Parameter
object contains parameterization
information, including the parameter name and value, for a parameterized test. For more
information about test parameterization, see Use Parameters in Class-Based Tests.
Creation
To create a Parameter
object, use the matlab.unittest.parameters.Parameter.fromData
method.
Properties
Name of the property that defines the parameter, represented as a character vector.
Attributes:
GetAccess | public |
SetAccess | private |
Parameter name, represented as a character vector. The Name
property uniquely identifies a particular value for a parameter.
Attributes:
GetAccess | public |
SetAccess | private |
Parameter value, represented as a value of any data type. The
Value
property holds the data that the test runner passes to the
parameterized method that uses the parameter.
Attributes:
GetAccess | public |
SetAccess | private |
Methods
matlab.unittest.parameters.Parameter.fromData | Create parameters from data |
Examples
Create parameters that are external to a test class by using the matlab.unittest.parameters.Parameter.fromData
method. Then, create a test suite by injecting these parameters and run the tests.
First, import the classes used in this example.
import matlab.unittest.parameters.Parameter import matlab.unittest.TestSuite
In a file named ZerosTest.m
in your current folder, create the ZerosTest
test class, which tests the zeros
function.
classdef ZerosTest < matlab.unittest.TestCase properties (TestParameter) type = {'single','double','uint16'}; size = struct("s2d",[3 3],"s3d",[2 5 4]); end methods (Test) function testClass(testCase,size,type) testCase.verifyClass(zeros(size,type),type) end function testSize(testCase,size) testCase.verifySize(zeros(size),size) 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
Using the fromData
static method, redefine the parameters associated with the type
property so that parameterized tests use int64
and uint64
instead of single
, double
, and uint16
as the data type.
newType = {'int64','uint64'}; param = Parameter.fromData("type",newType);
Create a test suite from the test class by injecting the new parameters, and then display the test names. The injected parameters are indicated by #ext
.
suite = TestSuite.fromClass(?ZerosTest,ExternalParameters=param); disp({suite.Name}')
{'ZerosTest/testClass(size=s2d,type=int64#ext)' } {'ZerosTest/testClass(size=s2d,type=uint64#ext)'} {'ZerosTest/testClass(size=s3d,type=int64#ext)' } {'ZerosTest/testClass(size=s3d,type=uint64#ext)'} {'ZerosTest/testSize(size=s2d)' } {'ZerosTest/testSize(size=s3d)' } {'ZerosTest/testDefaultClass' } {'ZerosTest/testDefaultSize' } {'ZerosTest/testDefaultValue' }
Run the tests in the test suite. In this example, all the tests pass.
results = suite.run;
Running ZerosTest ......... Done ZerosTest __________
Redefine the parameters associated with both the type
and size
parameterization properties.
newSize = struct("s1d",[1 5],"s4d",[2 3 2 4]); param = Parameter.fromData("type",newType,"size",newSize);
Create a test suite by injecting the new parameters in the param
array. Then, display the test names.
suite = TestSuite.fromClass(?ZerosTest,ExternalParameters=param); disp({suite.Name}')
{'ZerosTest/testClass(size=s1d#ext,type=int64#ext)' } {'ZerosTest/testClass(size=s1d#ext,type=uint64#ext)'} {'ZerosTest/testClass(size=s4d#ext,type=int64#ext)' } {'ZerosTest/testClass(size=s4d#ext,type=uint64#ext)'} {'ZerosTest/testSize(size=s1d#ext)' } {'ZerosTest/testSize(size=s4d#ext)' } {'ZerosTest/testDefaultClass' } {'ZerosTest/testDefaultSize' } {'ZerosTest/testDefaultValue' }
Run the tests using the newly injected parameters. The tests pass.
results = suite.run;
Running ZerosTest ......... Done ZerosTest __________
Version History
Introduced in R2018b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)