Main Content

getSharedTestFixtures

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

Provide access to shared test fixtures

Description

example

fixtures = getSharedTestFixtures(testCase) provides access to the shared test fixtures for the test case and returns them as an array of matlab.unittest.fixtures.Fixture objects. Shared test fixtures are specified using the SharedTestFixtures attribute of TestCase subclasses.

example

fixtures = getSharedTestFixtures(testCase,fixtureClassName) returns only the shared test fixtures whose class is named fixtureClassName.

Input Arguments

expand all

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

Fully qualified name of the class that defines the shared test fixture, specified as a string scalar or character vector.

Example: "matlab.unittest.fixtures.PathFixture"

Attributes

Sealedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Access the shared fixtures in your tests by using the getSharedTestFixtures method.

This example assumes that your current folder contains a subfolder named helperFiles. Create the subfolder if it does not exist.

[~,~] = mkdir("helperFiles")

In a file in your current folder, create the SampleTest test class that uses two shared test fixtures. For illustration purposes, in this example, the Test methods access the fixtures to perform their qualifications.

classdef (SharedTestFixtures={ ...
        matlab.unittest.fixtures.PathFixture("helperFiles"), ...
        matlab.unittest.fixtures.TemporaryFolderFixture}) ...
        SampleTest < matlab.unittest.TestCase
    methods (Test)
        function testFixtureCount(testCase)
            % Test the number of shared test fixtures
            f = testCase.getSharedTestFixtures;
            testCase.verifyNumElements(f,2)
        end

        function testPath(testCase)
            % Test the search path
            import matlab.unittest.constraints.ContainsSubstring
            f = testCase.getSharedTestFixtures( ...
                "matlab.unittest.fixtures.PathFixture");
            testCase.verifyThat(path,ContainsSubstring(f.Folder))
        end

        function testTempFolder(testCase)
            % Test writing to the temporary folder
            import matlab.unittest.constraints.IsFile
            f = testCase.getSharedTestFixtures( ...
                "matlab.unittest.fixtures.TemporaryFolderFixture");
            tempFolderName = f.Folder;
            filename = string(tempFolderName) + filesep + "myFile.dat";
            writematrix(magic(20),filename)
            testCase.verifyThat(filename,IsFile)
        end
    end
end

Run the test class. The testing framework first sets up the fixtures, runs the tests, and then tears down the fixtures. In this example, all the tests pass.

results = runtests("SampleTest");
Setting up PathFixture
Done setting up PathFixture: Added 'C:\work\helperFiles' to the path.
__________

Setting up TemporaryFolderFixture
Done setting up TemporaryFolderFixture: Created the temporary folder "C:\Temp\tpf8cf6d27_3c19_42ce_9c0f_d3a130a077f0".
__________

Running SampleTest
...
Done SampleTest
__________

Tearing down TemporaryFolderFixture
Done tearing down TemporaryFolderFixture: Deleted the temporary folder "C:\Temp\tpf8cf6d27_3c19_42ce_9c0f_d3a130a077f0" and all its contents.
__________

Tearing down PathFixture
Done tearing down PathFixture: Restored the path to its original state.
__________

Version History

Introduced in R2013b