Main Content

assertSuccess

Class: matlab.unittest.TestResult
Package: matlab.unittest

Assert test session ran without failure

Description

example

r = assertSuccess(results) asserts that none of the tests corresponding to the results array failed. If the assertion passes, the method returns results in r. If the assertion fails, MATLAB® throws an exception.

Input Arguments

expand all

Results of running the test suite corresponding to a test session, specified as a matlab.unittest.TestResult array.

Examples

expand all

Run a suite of tests and throw an exception if any of the tests failed.

In your current folder, create a file containing the ExampleTest class. The verifySize qualification causes a test failure.

classdef ExampleTest < matlab.unittest.TestCase
    methods(Test)
        function testOne(testCase)      % Test fails
            testCase.verifySize([1 2 3; 4 5 6],[2 4])
        end
        function testTwo(testCase)      % Test passes
            testCase.verifyClass(@sin,?function_handle)
        end
        function testThree(testCase)    % Test passes
            testCase.assertEqual(7*2,14)
        end
    end
end

At the command prompt, run the tests in ExampleTest and assert that no failing conditions were encountered. MATLAB throws an error because one of the tests failed.

results = assertSuccess(runtests('ExampleTest'));
Running ExampleTest

================================================================================
Verification failed in ExampleTest/testOne.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifySize failed.
    --> The value had an incorrect size.
        
        Actual Size:
             2     3
        Expected Size:
             2     4
    
    Actual Value:
         1     2     3
         4     5     6
    ------------------
    Stack Information:
    ------------------
    In C:\Users\username\Desktop\ExampleTest.m (ExampleTest.testOne) at 4
================================================================================
...
Done ExampleTest
__________

Failure Summary:

     Name                 Failed  Incomplete  Reason(s)
    ==================================================================
     ExampleTest/testOne    X                       Failed by verification.
    
Error using matlab.unittest.internal.BaseTestResult/assertSuccess (line 125)
At least one test failed in the test session.

Tips

  • Use assertSuccess as a qualification step to ensure that your tests do not produce any failures. For example, when you perform an automated build on a continuous integration platform (such as Jenkins™), you can instruct the build to fail if assertSuccess produces an error.

Introduced in R2020a