matlab.unittest.TestCase.forInteractiveUse
类: matlab.unittest.TestCase
命名空间: matlab.unittest
创建供交互测试的测试用例
语法
说明
testCase = matlab.unittest.TestCase.forInteractiveUse
创建一个为交互式测试配置的测试用例。返回的 TestCase
实例适合在命令提示符下进行试验。它通过向屏幕输出通过和失败事件的消息来响应鉴定。
输入参数
示例
测试子字符串
测试 actual 值是否包含指定的子字符串。
创建一个供交互测试的测试用例。
testCase = matlab.unittest.TestCase.forInteractiveUse;
定义 actual 值。
actual = "This is a long message.";
验证 actual
包含文本 "long"
。
verifySubstring(testCase,actual,"long")
Verification passed.
展示大小写不同所造成的影响。此测试失败,因为 actual
不包含 "Long"
。
verifySubstring(testCase,actual,"Long","Test is case sensitive.")
Verification failed. ---------------- Test Diagnostic: ---------------- Test is case sensitive. --------------------- Framework Diagnostic: --------------------- verifySubstring failed. --> The value does not contain the substring. Actual Value: "This is a long message." Expected Substring: "Long" ------------------ Stack Information: ------------------ In C:\work\TestForSubstringsExample.m (TestForSubstringsExample) at 22
显示如果子字符串比实际字符串长,测试将失败。
verifySubstring(testCase,actual,"This is a long message with extra words.")
Verification failed. --------------------- Framework Diagnostic: --------------------- verifySubstring failed. --> The value does not contain the substring. Actual Value: "This is a long message." Expected Substring: "This is a long message with extra words." ------------------ Stack Information: ------------------ In C:\work\TestForSubstringsExample.m (TestForSubstringsExample) at 27
为指定的类创建交互式测试用例
以交互方式运行测试类的 Test
方法。
在当前文件夹中名为 ZerosTest.m
的文件中,创建 ZerosTest
类来测试 zeros
函数。
classdef ZerosTest < matlab.unittest.TestCase properties (TestParameter) type = {'single','double','uint16'}; size = struct("s2d",[3 3],"s3d",[2 5 4]); end methods (Test) function testClass(testCase,size,type) testCase.verifyClass(zeros(size,type),type) end function testSize(testCase,size) testCase.verifySize(zeros(size),size) end function testDefaultClass(testCase) testCase.verifyClass(zeros,"double") end function testDefaultSize(testCase) testCase.verifySize(zeros,[1 1]) end function testDefaultValue(testCase) testCase.verifyEqual(zeros,0) end end end
创建一个供交互测试的 ZerosTest
类的实例。
testCase = matlab.unittest.TestCase.forInteractiveUse(?ZerosTest);
以交互方式使用该测试用例调用 testSize
方法。测试通过。
testCase.testSize([5 10])
Verification passed.
为交互式测试应用共享测试脚手架
自 R2024a 起
当您以交互方式运行测试类的 Test
方法时,请使用共享测试脚手架。
此示例假设您的当前文件夹中包含名为 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
创建一个供交互测试的 SampleTest
类的实例。由于 SampleTest
中的测试依赖共享测试脚手架,请在创建测试用例时应用脚手架。
testCase = matlab.unittest.TestCase.forInteractiveUse(?SampleTest, ...
ApplySharedTestFixtures=true);
以交互方式使用该测试用例调用 testFixtureCount
方法。测试通过,因为共享测试脚手架可供测试使用。如果您创建测试用例而没有应用脚手架,测试将会失败。
testCase.testFixtureCount
Verification passed.
版本历史记录
在 R2014a 中推出R2024a: 在交互式测试中使用共享测试脚手架
当从测试类创建一个交互式测试用例时,为了应用与该测试类相关联的共享测试脚手架,请将 tf
指定为 true
。有关示例,请参阅为交互式测试应用共享测试脚手架。
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)