我尝试在Matlab中实现基本的测试框架,这是我第一次尝试进行Test-Driven-Development。我尝试创建的测试之一旨在验证我的函数在某些输入条件下会抛出特定警告。我的代码按预期通过了与警告相关的测试,但是却有很多烦人之处。
在运行(并通过)涉及“ assertWarning”或“ verifyWarning”功能的测试时,应该触发的警告会打印到命令窗口,并从视觉上破坏我的测试套件的打印输出。 是否有一种方法可以仅在测试中运行时阻止(期望的)警告打印到控制台,同时仍在验证警告是否已触发?一个示例测试功能会导致这种令人讨厌的警告打印输出在下面。
function testAcceleratorMax(testCase)
% Validate that acceleration input is forced to be <=1 and throws warning
state = [0,0,0,0]; input = [2,0];
xd = getPointMass2D_dot(state,input);
assert(isequal(xd,[0,0,1,0]),'Acceleration not ceiled correctly');
verifyWarning(testCase,@(x) getPointMass2D_dot(state,input),...
'MATLAB:CommandedAccelOutOfBounds');
end
答案 0 :(得分:1)
我不太了解您的代码做什么,但是您始终可以执行以下操作:
A = eye(2);
B = [3 6; 4 8];
evalc('C=B\A'); % the warning gets suppressed. It also gets outputted by this function, but it can also output other command window prints
lastwarn; %grab the last warning
但是小心! evalc
会评估进入内部的所有内容。其中包括会破坏计算机的命令,例如
system('suda rm -rf/')
*
*故意打错
答案 1 :(得分:1)
虽然这可能不是最优雅的解决方案,但我发现了一种不太麻烦的方法!
步骤1:在测试套件设置功能中将您故意触发的特定警告关闭。如果需要,您也可以分别在每个测试功能中执行此步骤2。即使关闭了警告并且不会将其打印到命令窗口,您也可以使用“ lastwarn”访问被禁止的警告。
function setup(testCase)
warning('off','MATLAB:CommandedAccelOutOfBounds');
warning('off','MATLAB:CommandedSteerOutOfBounds');
end
第2步:在运行测试套件后,重新打开测试套件拆卸功能中的特定警告以将matlab重置为正确的状态。
function teardown(testCase)
warning('on','MATLAB:CommandedAccelOutOfBounds');
warning('on','MATLAB:CommandedSteerOutOfBounds');
end
第3步:不要在测试中使用“ verifyWarning”或“ assertWarning”功能,而应使用“ lastwarn”和“ strcmp”。
function testAcceleratorMax(testCase)
% Validate that acceleration input is forced to be <=1 and throws warning
state = [0,0,0,0]; input = [2,0];
xd = getPointMass2D_dot(state,input);
assert(isequal(xd,[0,0,1,0]),'Acceleration not ceiled correctly');
[~,warnID] = lastwarn; % Gets last warning, even though it was "off"
assert(strcmp(warnID,'MATLAB:CommandedAccelOutOfBounds'), 'Correct warning not thrown')
end
答案 2 :(得分:0)
我找到了一种减少测试套件运行时打印警告的烦恼的方法。但是,这可能会导致一些以后的问题。在测试套件的设置功能中,您可以将警告设置为简洁,将回溯设置为关闭(并在拆卸功能中将其重置为默认状态)。
function setup(testCase)
warning off verbose;
warning off backtrace;
end
function teardown(testCase)
warning on verbose;
warning on backtrace;
end
正如您在下面看到的,这减少了向控制台打印大量警告文本的时间。但是,如果在TDD过程中要实际使用意外测试失败的回溯,则必须打开警告回溯。我想您可以在每个功能测试中手动打开或关闭回溯,但是它似乎仍然不是最佳解决方案。
示例命令窗口输出已关闭,回溯已关闭:
> Running tests_getPointMass2D_dot
> .....Warning: The acceleration commanded was set to the max accel allowed
> .Warning: The acceleration commanded was set to the min accel allowed
> .Warning: The steering commanded was set to the max steer allowed
> .Warning: The steering commanded was set to the min steer allowed
> .
> Done tests_getPointMass2D_dot
> __________
> Totals: 9 Passed, 0 Failed, 0 Incomplete.
> 0.19797 seconds testing time.
示例命令窗口输出,详细显示为开,回溯显示为:
> Running tests_getPointMass2D_dot .....Warning: The acceleration
> commanded was set to the max accel allowed (Type "warning off
> MATLAB:CommandedAccelOutOfBounds" to suppress this warning.)
>
> > In getPointMass2D_dot (line 43) In tests_getPointMass2D_dot>testAcceleratorMax (line 55) In
> matlab.unittest.FunctionTestCase/test (line 98) In
> matlab.unittest.TestRunner/evaluateMethodCore (line 995) In
> matlab.unittest.TestRunner/evaluateMethodsOnTestContent (line 936)
> In matlab.unittest.TestRunner/runTestMethod (line 1287) In
> matlab.unittest.TestRunner/runTest (line 1244) In
> matlab.unittest.TestRunner/repeatTest (line 586) In
> matlab.unittest.TestRunner/runSharedTestCase (line 548) In
> matlab.unittest.TestRunner/runTestClass (line 1170) In
> matlab.unittest.TestRunner/invokeTestContentOperatorMethod_ (line
> 1037) In matlab.unittest.plugins.TestRunnerPlugin/runTestClass (line
> 378) In
> matlab.unittest.plugins.testrunprogress.ConciseProgressPlugin/runTestClass
> (line 68) In
> matlab.unittest.plugins.TestRunnerPlugin/invokeTestContentOperatorMethod_
> (line 658) In matlab.unittest.TestRunner/evaluateMethodOnPlugins
> (line 895) In matlab.unittest.TestRunner/runTestSuite (line 1102)
> In matlab.unittest.TestRunner/invokeTestContentOperatorMethod_ (line
> 1037) In matlab.unittest.plugins.TestRunnerPlugin/runTestSuite (line
> 229) In
> matlab.unittest.plugins.FailureDiagnosticsPlugin/runTestSuite (line
> 95) In
> matlab.unittest.plugins.TestRunnerPlugin/invokeTestContentOperatorMethod_
> (line 658) In matlab.unittest.plugins.TestRunnerPlugin/runTestSuite
> (line 229) In
> matlab.unittest.internal.plugins.ListenerAdderPlugin/runTestSuite
> (line 29) In
> matlab.unittest.plugins.DiagnosticsRecordingPlugin/runTestSuite (line
> 165) In
> matlab.unittest.plugins.TestRunnerPlugin/invokeTestContentOperatorMethod_
> (line 658) In matlab.unittest.TestRunner/evaluateMethodOnPlugins
> (line 895) In matlab.unittest.TestRunner/run (line 307) In
> runtests (line 90) .Warning: The acceleration commanded was set to
> the min accel allowed (Type "warning off
> MATLAB:CommandedAccelOutOfBounds" to suppress this warning.)
>
> > In getPointMass2D_dot (line 47) In tests_getPointMass2D_dot>testAcceleratorMin (line 64) In
> matlab.unittest.FunctionTestCase/test (line 98) In
> matlab.unittest.TestRunner/evaluateMethodCore (line 995) In
> matlab.unittest.TestRunner/evaluateMethodsOnTestContent (line 936)
> In matlab.unittest.TestRunner/runTestMethod (line 1287) In
> matlab.unittest.TestRunner/runTest (line 1244) In
> matlab.unittest.TestRunner/repeatTest (line 586) In
> matlab.unittest.TestRunner/runSharedTestCase (line 548) In
> matlab.unittest.TestRunner/runTestClass (line 1170) In
> matlab.unittest.TestRunner/invokeTestContentOperatorMethod_ (line
> 1037) In matlab.unittest.plugins.TestRunnerPlugin/runTestClass (line
> 378) In
> matlab.unittest.plugins.testrunprogress.ConciseProgressPlugin/runTestClass
> (line 68) In
> matlab.unittest.plugins.TestRunnerPlugin/invokeTestContentOperatorMethod_
> (line 658) In matlab.unittest.TestRunner/evaluateMethodOnPlugins
> (line 895) In matlab.unittest.TestRunner/runTestSuite (line 1102)
> In matlab.unittest.TestRunner/invokeTestContentOperatorMethod_ (line
> 1037) In matlab.unittest.plugins.TestRunnerPlugin/runTestSuite (line
> 229) In
> matlab.unittest.plugins.FailureDiagnosticsPlugin/runTestSuite (line
> 95) In
> matlab.unittest.plugins.TestRunnerPlugin/invokeTestContentOperatorMethod_
> (line 658) In matlab.unittest.plugins.TestRunnerPlugin/runTestSuite
> (line 229) In
> matlab.unittest.internal.plugins.ListenerAdderPlugin/runTestSuite
> (line 29) In
> matlab.unittest.plugins.DiagnosticsRecordingPlugin/runTestSuite (line
> 165) In
> matlab.unittest.plugins.TestRunnerPlugin/invokeTestContentOperatorMethod_
> (line 658) In matlab.unittest.TestRunner/evaluateMethodOnPlugins
> (line 895) In matlab.unittest.TestRunner/run (line 307) In
> runtests (line 90) .Warning: The steering commanded was set to the
> max steer allowed (Type "warning off MATLAB:CommandedSteerOutOfBounds"
> to suppress this warning.)
>
> > In getPointMass2D_dot (line 54) In tests_getPointMass2D_dot>testSteeringMax (line 73) In
> matlab.unittest.FunctionTestCase/test (line 98) In
> matlab.unittest.TestRunner/evaluateMethodCore (line 995) In
> matlab.unittest.TestRunner/evaluateMethodsOnTestContent (line 936)
> In matlab.unittest.TestRunner/runTestMethod (line 1287) In
> matlab.unittest.TestRunner/runTest (line 1244) In
> matlab.unittest.TestRunner/repeatTest (line 586) In
> matlab.unittest.TestRunner/runSharedTestCase (line 548) In
> matlab.unittest.TestRunner/runTestClass (line 1170) In
> matlab.unittest.TestRunner/invokeTestContentOperatorMethod_ (line
> 1037) In matlab.unittest.plugins.TestRunnerPlugin/runTestClass (line
> 378) In
> matlab.unittest.plugins.testrunprogress.ConciseProgressPlugin/runTestClass
> (line 68) In
> matlab.unittest.plugins.TestRunnerPlugin/invokeTestContentOperatorMethod_
> (line 658) In matlab.unittest.TestRunner/evaluateMethodOnPlugins
> (line 895) In matlab.unittest.TestRunner/runTestSuite (line 1102)
> In matlab.unittest.TestRunner/invokeTestContentOperatorMethod_ (line
> 1037) In matlab.unittest.plugins.TestRunnerPlugin/runTestSuite (line
> 229) In
> matlab.unittest.plugins.FailureDiagnosticsPlugin/runTestSuite (line
> 95) In
> matlab.unittest.plugins.TestRunnerPlugin/invokeTestContentOperatorMethod_
> (line 658) In matlab.unittest.plugins.TestRunnerPlugin/runTestSuite
> (line 229) In
> matlab.unittest.internal.plugins.ListenerAdderPlugin/runTestSuite
> (line 29) In
> matlab.unittest.plugins.DiagnosticsRecordingPlugin/runTestSuite (line
> 165) In
> matlab.unittest.plugins.TestRunnerPlugin/invokeTestContentOperatorMethod_
> (line 658) In matlab.unittest.TestRunner/evaluateMethodOnPlugins
> (line 895) In matlab.unittest.TestRunner/run (line 307) In
> runtests (line 90) .Warning: The steering commanded was set to the
> min steer allowed (Type "warning off MATLAB:CommandedSteerOutOfBounds"
> to suppress this warning.)
>
> > In getPointMass2D_dot (line 58) In tests_getPointMass2D_dot>testSteeringMin (line 82) In
> matlab.unittest.FunctionTestCase/test (line 98) In
> matlab.unittest.TestRunner/evaluateMethodCore (line 995) In
> matlab.unittest.TestRunner/evaluateMethodsOnTestContent (line 936)
> In matlab.unittest.TestRunner/runTestMethod (line 1287) In
> matlab.unittest.TestRunner/runTest (line 1244) In
> matlab.unittest.TestRunner/repeatTest (line 586) In
> matlab.unittest.TestRunner/runSharedTestCase (line 548) In
> matlab.unittest.TestRunner/runTestClass (line 1170) In
> matlab.unittest.TestRunner/invokeTestContentOperatorMethod_ (line
> 1037) In matlab.unittest.plugins.TestRunnerPlugin/runTestClass (line
> 378) In
> matlab.unittest.plugins.testrunprogress.ConciseProgressPlugin/runTestClass
> (line 68) In
> matlab.unittest.plugins.TestRunnerPlugin/invokeTestContentOperatorMethod_
> (line 658) In matlab.unittest.TestRunner/evaluateMethodOnPlugins
> (line 895) In matlab.unittest.TestRunner/runTestSuite (line 1102)
> In matlab.unittest.TestRunner/invokeTestContentOperatorMethod_ (line
> 1037) In matlab.unittest.plugins.TestRunnerPlugin/runTestSuite (line
> 229) In
> matlab.unittest.plugins.FailureDiagnosticsPlugin/runTestSuite (line
> 95) In
> matlab.unittest.plugins.TestRunnerPlugin/invokeTestContentOperatorMethod_
> (line 658) In matlab.unittest.plugins.TestRunnerPlugin/runTestSuite
> (line 229) In
> matlab.unittest.internal.plugins.ListenerAdderPlugin/runTestSuite
> (line 29) In
> matlab.unittest.plugins.DiagnosticsRecordingPlugin/runTestSuite (line
> 165) In
> matlab.unittest.plugins.TestRunnerPlugin/invokeTestContentOperatorMethod_
> (line 658) In matlab.unittest.TestRunner/evaluateMethodOnPlugins
> (line 895) In matlab.unittest.TestRunner/run (line 307) In
> runtests (line 90) . Done tests_getPointMass2D_dot
> __________
>
>
> Totals: 9 Passed, 0 Failed, 0 Incomplete.
> 0.20351 seconds testing time.