Create scalar test suites from test names by using the fromName
static method.
In a file in your current folder, create the add5
function. The function accepts a numeric input and increments it by 5. If called with a nonnumeric input, the function throws an error.
function y = add5(x)
% add5 - Increment input by 5
if ~isa(x,"numeric")
error("add5:InputMustBeNumeric","Input must be numeric.")
end
y = x + 5;
end
To test the add5
function, create the Add5Test
class in a file named Add5Test.m
in your current folder. The class tests the function against numeric and nonnumeric inputs.
classdef Add5Test < matlab.unittest.TestCase
properties (TestParameter)
type = {'double','single','int8','int32'};
end
methods (Test)
function numericInput(testCase,type)
actual = add5(cast(1,type));
testCase.verifyClass(actual,type)
end
function nonnumericInput(testCase)
testCase.verifyError(@() add5("0"),"add5:InputMustBeNumeric")
end
end
end
Import the TestSuite
class.
Create a test suite from the Add5Test
class and display the test names.
{'Add5Test/numericInput(type=double)'}
{'Add5Test/numericInput(type=single)'}
{'Add5Test/numericInput(type=int8)' }
{'Add5Test/numericInput(type=int32)' }
{'Add5Test/nonnumericInput' }
Create a test suite from the name of the test that corresponds to the nonnumericInput
method. The resulting test suite contains a single Test
object. Then run the test.
Running Add5Test
.
Done Add5Test
__________
Create a scalar test suite from the name of a parameterized test, and run the test.
Running Add5Test
.
Done Add5Test
__________