Main Content

matlab.unittest.TestCase.forInteractiveUse

Class: matlab.unittest.TestCase
Package: matlab.unittest

Create TestCase for interactive use

Description

example

tc = matlab.unittest.TestCase.forInteractiveUse creates a TestCase instance for interactive use. The TestCase is configured for experimentation at the command prompt. It reacts to qualification tests by printing messages to the screen for both passing and failing conditions.

example

tc = matlab.unittest.TestCase.forInteractiveUse(testclass) creates an instance of the testclass class for interactive use.

Input Arguments

testclass

meta.class instance that describes a matlab.unittest.TestCase subclass

Examples

expand all

Create a TestCase for interactive use.

import matlab.unittest.TestCase;
testCase = TestCase.forInteractiveUse;

Produce a passing verification.

testCase.verifyTrue(true, 'true should be true');
Interactive verification passed.

Produce a failing verification.

testCase.verifyTrue(false);
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
verifyTrue failed.
--> The value must evaluate to "true".

Actual Value:
         0

In a file in your working folder, create ExampleTest.m. This class is a subclass of TestCase and provides a helper verification method, verifySameSize.

classdef ExampleTest < matlab.unittest.TestCase
    methods
        function verifySameSize(testCase, actual, expected)
            import matlab.unittest.constraints.ReturnsTrue;
            
            diagnostic = ['Actual and expected value sizes do not match.'...
                '\nActual size: ' num2str(size(actual)) ...
                '\nExpected size: ' num2str(size(expected))];
            
            testCase.verifyThat(@()isequal(size(actual),size(expected)),...
                ReturnsTrue, sprintf(diagnostic));
        end
    end
end

At the command prompt, create an interactive test case from the ExampleTest class.

tc = matlab.unittest.TestCase.forInteractiveUse(?ExampleTest);

Use the test case at the command prompt to call the verifySameSize method interactively.

tc.verifySameSize(1:10,5)
Interactive verification failed.

----------------
Test Diagnostic:
----------------
Actual and expected value sizes do not match.
Actual size: 1  10
Expected size: 1  1

---------------------
Framework Diagnostic:
---------------------
ReturnsTrue failed.
--> The function handle should have evaluated to "true".
--> Returned value:
             0

Actual function_handle:
        @()isequal(size(actual),size(expected))
Introduced in R2014a