Main Content

functiontests

根据局部函数的句柄创建测试数组

说明

示例

tests = functiontests(f) 根据局部函数 f 句柄的元胞数组创建测试数组 tests。如要应用已定义的设置和清除函数,请在 f 中包含其函数句柄。

局部测试函数必须在函数名称的开头或结尾处包含 "test"。必须从测试文件内部调用 functiontests

示例

全部折叠

在 MATLAB® 路径中创建文件 exampleTest.m。在主函数中,创建测试数组。使用局部函数定义设置、清除以及两个函数测试。您的文件应当类似于以下文件。

function tests = exampleTest
tests = functiontests(localfunctions);

function setup(testCase)
function teardown(testCase)
function exampleOneTest(testCase)
function testExampleTwo(testCase)

通过命令行来调用 exampleTest 函数。

tests = exampleTest
tests = 

  1x2 Test array with properties:

    Name
    ProcedureName
    TestClass
    BaseFolder
    Parameterization
    SharedTestFixtures
    Tags

Tests Include:
    0 Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.

访问测试套件以验证两个函数测试的名称。

tests.Name
ans =

    'exampleTest/exampleOneTest'


ans =

    'exampleTest/testExampleTwo'

输入参数

全部折叠

局部测试函数的句柄,指定为元胞数组。在工作文件中使用 f=localfunctions 自动为该文件生成函数句柄的元胞数组。如要进行显式测试枚举,则通过列举单个函数构造 ff 必须包含测试所必需的任何设置或清除函数。

示例: f = localfunctions;

示例: f = {@setup,@exampleOneTest,@teardown};

版本历史记录

在 R2013b 中推出