Main Content

applyFixture

Class: matlab.unittest.TestCase
Namespace: matlab.unittest

Use fixture with test case

Description

example

applyFixture(testCase,fixture) sets up the specified fixture for use with the test case and then tears it down after testing.

Use applyFixture within a Test method, TestMethodSetup method, or TestClassSetup method. The timing and frequency of the setup and teardown actions depend on the scope in which you call the method:

  • Test method — The applyFixture method sets up the fixture for the current test and tears it down after the test runs.

  • TestMethodSetup method — The applyFixture method sets up the fixture before and tears it down after each test in the test class.

  • TestClassSetup method — The applyFixture method sets up the fixture a single time for the entire test class and tears it down after all the tests in the class run.

f = applyFixture(testCase,fixture) provides access to the fixture after setup by returning it as a matlab.unittest.fixtures.Fixture object.

Input Arguments

expand all

Test case, specified as a matlab.unittest.TestCase object.

Fixture, specified as a matlab.unittest.fixtures.Fixture object.

Attributes

Sealedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

You can suppress warnings while running a test by using a SuppressedWarningsFixture instance with your test case. For example, suppress the warning that is issued when you try to remove a folder that does not exist from the search path.

The rmpath command issues a warning when you use it to remove a nonexistent folder from the path.

rmpath nonexistentFolder
Warning: "nonexistentFolder" not found in path.

Return the identifier of the warning issued by the rmpath command.

[~,identifier] = lastwarn
identifier =

    'MATLAB:rmpath:DirNotFound'

In a file named SuppressedWarningTest.m in your current folder, create a test class that verifies that a call to rmpath runs without any warnings. For the test to pass, call the applyFixture method in your test.

classdef SuppressedWarningTest < matlab.unittest.TestCase
    methods (Test)
        function testWarningFree(testCase)
            import matlab.unittest.fixtures.SuppressedWarningsFixture
            testCase.applyFixture( ...
                SuppressedWarningsFixture("MATLAB:rmpath:DirNotFound"))
            testCase.verifyWarningFree(@() rmpath("nonexistentFolder"))
        end
    end
end

Run the test class. When the Test method invokes the function handle, the fixture suppresses the specified warning. The test passes.

runtests("SuppressedWarningTest");
Running SuppressedWarningTest
.
Done SuppressedWarningTest
__________

Once the test run is complete, the testing framework tears down the fixture and the environment returns to its original state. Therefore, a new call to rmpath with a nonexistent folder results in a warning.

rmpath nonexistentFolder
Warning: "nonexistentFolder" not found in path.

Version History

Introduced in R2013b