matlab.unittest.fixtures.Fixture Class
Namespace: matlab.unittest.fixtures
Fundamental interface for test fixtures
Description
The matlab.unittest.fixtures.Fixture
class provides an interface for test
fixtures. Fixtures specify setup and teardown code that sets up the test environment and
restores it to its original state after running the test.
A Fixture
subclass must implement the setup
method, which makes changes to the environment when the testing framework
sets up the fixture. To restore the environment when the framework tears down the fixture,
call the addTeardown
method within the
setup
method, or implement the teardown
method.
In addition to specifying setup and teardown actions in your Fixture
subclass, you must implement the isCompatible
method if the fixture is
configurable (for example, if its class constructor accepts input arguments). The testing
framework calls isCompatible
to determine whether instances of the same
Fixture
subclass correspond to the same shared test fixture state.
The matlab.unittest.fixtures.Fixture
class is a handle
class.
Properties
SetupDescription
— Description of setup actions
character vector
Description of the setup actions, returned as a character vector. Set this property
in your Fixture
implementation to describe the actions the fixture
performs when the testing framework invokes the setup
method. If you use the fixture as a shared test fixture, the framework
displays the property value when setting up the fixture.
Attributes:
GetAccess | public |
SetAccess | protected |
TeardownDescription
— Description of teardown actions
character vector
Description of the teardown actions, returned as a character vector. Set this
property in your Fixture
implementation to describe the actions the
fixture performs when the testing framework invokes the teardown
method or the function handle passed to the addTeardown
method. If you use the fixture as a shared test fixture, the
framework displays the property value when tearing down the fixture.
Attributes:
GetAccess | public |
SetAccess | protected |
Methods
Public Methods
addTeardown | Dynamically add teardown code to fixture |
applyAndRun | Run function in environment provided by fixture |
setup | Set up fixture |
teardown | Tear down fixture |
Protected Methods
applyFixture | Set up fixture to delegate work to another fixture |
isCompatible | Determine if two fixtures are compatible |
log | Record diagnostic information during fixture setup and teardown |
needsReset | Determine if shared test fixture needs to be reset |
onFailure | Dynamically add diagnostics for failures during fixture setup and teardown |
Qualification-Based Protected Methods
Similar to the Assumable
, Assertable
, and FatalAssertable
classes, the Fixture
class
provides several qualification methods for testing values and responding to failures. For
example, you can use these protected methods to implement the setup
and teardown
methods in Fixture
subclasses.
The Fixture
class supports assumptions, assertions, and fatal assertions.
These qualification types have parallel methods for the same types of tests. The methods use
a common naming convention. For instance, the methods that test for equality use the form
<qualify>
Equal
, where
<qualify>
can be assume
,
assert
, or fatalAssert
. That is:
assumeEqual
— Assume that two values are equal.assertEqual
— Assert that two values are equal.fatalAssertEqual
— Fatally assert that two values are equal.
|
Test
if Input Arguments
Name-Value Arguments
|
|
Produce
an unconditional qualification failure. Replace
Input Arguments
|
|
Test
if the value of Input Arguments
|
|
Test
if Input Arguments
|
|
Test
if Input Arguments
|
|
Test
if Input Arguments
|
|
Test
if Input Arguments
|
|
Test
if Input Arguments
|
|
Test
if the value of Input Arguments
|
|
Test
if Input Arguments
Output Arguments (Optional)
|
|
Test
if Input Arguments
Output Arguments (Optional)
|
|
Test
if Input Arguments
Output Arguments (Optional)
|
|
Test
if all elements of Input Arguments
|
|
Test
if all elements of Input Arguments
|
|
Test
if all elements of Input Arguments
|
|
Test
if all elements of Input Arguments
|
|
Test
if Input Arguments
|
|
Test
if Input Arguments
|
|
Test
if Input Arguments
|
|
Test
if Input Arguments
|
|
Test
if Input Arguments
|
|
Test
if the class of Input Arguments
|
|
Test
if Input Arguments
|
|
Test
if Input Arguments
|
|
Test
if Input Arguments
|
Events
Event Name | Trigger | Event Data | Event Attributes |
---|---|---|---|
AssumptionFailed | Triggered upon failing assumption. A QualificationEventData
object is passed to listener callback functions. | matlab.unittest.qualifications.QualificationEventData |
|
AssumptionPassed | Triggered upon passing assumption. A QualificationEventData
object is passed to listener callback functions. | matlab.unittest.qualifications.QualificationEventData |
|
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 |
|
FatalAssertionFailed | Triggered upon failing fatal assertion. A QualificationEventData
object is passed to listener callback functions. | matlab.unittest.qualifications.QualificationEventData |
|
FatalAssertionPassed | Triggered upon passing fatal assertion. A QualificationEventData
object is passed to listener callback functions. | matlab.unittest.qualifications.QualificationEventData |
|
ExceptionThrown | Triggered by the test runner when it catches an exception in the test content. An
ExceptionEventData object is passed to listener callback
functions. | matlab.unittest.qualifications.ExceptionEventData |
|
DiagnosticLogged | Triggered upon a call to the log method. A
LoggedDiagnosticEventData object is passed to listener callback
functions. | matlab.unittest.diagnostics.LoggedDiagnosticEventData |
|
Examples
Create and Use Test Fixture
Create and use a custom fixture that changes the output display format for numeric values to the currency format with two digits after the decimal point.
In a file named CurrencyFormatFixture.m
in your current folder,
create the CurrencyFormatFixture
class by subclassing the
matlab.unittest.fixtures.Fixture
interface. Implement the
setup
method in the class so that the fixture changes the display
format for numeric values to the currency format. To restore the display format to its
original state after testing, call the addTeardown
method within the
setup
method.
classdef CurrencyFormatFixture < matlab.unittest.fixtures.Fixture methods function setup(fixture) originalFormat = format; fixture.addTeardown(@format,originalFormat) format bank end end end
In a file named ExampleTest.m
in your current folder, create the
ExampleTest
class that applies the custom fixture and verifies that a
numeric value is displayed in the expected format. To simplify this example, the actual
value is produced by a call to the formattedDisplayText
function. In practice, you test user-defined
code.
classdef ExampleTest < matlab.unittest.TestCase methods (Test) function formatTest(testCase) testCase.applyFixture(CurrencyFormatFixture) actual = strtrim(formattedDisplayText(pi)); expected = "3.14"; testCase.verifyEqual(actual,expected) end end end
Run the ExampleTest
class. The testing framework sets up the fixture,
which changes the display format to the currency format. Once the test run is complete,
the framework tears down the fixture, which restores the original display format. In
this example, the test passes.
runtests("ExampleTest");
Running ExampleTest . Done ExampleTest __________
More About
Diagnostics
Depending on the test runner configuration, the testing framework can 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.automation.diagnostics.Diagnostic
objects.
Version History
Introduced in R2014aR2024b: Run function in environment provided by fixtures
The matlab.unittest.fixtures.Fixture
class has a new method
applyAndRun
that lets you run a function in the environment provided by
one or more fixtures.
R2020b: Report validity of shared test fixtures
The matlab.unittest.fixtures.Fixture
class has a new method
needsReset
that lets you report the validity of shared test fixtures to
the testing framework. A shared test fixture is valid if the test environment state,
configured by the fixture, is maintained throughout the test session.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)