Main Content

matlab.unittest.qualifications.Verifiable class

Package: 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 package.

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.

Since verifications do not throw an exception, all test content runs to completion even when the test fails. This 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, see Representing Polynomials with Classes.

In a file in your current folder, create the DocPolynomTest class, which tests different methods of the DocPolynom class. To access DocPolynom in your tests, define the addDocPolynomClassToPath method within a TestClassSetup methods block. The method uses a PathFixture instance to add the examples folder, including the DocPolynom class definition file, to the search path. Once the tests run to completion, the testing framework tears down the fixture and restores the path to its previous state.

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

    methods (TestClassSetup)
        function addDocPolynomClassToPath(testCase)
            import matlab.unittest.fixtures.PathFixture
            folder = fullfile(matlabroot, ...
                "help","techdoc","matlab_oop","examples");
            testCase.applyFixture(PathFixture(folder))
        end
    end
end

Now, add your tests to a methods block with the Test attribute. To test the DocPolynom class, use verifications within three Test methods:

  • 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 (TestClassSetup)
        function addDocPolynomClassToPath(testCase)
            import matlab.unittest.fixtures.PathFixture
            folder = fullfile(matlabroot, ...
                "help","techdoc","matlab_oop","examples");
            testCase.applyFixture(PathFixture(folder))
        end
    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.16695 seconds testing time.

Introduced in R2013a