matlab.unittest.TestCase.applyFixture - Use fixture with test case - MATLAB (original) (raw)

Main Content

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

Use fixture with test case

Syntax

Description

applyFixture([testCase](#mw%5F1b82dd2d-7b9f-4c30-8739-955f766fc976),[fixture](#mw%5F2093d151-9bce-44ca-a2dc-d7bd96f82b29)) 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:

example

Input Arguments

expand all

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

Examples

expand all

You can suppress warnings while running a test by using aSuppressedWarningsFixture 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.

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.

Warning: "nonexistentFolder" not found in path.

Version History

Introduced in R2013b