Main Content

matlab.unittest.TestSuite class

Package: matlab.unittest

Fundamental interface for grouping tests to run

Description

The matlab.unittest.TestSuite class is the fundamental interface used to group tests in the testing framework. The test runner operates on arrays of TestSuite objects.

Creation

Create TestSuite arrays by using static methods of the TestSuite class. You also can create a test suite by using the testsuite function.

Methods

expand all

Examples

collapse all

Create different test suites and then concatenate the suites.

In a file named eyeTest.m in your current folder, create a function-based test to test the eye function.

function tests = eyeTest
tests = functiontests(localfunctions);
end

function doubleClassTest(testCase)
actual = eye;
verifyClass(testCase,actual,"double")
end

function singleClassTest(testCase)
actual = eye("single");
verifyClass(testCase,actual,"single")
end

function uint16ClassTest(testCase)
actual = eye("uint16");
verifyClass(testCase,actual,"uint16")
end

function sizeTest(testCase)
expected = [7 13];
actual = eye(expected);
verifySize(testCase,actual,expected)
end

function valueTest(testCase)
actual = eye(42);
verifyEqual(testCase,unique(diag(actual)),1)    % Diagonal values must be 1
verifyEqual(testCase,unique(triu(actual,1)),0)  % Upper triangular values must be 0
verifyEqual(testCase,unique(tril(actual,-1)),0) % Lower triangular values must be 0
end

In another file named ZerosTest.m in your current folder, create a class-based test to test the zeros function.

classdef ZerosTest < matlab.unittest.TestCase
    properties (TestParameter)
        type = {'single','double','uint16'};
        size = struct("s2d",[3 3],"s3d",[2 5 4]);
    end
    
    methods (Test)
        function testClass(testCase,size,type)
            testCase.verifyClass(zeros(size,type),type)
        end
        
        function testSize(testCase,size)
            testCase.verifySize(zeros(size),size)
        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

Create a test suite from the function-based test file.

import matlab.unittest.TestSuite
suite1 = TestSuite.fromFile("eyeTest.m");

Create a test suite from the ZerosTest test class, including only the tests that are parameterized.

suite2 = TestSuite.fromClass(?ZerosTest,"ParameterProperty","*");

Concatenate the test suites and run the resulting suite. All the tests pass.

fullSuite = [suite1 suite2];
results = run(fullSuite);
Running eyeTest
.....
Done eyeTest
__________

Running ZerosTest
........
Done ZerosTest
__________

Version History

Introduced in R2013a

expand all