Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

getSharedTestFixtures

类: matlab.unittest.TestCase
命名空间: matlab.unittest

提供对共享测试脚手架的访问权限

说明

示例

fixtures = getSharedTestFixtures(testCase) 提供对测试用例的共享测试脚手架的访问,并将它们以 matlab.unittest.fixtures.Fixture 对象数组形式返回。共享测试脚手架是使用 TestCase 子类的 SharedTestFixtures 属性指定的。

示例

fixtures = getSharedTestFixtures(testCase,fixtureClassName) 只返回其类名称为 fixtureClassName 的共享测试脚手架。

输入参数

全部展开

测试用例,指定为 matlab.unittest.TestCase 对象。

定义共享测试脚手架的类的完全限定名称,指定为字符串标量或字符向量。

示例: "matlab.unittest.fixtures.PathFixture"

属性

Sealedtrue

要了解方法的属性,请参阅方法属性

示例

全部展开

使用 getSharedTestFixtures 方法访问测试中的共享脚手架。

此示例假定当前文件夹中存在子文件夹 helperFiles。如果该子文件夹不存在,请创建它。

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

在您的当前文件夹下的文件中,创建使用两个共享测试脚手架的 SampleTest 检验类。为了说明目的,在此示例中,Test 方法访问这些脚手架来执行其验证。

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

运行测试类。测试框架首先设置脚手架,运行测试,然后拆解脚手架。在此示例中,所有测试都通过。

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.
__________

版本历史记录

在 R2013b 中推出