matlab.unittest.selectors.HasBaseFolder 类
命名空间: matlab.unittest.selectors
超类: matlab.unittest.selectors.Selector
根据基本文件夹选择 TestSuite 数组元素
描述
matlab.unittest.selectors.HasBaseFolder 类提供用于根据测试的基本文件夹过滤测试套件的选择器。
测试的基本文件夹是包含定义测试的文件的文件夹。对于命名空间中定义的测试,基本文件夹是顶级命名空间文件夹的父级。
创建对象
描述
输入参量
基本文件夹的名称,指定为字符串标量、字符向量或 matlab.unittest.constraints.Constraint 对象。根据基本文件夹选择测试受以下条件限制:
如果指定字符串标量或字符向量,测试的基本文件夹必须与指定的值相同。指定的值必须为完整路径。
如果指定约束,测试的基本文件夹必须满足该约束。
此参量设置 Constraint 属性。
属性
将测试包含在经过过滤的测试套件中时基本文件夹必须满足的条件,以 matlab.unittest.constraints.Constraint 对象形式返回。
此属性由 folder 输入参量设置:
如果您指定字符串标量或字符向量,测试框架会将该属性设置为
IsEqualTo约束,并将预期值作为指定的基本文件夹。如果指定约束,测试框架会将该属性设置为约束。
属性:
GetAccess | public |
SetAccess | immutable |
示例
通过使用 HasBaseFolder 类选择测试来创建经过过滤的测试套件。为了简化测试代码,此示例中的测试类使用无条件测试失败作为未实现测试的占位符。
在当前文件夹中创建名为 myTests 的文件夹。然后在 myTests 中创建两个文件夹 feature1 和 feature2。
在 feature1 文件夹下名为 Feature1Test.m 的文件中,创建 Feature1Test 类。
classdef Feature1Test < matlab.unittest.TestCase methods (Test) function defaultBehavior(testCase) testCase.verifyFail("Add code to test default behavior.") end function otherBehavior(testCase) testCase.verifyFail("Add code to test nondefault behavior.") end end end
在 feature2 文件夹下名为 Feature2Test.m 的文件中,创建 Feature2Test 类。
classdef Feature2Test < matlab.unittest.TestCase methods (Test) function defaultBehavior(testCase) testCase.verifyFail("Add code to test default behavior.") end function otherBehavior(testCase) testCase.verifyFail("Add code to test nondefault behavior.") end end end
导入此示例中使用的类。
import matlab.unittest.TestSuite import matlab.unittest.selectors.HasBaseFolder import matlab.unittest.constraints.ContainsSubstring
请确保您的当前文件夹是 myTests 的父文件夹。基于 myTests 文件夹及其子文件夹创建一个测试套件。然后,显示 TestSuite 数组元素的名称。该测试套件包含在 feature1 和 feature2 文件夹中定义的测试。
suite = testsuite("myTests","IncludingSubfolders",true); disp({suite.Name}')
{'Feature1Test/defaultBehavior'}
{'Feature1Test/otherBehavior' }
{'Feature2Test/defaultBehavior'}
{'Feature2Test/otherBehavior' }选择在 feature1 文件夹中定义的所有测试。
suite1 = suite.selectIf(HasBaseFolder( ... fullfile(pwd,"myTests","feature1"))); disp({suite1.Name}')
{'Feature1Test/defaultBehavior'}
{'Feature1Test/otherBehavior' }选择 suite 中所有在 feature1 之外定义的测试。经过过滤的测试套件仅包含在 feature2 文件夹中定义的测试。
suite2 = suite.selectIf(~HasBaseFolder( ... fullfile(pwd,"myTests","feature1"))); disp({suite2.Name}')
{'Feature2Test/defaultBehavior'}
{'Feature2Test/otherBehavior' }通过仅包括其基本文件夹包含子字符串 "2" 的测试,直接基于 myTests 及其子文件夹创建一个经过过滤的测试套件。
suite3 = TestSuite.fromFolder("myTests", ... HasBaseFolder(ContainsSubstring("2")), ... "IncludingSubfolders",true); disp({suite3.Name}')
{'Feature2Test/defaultBehavior'}
{'Feature2Test/otherBehavior' }运行过滤后的测试套件。
results = run(suite3)
Running Feature2Test
================================================================================
Verification failed in Feature2Test/defaultBehavior.
----------------
Test Diagnostic:
----------------
Add code to test default behavior.
------------------
Stack Information:
------------------
In C:\work\myTests\feature2\Feature2Test.m (Feature2Test.defaultBehavior) at 4
================================================================================
.
================================================================================
Verification failed in Feature2Test/otherBehavior.
----------------
Test Diagnostic:
----------------
Add code to test nondefault behavior.
------------------
Stack Information:
------------------
In C:\work\myTests\feature2\Feature2Test.m (Feature2Test.otherBehavior) at 7
================================================================================
.
Done Feature2Test
__________
Failure Summary:
Name Failed Incomplete Reason(s)
===========================================================================
Feature2Test/defaultBehavior X Failed by verification.
---------------------------------------------------------------------------
Feature2Test/otherBehavior X Failed by verification.
results =
1×2 TestResult array with properties:
Name
Passed
Failed
Incomplete
Duration
Details
Totals:
0 Passed, 2 Failed (rerun), 0 Incomplete.
0.20227 seconds testing time.版本历史记录
在 R2014a 中推出
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)