Main Content

matlab.unittest.TestSuite.fromFile

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

Create TestSuite array from test file

Description

example

suite = matlab.unittest.TestSuite.fromFile(file) creates a TestSuite array from all of the tests 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 tests 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

Absolute or relative path to the test file, 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 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 quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

BaseFolder

Name of the folder that contains the file defining the tests, specified as a string array, character vector, or cell array of character vectors. This argument filters TestSuite array elements. For the testing framework to include a test in the suite, the Test element must be contained in one of the base folders specified by BaseFolder. If none of the Test elements matches a base folder, an empty test suite is returned. Use the wildcard character * to match any number of characters. Use the question mark character ? to match a single 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.

Name

Name of the suite element, specified as a string array, character vector, or cell array of character vectors. This argument filters TestSuite array elements. For the testing framework to include a test in the suite, the Name property of the Test element must match one of the names specified by Name. If none of the Test elements has a matching name, an empty test suite is returned. Use the wildcard character * to match any number of characters. Use the question mark character ? to match a single character.

ParameterProperty

Name of a test class property that defines a parameter used by the test suite element, specified as a string array, character vector, or cell array of character vectors. This argument filters TestSuite array elements. For the testing framework to include a test in the suite, the Parameterization property of the Test element must contain at least one of the property names specified by ParameterProperty. If none of the Test elements has a matching property name, an empty test suite is returned. Use the wildcard character * to match any number of characters. Use the question mark character ? to match to a single character.

ParameterName

Name of a parameter used by the test suite element, specified as a string array, character vector, or cell array of character vectors. MATLAB generates parameter names based on the test class property that defines the parameters:

  • If the property value is a cell array of character vectors, MATLAB generates parameter names from the values in the cell array. Otherwise, MATLAB specifies parameter names as value1, value2, …, valueN.

  • If the property value is a structure, MATLAB generates parameter names from the structure fields.

The ParameterName argument filters TestSuite array elements. For the testing framework to include a test in the suite, the Parameterization property of the Test element must contain at least one of the parameter names specified by ParameterName. If none of the Test elements has a matching parameter name, an empty test suite is returned. Use the wildcard character * to match any number of characters. Use the question mark character ? to match a single character.

ProcedureName

Name of the test procedure, specified as a string array, character vector, or cell array of character vectors. This argument filters TestSuite array elements. For the testing framework to include a test in the suite, the ProcedureName property of the Test element must match one of the procedure names specified by ProcedureName. If none of the Test elements has a matching procedure name, an empty test suite is returned. Use the wildcard character * to match any number of characters. Use the question mark character ? to match a single 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 the class that the test class derives from, specified as a string array, character vector, or cell array of character vectors. This argument filters TestSuite array elements. For the testing framework to include a test in the suite, the TestClass property of the Test element must point to a test class that derives from one of the classes specified by Superclass. If none of the Test elements matches a class, an empty test suite is returned.

Tag

Name of a test tag used by the test suite element, specified as a string array, character vector, or cell array of character vectors. This argument filters TestSuite array elements. For the testing framework to include a test in the suite, the Tags property of the Test element must contain at least one of the tag names specified by Tag. If none of the Test elements has a matching tag name, an empty test suite is returned. Use the wildcard character * to match any number of characters. Use the question mark character ? to match a single 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

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
__________

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 = 

  1×8 Test array with properties:

    Name
    ProcedureName
    TestClass
    BaseFolder
    Parameterization
    SharedTestFixtures
    Tags

Tests Include:
   5 Unique Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.

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 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')).

Introduced in R2013a