Documentation Center

  • Trial Software
  • Product Updates

matlab.unittest.TestSuite.fromFile

Class: matlab.unittest.TestSuite
Package: matlab.unittest

Create TestSuite array from test file

Syntax

  • suite = matlab.unittest.TestSuite.fromFile(file) example
  • suite = matlab.unittest.TestSuite.fromFile(file,s) example
  • suite = matlab.unittest.TestSuite.fromFile(___,Name,Value) example

Description

example

suite = matlab.unittest.TestSuite.fromFile(file) creates a TestSuite array from all of the Test methods in file.

When the test suite is run, MATLAB® changes the current folder to the folder that defines the test content, and adds it to the path for the duration of the test run.

example

suite = matlab.unittest.TestSuite.fromFile(file,s) creates a TestSuite array from all of the Test methods in file that satisfy the conditions specified by the selector, s.

example

suite = matlab.unittest.TestSuite.fromFile(___,Name,Value) creates a TestSuite array with additional options specified by one or more Name,Value pair arguments. You can use this syntax with any of the input arguments of the previous syntaxes.

Input Arguments

file

Class file derived from matlab.unittest.TestCase containing test methods, specified as a string.

s

Selector, specified as an instance of a class from the matlab.unittest.selector package.

Name-Value Pair Arguments

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.

'Name'

String indicating the name of the suite element. To include a test element 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 to exactly one character.

'ParameterProperty'

String indicating the name of a property that defines a parameter used by the test suite element. Use the wildcard character, *, to match any number of characters. Use the question mark character, ?, to match to exactly one character.

'ParameterName'

String indicating the name of a parameter used by the test suite element. Use the wildcard character, *, to match any number of characters. Use the question mark character, ?, to match to exactly one character.

'BaseFolder'

String indicating the name of the folder that contains the file defining the test class or function. 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 to exactly one character. For test classes defined in packages, the base folder is the parent of the top-level package folder.

Output Arguments

suite

Set of tests, specified as a matlab.unittest.Test array.

Attributes

Statictrue

To learn about attributes of methods, see Method Attributes in the MATLAB Object-Oriented Programming documentation.

Examples

expand all

Run Tests in Class File

Function for unit testing:

function res = add5(x)
%ADD5 Increment input by 5.
if ~isa(x,'numeric')
    error('add5:InputMustBeNumeric','Input must be numeric.');
end
res = x + 5;
end

TestCase class containing test methods:

classdef Add5Test < matlab.unittest.TestCase
    methods (Test)
        function testDoubleOut(testCase)
            actOutput = add5(1);
            testCase.verifyClass(actOutput,'double');
        end
        function testNonNumericInput(testCase)
            testCase.verifyError(@()add5('0'),'add5:InputMustBeNumeric');
        end
    end
end

Create a test suite from the Add5Test class file.

suite = matlab.unittest.TestSuite.fromFile('Add5Test.m');
result = run(suite);
Running Add5Test
..
Done Add5Test
__________

Create Suite of Test Elements Using Selector

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 test class contains two parameterized test methods, testClass and testSize.

At the command prompt, create a test suite from all parameterized test methods in testZeros.m using the HasParameter selector.

import matlab.unittest.TestSuite;
import matlab.unittest.selectors.HasParameter;

suite = TestSuite.fromFile('testZeros.m', HasParameter)
suite = 

  1x8 Test array with properties:

    Name
    Parameterization
    SharedTestFixtures

Create a test suite from only the test elements from the testSize method using the HasName selector with the StartsWithSubstring constraint.

import matlab.unittest.selectors.HasName;
import matlab.unittest.constraints.StartsWithSubstring;

suite = TestSuite.fromFile('testZeros.m',...
    HasName(StartsWithSubstring('testZeros/testSize')));
{suite.Name}'
ans = 

    'testZeros/testSize(outSize=s2d)'
    'testZeros/testSize(outSize=s3d)'

The test suite contains the two parameterized tests from the testSize method.

Create Suite of Test Elements Using Name-Value Arguments

Create the testZeros.m class from the previous example.

At the command prompt, create a test suite from all test methods in testZeros.m that have a name starting with 'testZeros/testSize'. This test suite contains parameterized tests from the testSize method.

import matlab.unittest.TestSuite;

suite = TestSuite.fromFile('testZeros.m', 'Name', 'testZeros/testSize*');
{suite.Name}'
ans = 

    'testZeros/testSize(outSize=s2d)'
    'testZeros/testSize(outSize=s3d)'

To ensure that a test suite is comprised of test elements associated with one particular test method, use the fromMethod method of TestSuite.

At the command prompt, create a test suite from all test methods in testZeros.m that have a name ending in 'Size'.

import matlab.unittest.TestSuite;

suite = TestSuite.fromFile('testZeros.m', 'Name', '*Size');
{suite.Name}'
ans = 

    'testZeros/testDefaultSize'

Note that elements from the testSize method are not included in the test suite. The name of these elements contains information about the parameterization, and therefore it does not end with 'Size'.

Create a test suite of all tests that use the parameter name 'double'.

suite = TestSuite.fromFile('testZeros.m', 'ParameterName', 'double');
{suite.Name}'
ans = 

    'testZeros/testClass(type=double,outSize=s2d)'
    'testZeros/testClass(type=double,outSize=s3d)'

To construct the same test suite using selectors, use suite = TestSuite.fromFile('testZeros.m', HasParameter('Name','double')).

See Also

| |

Was this topic helpful?