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 in the matlab.unittest.selectors package. If you have MATLAB Test™ installed, you also can specify s as a matlabtest.selectors.DependsOn object.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

ExternalParameters

External parameters, specified as an array of matlab.unittest.parameters.Parameter objects. 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.

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 match 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.

DependsOn

Names of the files and folders that contain source code, specified as a string vector, character vector, or cell vector of character vectors. This argument filters the test suite. For the testing framework to include a test in the filtered suite, the file that defines the test must depend on the specified source code. If none of the test files depend on the source code, an empty test suite is returned.

The specified value must represent at least one existing file with a .m, .p, .mlx, .mlapp, .mat, or .slx extension. You cannot explicitly specify a filename with an unsupported extension. If you specify a folder name, the framework extracts the paths to the supported files within the folder.

You must have MATLAB Test installed to use DependsOn. For more information about selecting tests by source code dependency, see matlabtest.selectors.DependsOn (MATLAB Test).

Example: ["myFile.m" "myFolder"]

Example: ["folderA" "C:\work\folderB"]

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 have 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 have 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, MATLAB generates parameter names from the elements of the cell array by taking into account their values, types, and dimensions.

  • 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 have 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 have 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 match 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 have 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')).

Version History

Introduced in R2013a

expand all