matlab.unittest.TestSuite.fromClass

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

Create suite from TestCase class

Syntax

  • suite = matlab.unittest.TestSuite.fromClass(testClass) example
  • suite = matlab.unittest.TestSuite.fromClass(testClass,s) example
  • suite = matlab.unittest.TestSuite.fromClass(___,Name,Value) example

Description

example

suite = matlab.unittest.TestSuite.fromClass(testClass) creates a TestSuite array from all of the Test methods contained in testClass and returns that array in suite.

example

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

example

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

Tips

  • testClass must be on the MATLAB® path when using this method to create suite, as well as when suite is run.

Input Arguments

testClass

Class containing test methods, specified as a meta.class instance. Use the ? operator to create a meta.class instance. testClass must derive from matlab.unittest.TestCase.

s

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

Name-Value Pair Arguments

'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 a Package Class

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

import matlab.unittest.TestSuite;
suite = TestSuite.fromClass(?mypackage.MyTestClass);
result = run(suite)

Run Tests in a Class Without a Package

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

import matlab.unittest.TestSuite;
suite = TestSuite.fromClass(?MyTestClass);
result = run(suite)

Create Suite of Test Elements Using Selector

In your working 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 the test elements that tests the 'double' data type.

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

suite = TestSuite.fromClass(?testZeros, ...
    HasParameter('Property','type','Name','double'));
{suite.Name}'
ans = 

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

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 the test elements that tests the 'double' data type.

import matlab.unittest.TestSuite;

suite = TestSuite.fromClass(?testZeros, ...
    'ParameterProperty','type', 'ParameterName','double');
{suite.Name}'
ans = 

    'testZeros/testClass(type=double,outSize=s2d)'
    'testZeros/testClass(type=double,outSize=s3d)'
Was this topic helpful?