Main Content

matlab.unittest.qualifications.Verifiable Class

Namespace: matlab.unittest.qualifications

Qualification to produce soft-failure conditions

Description

The Verifiable class provides a qualification to produce soft-failure conditions. Apart from actions performed for failures, the Verifiable class works the same as other qualification classes in the matlab.unittest.qualifications namespace.

Upon a verification failure, the Verifiable class informs the testing framework of the failure, including all diagnostic information associated with the failure, but continues to execute the currently running test without throwing an exception. This behavior is most useful when a failure at the verification point is not fatal to the remaining test content. Often, you use verifications as the primary qualification type within a Four-Phase Test. Use other qualification types, such as assumptions, assertions, and fatal assertions to test for violation of preconditions or incorrect test setup.

Because verifications do not throw an exception, all test content runs to completion even when the test fails. This behavior helps you understand how close a piece of software is to meeting the test suite requirements. Qualification types that throw exceptions do not provide this insight, because once an exception is thrown, an arbitrary amount of code remains that is not reached or exercised. Verifications also provide more testing coverage in failure conditions. However, if you overuse verifications, they can produce excess noise for a single failure. If a failure will cause later qualification points to also fail, use assertions or fatal assertions instead.

The matlab.unittest.qualifications.Verifiable class is a handle class.

Methods

expand all

Events

Event NameTriggerEvent DataEvent Attributes
VerificationFailedTriggered upon failing verification. A QualificationEventData object is passed to listener callback functions.matlab.unittest.qualifications.QualificationEventData

NotifyAccess: private

ListenAccess: public

VerificationPassedTriggered upon passing verification. A QualificationEventData object is passed to listener callback functions.matlab.unittest.qualifications.QualificationEventData

NotifyAccess: private

ListenAccess: public

Examples

collapse all

Use verifications to test the DocPolynom class, which represents polynomials in MATLAB®. For more information about the class and to view the class code, see Representing Polynomials with Classes.

In a file named DocPolynom.m in your current folder, create the DocPolynom class. To test different methods of the DocPolynom class, create the DocPolynomTest class in another file named DocPolynomTest.m in your current folder. Define three Test methods in the DocPolynomTest class:

  • testConstructor method — Use verifyClass to test the DocPolynom class constructor.

  • testAddition method — Use verifyEqual to test the addition of DocPolynom objects.

  • testMultiplication method — Use verifyEqual to test the multiplication of DocPolynom objects.

classdef DocPolynomTest < matlab.unittest.TestCase
    properties
        TextToDisplay = "Equation under test: "
    end

    methods (Test)
        function testConstructor(testCase)
            p = DocPolynom([1 0 1]);
            testCase.verifyClass(p,?DocPolynom)
        end

        function testAddition(testCase)
            p1 = DocPolynom([1 0 1]);
            p2 = DocPolynom([5 2]);
            actual = p1 + p2;
            expected = DocPolynom([1 5 3]);
            diagnostic = [testCase.TextToDisplay ...
                "(x^2 + 1) + (5*x + 2) = x^2 + 5*x + 3"];
            testCase.verifyEqual(actual,expected,diagnostic)
        end

        function testMultiplication(testCase)
            p1 = DocPolynom([1 0 3]);
            p2 = DocPolynom([5 2]);
            actual = p1 * p2;
            expected = DocPolynom([5 2 15 6]);
            diagnostic = [testCase.TextToDisplay ...
                "(x^2 + 3) * (5*x + 2) = 5*x^3 + 2*x^2 + 15*x + 6"];
            testCase.verifyEqual(actual,expected,diagnostic)
        end
    end
end

Run the tests in the DocPolynomTest class. In this example, all the tests pass.

runtests("DocPolynomTest")
Running DocPolynomTest
...
Done DocPolynomTest
__________
ans = 
  1×3 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   3 Passed, 0 Failed, 0 Incomplete.
   0.26915 seconds testing time.

Version History

Introduced in R2013a