Main Content

matlab.unittest.Test class

Package: matlab.unittest
Superclasses: matlab.unittest.TestSuite

Specification of single test method

Description

The matlab.unittest.Test class holds the information required by the TestRunner object to run a single Test method of a TestCase class. A scalar Test instance is the fundamental element contained in TestSuite arrays. A simple array of Test instances is a commonly used form of a TestSuite array.

Properties

Name

Name of the Test element.

ProcedureName

Name of the test procedure that runs for the Test element. 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 the Name property, ProcedureName does not include any class or package name or information about parameterization.

TestClass

Name of the test class for the TestCase. If a Test element is not a class-based test, then TestClass is an empty string.

BaseFolder

Name of the folder that contains the file defining the test content. For tests defined in packages, the base folder is the parent of the top-level package folder.

Parameterization

Row vector of parameters required for the test. The Parameterization property contains all the parameterized data required by the TestRunner.

SharedTestFixtures

Row vector of fixtures required for the test. The SharedTestFixtures property contains all the fixtures specified by the SharedTestFixtures class-level attribute of the TestCase class.

Tags

Cell array of tags applied to the Test element.

Examples

collapse all

In your current folder, create a file named BankAccountTest.m containing the following test class.

classdef BankAccountTest < matlab.unittest.TestCase
    
    methods (TestClassSetup)
        function addBankAccountClassToPath(testCase)
            p = path;
            testCase.addTeardown(@path,p);
            addpath(fullfile(matlabroot,'help','techdoc','matlab_oop',...
                'examples'));
        end
    end
    
    methods (Test)
        function testConstructor(testCase)
            b = BankAccount(1234, 100);
            testCase.verifyEqual(b.AccountNumber, 1234, ...
                'Constructor failed to correctly set account number');
            testCase.verifyEqual(b.AccountBalance, 100, ...
                'Constructor failed to correctly set account balance');
        end
        
        function testConstructorNotEnoughInputs(testCase)
            import matlab.unittest.constraints.Throws;
            testCase.verifyThat(@()BankAccount, ...
                Throws('MATLAB:minrhs'));
        end
        
        function testDesposit(testCase)
            b = BankAccount(1234, 100);
            b.deposit(25);
            testCase.verifyEqual(b.AccountBalance, 125);
        end
        
        function testWithdraw(testCase)
            b = BankAccount(1234, 100);
            b.withdraw(25);
            testCase.verifyEqual(b.AccountBalance, 75);
        end
        
        function testNotifyInsufficientFunds(testCase)
            callbackExecuted = false;
            function testCallback(~,~)
                callbackExecuted = true;
            end
            
            b = BankAccount(1234, 100);
            b.addlistener('InsufficientFunds', @testCallback);
            
            b.withdraw(50);
            testCase.assertFalse(callbackExecuted, ...
                'The callback should not have executed yet');
            b.withdraw(60);
            testCase.verifyTrue(callbackExecuted, ...
                'The listener callback should have fired');
        end
    end
end

Create a suite of Test objects from all Test methods in the BankAccountTest class.

import matlab.unittest.TestSuite 
suite = TestSuite.fromClass(?BankAccountTest); 
whos suite
  Name       Size            Bytes  Class                   Attributes

  suite      1x5             10502  matlab.unittest.Test   

Each test is a matlab.unittest.Test object.

Display the Test element names.

{suite.Name}'
ans =

  5×1 cell array

    {'BankAccountTest/testConstructor'               }
    {'BankAccountTest/testConstructorNotEnoughInputs'}
    {'BankAccountTest/testDesposit'                  }
    {'BankAccountTest/testWithdraw'                  }
    {'BankAccountTest/testNotifyInsufficientFunds'   }