matlab.unittest.TestSuite.fromPackage

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

Create TestSuite array from all tests in package

Description

example

suite = matlab.unittest.TestSuite.fromPackage(package) creates a TestSuite array from all of the Test methods of all the tests contained in package and returns that array in suite. The method is not recursive, returning only those tests directly in the package specified.

example

suite = matlab.unittest.TestSuite.fromPackage(package,s) creates a TestSuite array from all the tests contained in package that satisfy the conditions specified by the selector, s.

example

suite = matlab.unittest.TestSuite.fromPackage(___,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

package

The name of the desired package to find tests, specified as a character vector or string scalar.

s

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

Name-Value Pair Arguments

'BaseFolder'

Character vector or string scalar indicating the name of the parent of the top-level package folder that contains the tests. 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 to exactly one character.

'ExternalParameters'

Array of matlab.unittest.parameters.Parameter instances for use in parameterized tests. The framework uses these external parameters in place of the corresponding parameters that are defined within a parameterized test. For more information, see Use External Parameters in Parameterized Test.

'IncludingSubpackages'

Indicator for whether to include subpackages in the TestSuite array, specified as false or true (logical 0 or 1). This property is false by default. You can specify it as true during construction.

'Name'

Character vector or string scalar indicating the name of the suite element. This argument filters TestSuite array elements. 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.

'ParameterName'

Character vector or string scalar indicating the name of a parameter used by the test suite element. 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.

'ParameterProperty'

Character vector or string scalar indicating the name of a property that defines a parameter used by the test suite element. 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.

'ProcedureName'

Name of test procedure, specified as a character vector or string scalar. 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.

In a class-based test, the ProcedureName is the name of the test method. In a function-based, test it is the name of the local function that contains the test. In a script-based test, it is a name generated from the test section title. Unlike Name, the name of the test procedure does not include any class or package name or information about parameterization.

'Superclass'

Name of a class that the test class derives from, specified as a character vector or string scalar. This argument filters TestSuite array elements.

'Tag'

Character vector or string scalar indicating the name of the tag applied to the test suite element. This argument filters TestSuite array elements. This argument filters TestSuite array elements. Use the wildcard character, *, to match any number of characters. Use the question mark character, ?, to match exactly one character.

Output Arguments

suite

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

Attributes

Statictrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Add the matlab.unittest.TestSuite class to the current import list.

import matlab.unittest.TestSuite;
suite = TestSuite.fromPackage('mypackage.subpackage');
result = run(suite)

Run tests in mypackage including all subpackages.

suite = TestSuite.fromPackage('mypackage','IncludingSubpackages',true);
result = run(suite)

In your working folder, create a new package by creating a new folder, +myPackage. In that folder, create the following testZeros.m test file. 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 tests that use the parameter property 'outSize'.

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

suite = TestSuite.fromPackage('myPackage', ...
    HasParameter('Property','outSize'));
{suite.Name}'
ans = 

    'myPackage.testZeros/testClass(type=single,outSize=s2d)'
    'myPackage.testZeros/testClass(type=single,outSize=s3d)'
    'myPackage.testZeros/testClass(type=double,outSize=s2d)'
    'myPackage.testZeros/testClass(type=double,outSize=s3d)'
    'myPackage.testZeros/testClass(type=uint16,outSize=s2d)'
    'myPackage.testZeros/testClass(type=uint16,outSize=s3d)'
    'myPackage.testZeros/testSize(outSize=s2d)'
    'myPackage.testZeros/testSize(outSize=s3d)'

Create the +myPackage folder and testZeros.m class from the previous example.

At the command prompt, create a test suite from all parameterized tests that use the parameter property 'outSize'.

import matlab.unittest.TestSuite;

suite = TestSuite.fromPackage('myPackage', ...
    'ParameterProperty', 'outSize');
{suite.Name}'
ans = 

    'myPackage.testZeros/testClass(type=single,outSize=s2d)'
    'myPackage.testZeros/testClass(type=single,outSize=s3d)'
    'myPackage.testZeros/testClass(type=double,outSize=s2d)'
    'myPackage.testZeros/testClass(type=double,outSize=s3d)'
    'myPackage.testZeros/testClass(type=uint16,outSize=s2d)'
    'myPackage.testZeros/testClass(type=uint16,outSize=s3d)'
    'myPackage.testZeros/testSize(outSize=s2d)'
    'myPackage.testZeros/testSize(outSize=s3d)'

Tips

  • The root folder(s) where package is defined must be on the MATLAB® path when creating suite using this method as well as when suite is run.

Introduced in R2013a