Main Content

run

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

Run tests corresponding to test case

Description

example

result = run(testCase) runs all the Test methods of the class defining testCase and returns the result of the test run as a matlab.unittest.TestResult array, where each TestResult object corresponds to a Test method.

The run method is a convenience method for interactive experimentation with TestCase subclasses. It runs the tests by using a TestRunner instance that is configured for text output. The text output includes test run progress as well as diagnostics in the event of test failures.

example

result = run(testCase,method) runs the specified Test method of testCase.

Input Arguments

expand all

Test case, specified as a matlab.unittest.TestCase object.

Identifier of the Test method of testCase to run, specified as a string scalar, character vector, or matlab.metadata.Method instance.

Attributes

Sealedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Test the properties of a figure by creating and running a test class.

In a file named FigurePropertiesTest.m in your current folder, create the FigurePropertiesTest test class by:

  • Subclassing the matlab.unittest.TestCase class

  • Adding a property to represent the figure to test

  • Adding setup and teardown code for each test in a TestMethodSetup methods block

  • Adding tests in a Test methods block

classdef FigurePropertiesTest < matlab.unittest.TestCase
    properties
        TestFigure
    end

    methods (TestMethodSetup)
        function createFigure(testCase)
            testCase.TestFigure = figure;
            testCase.addTeardown(@close,testCase.TestFigure)
        end
    end

    methods (Test)
        function defaultCurrentPoint(testCase)
            cp = testCase.TestFigure.CurrentPoint;
            testCase.verifyEqual(cp,[0 0], ...
                "Default current point must be [0 0].")
        end

        function defaultCurrentObject(testCase)
            import matlab.unittest.constraints.IsEmpty
            co = testCase.TestFigure.CurrentObject;
            testCase.verifyThat(co,IsEmpty, ...
                "Default current object must be empty.")
        end
    end
end

To run the tests interactively, create a test case from the test class. You also can use the forInteractiveUse static method to create a test case.

testCase = FigurePropertiesTest;

Run the tests corresponding to the test case. Both of the tests pass.

result1 = run(testCase)
Running FigurePropertiesTest
.
.
Done FigurePropertiesTest
__________
result1 = 
  1×2 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   2 Passed, 0 Failed, 0 Incomplete.
   0.10772 seconds testing time.

Now, run the defaultCurrentPoint Test method.

result2 = run(testCase,"defaultCurrentPoint")
Running FigurePropertiesTest
.
Done FigurePropertiesTest
__________
result2 = 
  TestResult with properties:

          Name: 'FigurePropertiesTest/defaultCurrentPoint'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 0.0553
       Details: [1×1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   0.055328 seconds testing time.

Version History

Introduced in R2013a