matlab.unittest.TestSuite - Fundamental interface for grouping tests to run - MATLAB (original) (raw)

Main Content

Namespace: 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 ofTestSuite objects.

Creation

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

Methods

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

The method matlab.unittest.TestSuite.fromPackage is now namedmatlab.unittest.TestSuite.fromNamespace. The behavior of this method remains the same, and existing instances ofmatlab.unittest.TestSuite.fromPackage in your code continue to work as expected. There are no plans to remove support for existing references tomatlab.unittest.TestSuite.fromPackage.

The matlab.unittest.TestSuite class has a new static methodmatlab.unittest.TestSuite.fromRequirements that lets you create a test suite from tests that verify requirements. You must have MATLAB® Test™ and Requirements Toolbox™ installed to use this method.

The matlab.unittest.TestSuite class has a new static methodmatlab.unittest.TestSuite.fromProject that lets you create a test suite from the test files in a MATLAB project.