Main Content

matlab.unittest.TestCase.forInteractiveUse

Class: matlab.unittest.TestCase
Namespace: matlab.unittest

Create test case for interactive use

Description

testCase = matlab.unittest.TestCase.forInteractiveUse creates a test case configured for interactive testing. The returned TestCase instance is suited for experimentation at the command prompt. It reacts to qualifications by printing messages to the screen for both passing and failing events.

example

testCase = matlab.unittest.TestCase.forInteractiveUse(testClass) creates an instance of the specified test class for interactive testing.

example

testCase = matlab.unittest.TestCase.forInteractiveUse(testClass,ApplySharedTestFixtures=tf) also specifies whether to apply any shared test fixtures associated with the test class for interactive testing. If tf is true, the method sets up the shared test fixtures when it creates an interactive test case from testClass. The testing framework automatically tears down these fixtures when the test case goes out of scope. Shared test fixtures are specified using the SharedTestFixtures attribute of TestCase subclasses. (since R2024a)

example

Input Arguments

expand all

Test class deriving from matlab.unittest.TestCase, specified as a matlab.metadata.Class instance.

Example: ?ExampleTest

Since R2024a

Option to apply the shared test fixtures associated with testClass, specified as a numeric or logical 0 (false) or 1 (true). By default, the method ignores shared test fixtures when creating an interactive test case from a test class.

Attributes

Statictrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Test if the actual value contains the specified substring.

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Define the actual value.

actual = "This is a long message.";

Verify that actual contains the text "long".

verifySubstring(testCase,actual,"long")
Verification passed.

Show that case matters. This test fails because actual does not contain "Long".

verifySubstring(testCase,actual,"Long","Test is case sensitive.")
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Test is case sensitive.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifySubstring failed.
    --> The value does not contain the substring.
    
    Actual Value:
        "This is a long message."
    Expected Substring:
        "Long"
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestForSubstringsExample.m (TestForSubstringsExample) at 22

Show that the test fails if the substring is longer than the actual string.

verifySubstring(testCase,actual,"This is a long message with extra words.")
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifySubstring failed.
    --> The value does not contain the substring.
    
    Actual Value:
        "This is a long message."
    Expected Substring:
        "This is a long message with extra words."
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestForSubstringsExample.m (TestForSubstringsExample) at 27

Run a Test method of a test class interactively.

In a file named ZerosTest.m in your current folder, create the ZerosTest class, which tests 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 an instance of the ZerosTest class for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse(?ZerosTest);

Use the test case to call the testSize method interactively. The test passes.

testCase.testSize([5 10])
Verification passed.

Since R2024a

Use shared test fixtures when you run a Test method of a test class interactively.

This example assumes that your current folder contains a subfolder named helperFiles. Create the subfolder if it does not exist.

[~,~] = mkdir("helperFiles")

In a file in your current folder, create the SampleTest test class that uses two shared test fixtures. For illustration purposes, in this example, the Test methods access the fixtures to perform their qualifications.

classdef (SharedTestFixtures={ ...
        matlab.unittest.fixtures.PathFixture("helperFiles"), ...
        matlab.unittest.fixtures.TemporaryFolderFixture}) ...
        SampleTest < matlab.unittest.TestCase
    methods (Test)
        function testFixtureCount(testCase)
            % Test the number of shared test fixtures
            f = testCase.getSharedTestFixtures;
            testCase.verifyNumElements(f,2)
        end

        function testPath(testCase)
            % Test the search path
            import matlab.unittest.constraints.ContainsSubstring
            f = testCase.getSharedTestFixtures( ...
                "matlab.unittest.fixtures.PathFixture");
            testCase.verifyThat(path,ContainsSubstring(f.Folder))
        end

        function testTempFolder(testCase)
            % Test writing to the temporary folder
            import matlab.unittest.constraints.IsFile
            f = testCase.getSharedTestFixtures( ...
                "matlab.unittest.fixtures.TemporaryFolderFixture");
            tempFolderName = f.Folder;
            filename = string(tempFolderName) + filesep + "myFile.dat";
            writematrix(magic(20),filename)
            testCase.verifyThat(filename,IsFile)
        end
    end
end

Create an instance of the SampleTest class for interactive testing. Because the tests in SampleTest rely on the shared test fixtures, apply the fixtures when creating a test case.

testCase = matlab.unittest.TestCase.forInteractiveUse(?SampleTest, ...
    ApplySharedTestFixtures=true);

Use the test case to call the testFixtureCount method interactively. The test passes because the shared test fixtures are available to the test. If you create a test case without applying the fixtures, the test fails.

testCase.testFixtureCount
Verification passed.

Version History

Introduced in R2014a

expand all