matlab.unittest.qualifications.FatalAssertable - Qualification to abort test execution - MATLAB (original) (raw)

Namespace: matlab.unittest.qualifications

Qualification to abort test execution

Description

The FatalAssertable class provides a qualification to abort test execution. Apart from actions performed for failures, the FatalAssertable class works the same as other qualification classes in the matlab.unittest.qualifications namespace.

Upon a fatal assertion failure, the FatalAssertable class informs the testing framework of the failure by throwing a FatalAssertionFailedException object. The framework then displays diagnostic information for the failure and aborts the test session. This behavior is useful when the failure is so fundamental that continuing testing does not make sense. Also, you can use fatal assertions in fixture teardown to guarantee that the environment state is restored correctly. If you can make the fixture teardown exception safe and restore the state after failure, use assertions instead.

Fatal assertions prevent false test failures due to the failure of a fundamental test. They also prevent false test failures when a prior test fails to restore the state. If the framework cannot properly tear down fixtures, you must manually reset the state. For example, you might need to restart MATLAB®.

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

Methods

expand all

The FatalAssertable class provides several qualification methods for testing values and responding to failures. For example, fatalAssertEmpty tests that a value is empty, and fatalAssertTrue tests that the actual value is true.

Note

The methods of the FatalAssertable class correspond to the methods of the Verifiable class. They differ only in terms of qualification type. You can call the FatalAssertable methods in the same way you call the Verifiable methods.

General Purpose

fatalAssertEqual fatalAssertEqual(testCase,actual,expected,diagnostic,Name,Value)Fatally assert that actual is strictly equal toexpected. Similar to verifyEqual.Input Arguments testCase — Test case.actual — Value to test.expected — Expected value.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.Name-Value Arguments AbsTol — Absolute tolerance, specified as a numeric array.RelTol — Relative tolerance, specified as a numeric array.
fatalAssertFail fatalAssertFail(testCase,diagnostic)Produce an unconditional fatal assertion failure. Similar to verifyFail.Input Arguments testCase — Test case.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertFalse fatalAssertFalse(testCase,actual,diagnostic)Fatally assert that the value of actual is logical0 (false). Similar to verifyFalse.Input Arguments testCase — Test case.actual — Value to test.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertNotEqual fatalAssertNotEqual(testCase,actual,prohibited,diagnostic)Fatally assert that actual is not equal toprohibited. Similar to verifyNotEqual.Input Arguments testCase — Test case.actual — Value to test.prohibited — Value to compare against.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertNotSameHandle fatalAssertNotSameHandle(testCase,actual,prohibited,diagnostic)Fatally assert that actual is not the same as the prohibited handle array. Similar to verifyNotSameHandle.Input Arguments testCase — Test case.actual — Value to test.prohibited — Value to compare against, specified as a handle array.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertReturnsTrue fatalAssertReturnsTrue(testCase,actual,diagnostic)Fatally assert that actual is a function handle that returns the logical scalar 1 (true). Similar toverifyReturnsTrue.Input Arguments testCase — Test case.actual — Value to test.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertSameHandle fatalAssertSameHandle(testCase,actual,expected,diagnostic)Fatally assert that actual is the same as the expected handle array. Similar to verifySameHandle.Input Arguments testCase — Test case.actual — Value to test.expected — Expected value, specified as a handle array.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertThat fatalAssertThat(testCase,actual,constraint,diagnostic)Fatally assert that actual is a value that satisfies the specified constraint. Similar to verifyThat.Input Arguments testCase — Test case.actual — Value to test.constraint — Constraint that the actual value must satisfy, specified as an instance of the matlab.unittest.constraints.Constraint class.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertTrue fatalAssertTrue(testCase,actual,diagnostic)Fatally assert that the value of actual is logical1 (true). Similar to verifyTrue.Input Arguments testCase — Test case.actual — Value to test.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.

Errors and Warnings

fatalAssertError [output1,...,outputN] = fatalAssertError(testCase,actual,identifier,diagnostic)Fatally assert that actual is a function handle that throws the exception specified by identifier. Similar to verifyError.Input Arguments testCase — Test case.actual — Value to test.identifier — Error identifier, specified as a string scalar, character vector, or matlab.metadata.Class instance.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.Output Arguments output1,...,outputN — Output arguments, 1 through N, requested from actual.
fatalAssertWarning [output1,...,outputN] = fatalAssertWarning(testCase,actual,identifier,diagnostic)Fatally assert that actual is a function handle that issues the warning specified by identifier. Similar to verifyWarning.Input Arguments testCase — Test case.actual — Value to test.identifier — Warning identifier, specified as a character vector, cell array of character vectors, or string array.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.Output Arguments output1,...,outputN — Output arguments, 1 through N (if any), produced when actual is invoked.
fatalAssertWarningFree [output1,...,outputN] = fatalAssertWarningFree(testCase,actual,diagnostic)Fatally assert that actual is a function handle that issues no warnings. Similar to verifyWarningFree.Input Arguments testCase — Test case.actual — Value to test.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.Output Arguments output1,...,outputN — Output arguments, 1 through N (if any), produced when actual is invoked.

Inequalities

fatalAssertGreaterThan fatalAssertGreaterThan(testCase,actual,floor,diagnostic)Fatally assert that all elements of actual are greater than all elements of floor. Similar to verifyGreaterThan.Input Arguments testCase — Test case.actual — Value to test.floor — Minimum value.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertGreaterThanOrEqual fatalAssertGreaterThanOrEqual(testCase,actual,floor,diagnostic)Fatally assert that all elements of actual are greater than or equal to all elements of floor. Similar to verifyGreaterThanOrEqual.Input Arguments testCase — Test case.actual — Value to test.floor — Minimum value.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertLessThan fatalAssertLessThan(testCase,actual,ceiling,diagnostic)Fatally assert that all elements of actual are less than all elements of ceiling. Similar to verifyLessThan.Input Arguments testCase — Test case.actual — Value to test.ceiling — Maximum value.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertLessThanOrEqual fatalAssertLessThanOrEqual(testCase,actual,ceiling,diagnostic)Fatally assert that all elements of actual are less than or equal to all elements of ceiling. Similar to verifyLessThanOrEqual.Input Arguments testCase — Test case.actual — Value to test.ceiling — Maximum value.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.

Array Size

fatalAssertEmpty fatalAssertEmpty(testCase,actual,diagnostic)Fatally assert that actual is an empty MATLAB array. Similar to verifyEmpty.Input Arguments testCase — Test case.actual — Value to test.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertLength fatalAssertLength(testCase,actual,expectedLength,diagnostic)Fatally assert that actual is a MATLAB array with the expected length. Similar to verifyLength.Input Arguments testCase — Test case.actual — Value to test.expectedLength — Expected array length.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertNotEmpty fatalAssertNotEmpty(testCase,actual,diagnostic)Fatally assert that actual is a nonempty MATLAB array. Similar to verifyNotEmpty.Input Arguments testCase — Test case.actual — Value to test.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertNumElements fatalAssertNumElements(testCase,actual,expectedNumElements,diagnostic)Fatally assert that actual is a MATLAB array with the expected number of elements. Similar to verifyNumElements.Input Arguments testCase — Test case.actual — Value to test.expectedNumElements — Expected number of elements.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertSize fatalAssertSize(testCase,actual,expectedSize,diagnostic)Fatally assert that actual is a MATLAB array with the expected size. Similar to verifySize.Input Arguments testCase — Test case.actual — Value to test.expectedSize — Expected array size.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.

Type

fatalAssertClass fatalAssertClass(testCase,actual,class,diagnostic)Fatally assert that the class of actual is the specified class. Similar to verifyClass.Input Arguments testCase — Test case.actual — Value to test.class — Expected class, specified as a string scalar, character vector, or matlab.metadata.Class instance.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertInstanceOf fatalAssertInstanceOf(testCase,actual,class,diagnostic)Fatally assert that actual is an instance of the specified class. Similar to verifyInstanceOf.Input Arguments testCase — Test case.actual — Value to test.class — Expected class, specified as a string scalar, character vector, or matlab.metadata.Class instance.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.

Strings

fatalAssertMatches fatalAssertMatches(testCase,actual,expression,diagnostic)Fatally assert that actual is a string scalar or character vector that matches the specified regular expression. Similar to verifyMatches.Input Arguments testCase — Test case.actual — Value to test.expression — Regular expression to match.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.
fatalAssertSubstring fatalAssertSubstring(testCase,actual,substring,diagnostic)Fatally assert that actual is a string scalar or character vector that contains substring. Similar to verifySubstring.Input Arguments testCase — Test case.actual — Value to test.substring — Text contained within actual value.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.

Baselines

fatalAssertEqualsBaseline (requires MATLAB Test™) fatalAssertEqualsBaseline(testCase,actual,baseline,diagnostic,Name=Value)Fatally assert that actual is strictly equal to the baseline data represented by baseline. Similar to verifyEqualsBaseline (MATLAB Test).Input Arguments testCase — Test case.actual — Value to test.baseline — Representation of the baseline data, specified as a matlabtest.baselines.MATFileBaseline object.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics.Name-Value Arguments AbsTol — Absolute tolerance, specified as a nonnegative real scalar.RelTol — Relative tolerance, specified as a nonnegative real scalar.

Events

Examples

collapse all

Test a function that sets the value of an operating system environment variable. If the environment variable cannot be reset to its original value after the test, abort the test session using a fatal assertion failure.

In a file in your current folder, create the setUserName function. The function uses a call to setenv to set the 'UserName' environment variable.

function setUserName(name) setenv('UserName',name) end

To test the setUserName function, create a test class named SetUserNameTest in your current folder. Define the necessary class members for your test:

classdef SetUserNameTest < matlab.unittest.TestCase properties (SetAccess = private) OriginalUserName end

methods (Test)
    function testUpdate(testCase)
        testCase.OriginalUserName = getenv('UserName');
        setUserName('David')
        testCase.addTeardown(@() testCase.resetUserName)
        testCase.verifyEqual(getenv('UserName'),'David')
    end
end

methods (Access = private)
    function resetUserName(testCase)
        setUserName(testCase.OriginalUserName)
        testCase.fatalAssertEqual(getenv('UserName'),testCase.OriginalUserName)
    end
end

end

Run the SetUserNameTest class. The test passes.

runtests("SetUserNameTest")

Running SetUserNameTest . Done SetUserNameTest


ans = TestResult with properties:

      Name: 'SetUserNameTest/testUpdate'
    Passed: 1
    Failed: 0
Incomplete: 0
  Duration: 0.0226
   Details: [1×1 struct]

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

More About

expand all

Depending on the test runner configuration, the testing framework might display diagnostics when a qualification passes or fails. By default, the framework displays diagnostics only when the qualification fails. You can override the default behavior by customizing the test runner. For example, you can use a DiagnosticsOutputPlugin instance to display both failing and passing event diagnostics.

To add a diagnostic message to a test case, use the optionaldiagnostic argument in any of the qualification methods. You can specify diagnostic as a string array, character array, function handle, or array of matlab.automation.diagnostics.Diagnostic objects.

Test content sometimes changes the state of its environment. The content is exception safe when the environment can be restored to its original state, even in the presence of exceptions. Exception safety ensures that a test that throws an exception does not affect subsequent tests by corrupting the environment. To achieve exception safety, perform all teardown actions using the addTeardown method. Call addTeardown immediately before or after the original state change, without any other code in between that can throw an exception.

For example, this code is not exception safe. If the test fails, the testing framework does not close the figure. The presence of this figure might cause subsequent tests to fail.

% Not exception safe f = figure; testCase.fatalAssertEqual(actual,expected) close(f)

On the other hand, this code is exception safe, because the framework closes the figure regardless of the test outcome.

% Exception safe f = figure; testCase.addTeardown(@close,f) testCase.fatalAssertEqual(actual,expected)

Tearing down a fixture using addTeardown does not guarantee that code is exception safe. This code is not exception safe, because the call toaddTeardown is after the test. If the test fails, the framework does not close the figure.

% Not exception safe f = figure; testCase.fatalAssertEqual(actual,expected) testCase.addTeardown(@close,f)

Version History

Introduced in R2013a

expand all

If you have a MATLAB Test license, you can use the fatalAssertEqualsBaseline method to test if a value is equal to baseline data.

See Also

Classes

Topics