matlab.unittest.qualifications.Assumable - Qualification to filter test content - MATLAB (original) (raw)
Namespace: matlab.unittest.qualifications
Qualification to filter test content
Description
The Assumable
class provides a qualification to filter test content. Apart from actions performed for failures, the Assumable
class works the same as other qualification classes in the matlab.unittest.qualifications namespace.
Upon an assumption failure, the Assumable
class informs the testing framework of the failure by throwing an AssumptionFailedException object. The framework then marks the current test content as filtered and continues testing. Assumptions ensure that a test is run only when certain preconditions are met, but running the test without satisfying the preconditions does not produce a test failure. If the unmet preconditions are meant to produce a test failure, use assertions instead of assumptions.
When an assumption failure is produced within a method of the TestCase class, the type of the method determines which tests are filtered:
Test
method — The framework marks the entireTest
method as filtered.TestMethodSetup
orTestMethodTeardown
method — The framework marks theTest
method to run for that method instance as filtered.TestClassSetup
orTestClassTeardown
method — The framework marks the entire test class as filtered.
When you use assumptions, ensure that your test content is exception safe. Because assumptions do not produce test failures, portions of your test code might silently be filtered. To avoid the creation of dead test code, consider monitoring your filtered tests.
The matlab.unittest.qualifications.Assumable
class is a handle class.
Methods
The Assumable
class provides several qualification methods for testing values and responding to failures. For example, assumeEmpty
tests that a value is empty, and assumeTrue
tests that the actual value is true.
Note
The methods of the Assumable
class correspond to the methods of theVerifiable class. They differ only in terms of qualification type. You can call the Assumable
methods in the same way you call the Verifiable
methods.
General Purpose
assumeEqual | assumeEqual(testCase,actual,expected,diagnostic,Name,Value)Assume 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. |
---|---|
assumeFail | assumeFail(testCase,diagnostic)Produce an unconditional assumption failure. Similar to verifyFail.Input Arguments testCase — Test case.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics. |
assumeFalse | assumeFalse(testCase,actual,diagnostic)Assume that the value of actual is logical 0 (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. |
assumeNotEqual | assumeNotEqual(testCase,actual,prohibited,diagnostic)Assume 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. |
assumeNotSameHandle | assumeNotSameHandle(testCase,actual,prohibited,diagnostic)Assume 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. |
assumeReturnsTrue | assumeReturnsTrue(testCase,actual,diagnostic)Assume that actual is a function handle that returns the logical scalar 1 (true). Similar to verifyReturnsTrue.Input Arguments testCase — Test case.actual — Value to test.diagnostic — Diagnostic information to display when the qualification passes or fails. For details, see Diagnostics. |
assumeSameHandle | assumeSameHandle(testCase,actual,expected,diagnostic)Assume 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. |
assumeThat | assumeThat(testCase,actual,constraint,diagnostic)Assume 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. |
assumeTrue | assumeTrue(testCase,actual,diagnostic)Assume that the value of actual is logical 1 (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
assumeError | [output1,...,outputN] = assumeError(testCase,actual,identifier,diagnostic)Assume 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. |
---|---|
assumeWarning | [output1,...,outputN] = assumeWarning(testCase,actual,identifier,diagnostic)Assume 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. |
assumeWarningFree | [output1,...,outputN] = assumeWarningFree(testCase,actual,diagnostic)Assume 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
assumeGreaterThan | assumeGreaterThan(testCase,actual,floor,diagnostic)Assume 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. |
---|---|
assumeGreaterThanOrEqual | assumeGreaterThanOrEqual(testCase,actual,floor,diagnostic)Assume 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. |
assumeLessThan | assumeLessThan(testCase,actual,ceiling,diagnostic)Assume that all elements of actual are less than all elements ofceiling. 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. |
assumeLessThanOrEqual | assumeLessThanOrEqual(testCase,actual,ceiling,diagnostic)Assume 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
assumeEmpty | assumeEmpty(testCase,actual,diagnostic)Assume 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. |
---|---|
assumeLength | assumeLength(testCase,actual,expectedLength,diagnostic)Assume 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. |
assumeNotEmpty | assumeNotEmpty(testCase,actual,diagnostic)Assume 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. |
assumeNumElements | assumeNumElements(testCase,actual,expectedNumElements,diagnostic)Assume 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. |
assumeSize | assumeSize(testCase,actual,expectedSize,diagnostic)Assume 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
assumeClass | assumeClass(testCase,actual,class,diagnostic)Assume that the class of actual is the specified class. Similar toverifyClass.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. |
---|---|
assumeInstanceOf | assumeInstanceOf(testCase,actual,class,diagnostic)Assume 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
assumeMatches | assumeMatches(testCase,actual,expression,diagnostic)Assume 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. |
---|---|
assumeSubstring | assumeSubstring(testCase,actual,substring,diagnostic)Assume 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
assumeEqualsBaseline (requires MATLAB Test™) | assumeEqualsBaseline(testCase,actual,baseline,diagnostic,Name=Value)Assume 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
Use an assumption to ensure that your tests can run only on a Linux® platform. Instruct the testing framework to filter the tests if MATLAB is installed on a Microsoft® Windows® or macOS platform.
In a file in your current folder, create the LinuxTests
class. To test for the platform, define the testPlatform
method within a TestClassSetup
methods
block. The method uses a call to the assumeTrue
method to test that MATLAB is installed on a Linux platform. If the assumption fails, the framework filters the entire test class.
classdef LinuxTests < matlab.unittest.TestCase methods (TestClassSetup) function testPlatform(testCase) testCase.assumeTrue(isunix && ~ismac, ... "Tests must run on a Linux platform.") end end end
Define your tests within a methods
block with the Test
attribute. The tests in this example are for illustration purposes only.
classdef LinuxTests < matlab.unittest.TestCase methods (TestClassSetup) function testPlatform(testCase) testCase.assumeTrue(isunix && ~ismac, ... "Tests must run on a Linux platform.") end end
methods (Test)
function test1(testCase)
testCase.verifyWarningFree(@rand)
end
function test2(testCase)
testCase.verifyWarningFree(@() size([]))
end
end
end
Run the tests on a Windows machine. Because the assumption fails at the class-setup level, the framework filters the tests defined by the LinuxTests
class.
Running LinuxTests
================================================================================ All tests in LinuxTests were filtered. Test Diagnostic: Tests must run on a Linux platform. Details
Done LinuxTests
Failure Summary:
Name Failed Incomplete Reason(s)
===============================================================
LinuxTests/test1 X Filtered by assumption.
---------------------------------------------------------------
LinuxTests/test2 X Filtered by assumption.
ans = 1×2 TestResult array with properties:
Name
Passed
Failed
Incomplete
Duration
Details
Totals: 0 Passed, 0 Failed, 2 Incomplete. 0.27782 seconds testing time.
More About
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.assumeEqual(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.assumeEqual(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.assumeEqual(actual,expected) testCase.addTeardown(@close,f)
Version History
Introduced in R2013a
If you have a MATLAB Test license, you can use the assumeEqualsBaseline
method to test if a value is equal to baseline data.
See Also
Classes
- matlab.unittest.qualifications.Verifiable | matlab.unittest.qualifications.Assertable | matlab.unittest.qualifications.FatalAssertable | matlab.unittest.TestCase | matlab.unittest.qualifications.QualificationEventData