在Matlab中成功进行单元测试时显示消息

时间:2014-03-30 14:27:19

标签: matlab unit-testing

有单向测试通过时显示消息的方法吗?比如它正在测试什么。

我知道我可以在传递

时显示消息
function testOne (testCase)
% some test here
msg = 'This will show what it fails';
             testCase.assertEqual(properties(Object), expProp, msg);  
end

2 个答案:

答案 0 :(得分:2)

扩展kyamagu的评论,假设您正在使用R2014a,您可以编写一个监听AssertionPassed事件的监听器。此侦听器是一个函数,它接受源对象(TestCase实例)和event data实例,其中包含有关断言的信息,如实际值,使用的约束和用户传递的diagnsotics。如果你在一次测试中这样做,你可以直接在测试中添加这个监听器

methods(Test)
    function testOne (testCase)
        testCase.addlistener('AssertionPassed', ...
            @(src,evd) disp('This will show what it succeeds'));

        % some test here
        msg = 'This will show what it fails';
        testCase.assertEqual(properties(Object), expProp, msg);  
    end
end

如果您想展示每个断言或验证的成功,您可以通过writing your own plugin获得您想要的内容。插件获得正确的TestCase实例,并可以使用它们为传递和失败的资格添加这些侦听器。编写插件后,可以将其安装到TestRunner上,并能够获得所有断言,验证等所需的行为。

答案 1 :(得分:0)

helpdlg怎么样?

function testOne (testCase)
% some test here, result is boolean in variable Test
if Test
msg = 'This will show what it succeeds';
helpdlg(msg);
else
msg = 'This will show what it fails';
testCase.assertEqual(properties(Object), expProp, msg);  
end
end

你可以为它添加一个计时器,以便在一段时间后消失...如果内联命令是对象,则disp也是一个解决方案......

相关问题