在工作文件夹下的文件中,创建一个名为 cylinderPlotTest 的基于函数的测试。
function tests = cylinderPlotTest
tests = functiontests(localfunctions);
end
function setupOnce(testCase)
testCase.TestData.Figure = figure;
addTeardown(testCase,@close,testCase.TestData.Figure)
end
function setup(testCase)
testCase.TestData.Axes = axes('Parent',testCase.TestData.Figure);
addTeardown(testCase,@clf,testCase.TestData.Figure)
cylinder(testCase.TestData.Axes,10)
end
function testXLim(testCase)
xlim = testCase.TestData.Axes.XLim;
verifyLessThanOrEqual(testCase,xlim(1),-10,'Minimum x-limit too large')
verifyGreaterThanOrEqual(testCase,xlim(2),10,'Maximum x-limit too small')
end
function zdataTest(testCase)
s = findobj(testCase.TestData.Axes,'Type','surface');
verifyEqual(testCase,min(s.ZData(:)),0,'Min cylinder value is incorrect')
verifyEqual(testCase,max(s.ZData(:)),1,'Max cylinder value is incorrect')
end
在命令提示符下运行测试。
results = run(cylinderPlotTest);
Running cylinderPlotTest
..
Done cylinderPlotTest
__________
默认情况下,测试运行程序使用详细级别 2。
创建一个用于报告级别 1 的诊断的测试运行程序,并重新运行该测试。
import matlab.unittest.TestRunner
import matlab.unittest.plugins.TestRunProgressPlugin
runner = TestRunner.withNoPlugins;
p = TestRunProgressPlugin.withVerbosity(1);
runner.addPlugin(p);
results = runner.run(cylinderPlotTest);
..
创建一个用于报告级别 4 的诊断的测试运行程序,并重新运行该测试。
runner = TestRunner.withNoPlugins;
p = TestRunProgressPlugin.withVerbosity(4);
runner.addPlugin(p);
results = runner.run(cylinderPlotTest);
Running cylinderPlotTest
Setting up cylinderPlotTest
Evaluating TestClassSetup: setupOnce
Done setting up cylinderPlotTest in 0.067649 seconds
Running cylinderPlotTest/testXLim
Evaluating TestMethodSetup: setup
Evaluating Test: testXLim
Evaluating TestMethodTeardown: teardown
Evaluating addTeardown function: clf
Done cylinderPlotTest/testXLim in 0.053834 seconds
Running cylinderPlotTest/zdataTest
Evaluating TestMethodSetup: setup
Evaluating Test: zdataTest
Evaluating TestMethodTeardown: teardown
Evaluating addTeardown function: clf
Done cylinderPlotTest/zdataTest in 0.037715 seconds
Tearing down cylinderPlotTest
Evaluating TestClassTeardown: teardownOnce
Evaluating addTeardown function: close
Done tearing down cylinderPlotTest in 0.022783 seconds
Done cylinderPlotTest in 0.18198 seconds
__________