matlab.unittest.qualifications.Assertable class
Package: matlab.unittest.qualifications
Qualification to validate preconditions of a test
Description
The Assertable
class provides a qualification to validate preconditions of
a test. Apart from actions performed for failures, the Assertable
class works
the same as other qualification classes in the matlab.unittest.qualifications
package.
Upon an assertion failure, the Assertable
class informs the testing
framework of the failure by throwing an AssertionFailedException
object. This behavior is most useful when a failure at the
assertion point renders the remainder of the current test invalid, but does not prevent proper
execution of subsequent tests. Often, you use assertions to ensure that preconditions of the
current test are not violated or that fixtures are set up correctly. If you cannot make the
fixture teardown exception safe or
restore the environment state after failure, use fatal assertions instead.
When an assertion failure is produced within a method of the TestCase
class, the type of the method determines which tests are affected:
Test
method — The framework marks the entireTest
method as failed and incomplete.TestMethodSetup
orTestMethodTeardown
method — The framework marks theTest
method to run for that method instance as failed and incomplete.TestClassSetup
orTestClassTeardown
method — The framework marks the entire test class as failed and incomplete.
Assertions let remaining tests receive coverage when preconditions are violated in a test but the state is recoverable. They prevent unnecessary failures by not performing later verifications that fail due to invalidated preconditions. If the failure does not affect the preconditions of the test or cause problems with fixture setup or teardown, use verifications, which ensure that the full test content runs.
The matlab.unittest.qualifications.Assertable
class is a handle
class.
Methods
Public Methods
The Assertable
class provides several qualification methods for testing
values and responding to failures. For example, assertEmpty
tests that a
value is empty, and assertTrue
tests that the actual value is true.
Note
The methods of the Assertable
class correspond to the methods of the
Verifiable
class. They differ only in terms of
qualification type. You can call the Assertable
methods in the same way you
call the Verifiable
methods.
assertEqual |
Assert
that Input Arguments
Name-Value Arguments
|
assertFail |
Produce
an unconditional assertion failure. Similar to Input Arguments
|
assertFalse |
Assert
that the value of Input Arguments
|
assertNotEqual |
Assert
that Input Arguments
|
assertNotSameHandle |
Assert
that Input Arguments
|
assertReturnsTrue |
Assert
that Input Arguments
|
assertSameHandle |
Assert
that Input Arguments
|
assertThat |
Assert
that Input Arguments
|
assertTrue |
Assert
that the value of Input Arguments
|
assertError |
Assert
that Input Arguments
Output Arguments
|
assertWarning |
Assert
that Input Arguments
Output Arguments
|
assertWarningFree |
Assert
that Input Arguments
Output Arguments
|
assertGreaterThan |
Assert
that all elements of Input Arguments
|
assertGreaterThanOrEqual |
Assert
that all elements of Input Arguments
|
assertLessThan |
Assert
that all elements of Input Arguments
|
assertLessThanOrEqual |
Assert
that all elements of Input Arguments
|
assertEmpty |
Assert
that Input Arguments
|
assertLength |
Assert
that Input Arguments
|
assertNotEmpty |
Assert
that Input Arguments
|
assertNumElements |
Assert
that Input Arguments
|
assertSize |
Assert
that Input Arguments
|
assertClass |
Assert
that the class of Input Arguments
|
assertInstanceOf |
Assert
that Input Arguments
|
assertMatches |
Assert
that Input Arguments
|
assertSubstring |
Assert
that Input Arguments
|
Events
Event Name | Trigger | Event Data | Event Attributes |
---|---|---|---|
AssertionFailed | Triggered upon failing assertion. A QualificationEventData
object is passed to listener callback functions. | matlab.unittest.qualifications.QualificationEventData |
|
AssertionPassed | Triggered upon passing assertion. A QualificationEventData
object is passed to listener callback functions. | matlab.unittest.qualifications.QualificationEventData |
|
Examples
Validate Precondition of Tests
To test multiplication of DocPolynom
objects, first add the examples
folder to the search path and validate that the DocPolynom
class is available to your tests. For more information about the DocPolynom
class, see Representing Polynomials with Classes.
In a file in your current folder, create the DocPolynomMultiplicationTest
class, which tests multiplication of DocPolynom
objects. To access DocPolynom
in your tests, define the addDocPolynomClassToPath
method within a TestClassSetup
methods
block. Use a PathFixture
instance within the method to add the examples
folder, including the DocPolynom
class definition file, to the path. Then, use an assertion to validate that the fixture has made DocPolynom
available to your tests. If the assertion fails, the framework fails the Test
methods without trying to run them. Otherwise, the framework runs the Test
methods. Finally, the framework tears down the fixture and restores the path to its previous state.
classdef DocPolynomMultiplicationTest < 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)) testCase.assertNotEmpty(?DocPolynom) end end end
Now, add your tests to a methods
block with the Test
attribute.
classdef DocPolynomMultiplicationTest < 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)) testCase.assertNotEmpty(?DocPolynom) end end methods (Test) function test1(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 function test2(testCase) p1 = DocPolynom([1 4]); p2 = DocPolynom([2 3]); p3 = DocPolynom([1 0 -1]); actual = p1 * p2 * p3; expected = DocPolynom([2 11 10 -11 -12]); diagnostic = [testCase.TextToDisplay ... "(x + 4) * (2*x + 3) * (x^2 - 1) = 2*x^4 + 11*x^3 + 10*x^2 - 11*x - 12"]; testCase.verifyEqual(actual,expected,diagnostic) end end end
Run the tests in the DocPolynomMultiplicationTest
class. In this example, both of the tests pass.
runtests("DocPolynomMultiplicationTest")
Running DocPolynomMultiplicationTest .. Done DocPolynomMultiplicationTest __________
ans = 1×2 TestResult array with properties: Name Passed Failed Incomplete Duration Details Totals: 2 Passed, 0 Failed, 0 Incomplete. 0.38698 seconds testing time.
More About
Diagnostics
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 optional
diagnostic
argument in any of the qualification methods. You can
specify diagnostic
as a string array, character array, function handle,
or array of matlab.unittest.diagnostics.Diagnostic
objects.
Exception Safe
Test content is exception safe when all fixture
teardown is performed with the addTeardown
method of TestCase
or Fixture
classes, or when it is performed using
object destructors upon a failure. Exception safety ensures that a test failure does not
affect subsequent tests even when an exception is thrown.
For example, this code is not exception safe. If the test fails, the testing framework does not close the figure.
% Not exception safe
f = figure;
testCase.assertEqual(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.assertEqual(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 to
addTeardown
is placed after the test. If the test fails, the framework
cannot close the
figure.
% Not exception safe
f = figure;
testCase.assertEqual(actual,expected)
testCase.addTeardown(@close,f)
Version History
Introduced in R2013a
Beispiel öffnen
Sie haben eine geänderte Version dieses Beispiels. Möchten Sie dieses Beispiel mit Ihren Änderungen öffnen?
MATLAB-Befehl
Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:
Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)