如何编写单元测试断言来检查具有指定标识符和特定消息的错误?

时间:2013-07-01 10:13:24

标签: matlab unit-testing

我正在使用在R2013a,matlab.unittest中引入的新的unit testing framework for MATLAB。我想写一个断言,发生以下两件事:

  1. 引发具有指定标识符的异常。
  2. 该异常的消息满足某些条件。
  3. 我找到了verifyError方法,但这似乎只允许我检查标识符或错误元类。

    另一个选项似乎是verifyThat Throws约束。这似乎更有希望,但Throws的文档显得有些稀疏,我无法弄清楚如何让它做我想做的事。

    我意识到我可以将消息文本添加到错误标识符中,但我真的不想这样做。消息文本来自使用mex文件调用的本机库。并且文本使用空格等格式化。文本可能会很长,并且会使错误标识符变得混乱。

    那么,是否有可能实现我想要的,如果是这样的话?

1 个答案:

答案 0 :(得分:10)

没有现成的功能。这是破解它的一种方法。

考虑我们正在测试的以下功能。它会在非数字输入上抛出特定错误:

increment.m

function out = increment(x)
    if ~isa(x,'numeric')
        error('increment:NonNumeric', 'Input must be numeric.');
    end
    out = x + 1;
end

这是单元测试代码:

IncrementTest.m

classdef IncrementTest < matlab.unittest.TestCase
    methods (Test)
        function testOutput(t)
            t.verifyEqual(increment(1), 2);
        end
        function testClass(t)
            t.verifyClass(increment(1), class(2));
        end
        function testErrId(t)
            t.verifyError(@()increment('1'), 'increment:NonNumeric');
        end

        function testErrIdMsg(t)
            % expected exception
            expectedME = MException('increment:NonNumeric', ...
                'Input must be numeric.');

            noErr = false;
            try
                [~] = increment('1');
                noErr = true;
            catch actualME
                % verify correct exception was thrown
                t.verifyEqual(actualME.identifier, expectedME.identifier, ...
                    'The function threw an exception with the wrong identifier.');
                t.verifyEqual(actualME.message, expectedME.message, ...
                    'The function threw an exception with the wrong message.');
            end

            % verify an exception was thrown
            t.verifyFalse(noErr, 'The function did not throw any exception.');
        end
    end
end

使用try / catch块的灵感来自Steve Eddins对旧版xUnit Test FrameworkassertExceptionThrown函数。 (更新:框架似乎已从文件交换中删除,我想鼓励使用新的内置框架。如果您感兴趣,这是旧xUnit的流行分支: psexton/matlab-xunit)。

如果您想在测试错误消息时提供更多灵活性,请使用verifyMatches来匹配使用正则表达式的字符串。


此外,如果您有冒险精神,可以学习matlab.unittest.constraints.Throws课程并创建own version除了错误ID之外还会检查错误消息。

ME = MException('error:id', 'message');

import matlab.unittest.constraints.Throws
%t.verifyThat(@myfcn, Throws(ME));
t.verifyThat(@myfcn, ThrowsWithId(ME));

其中ThrowsWithId是您的扩展版


编辑:

好,所以我浏览了matlab.unittest.constraints.Throws的代码,并实施了custom Constraint课程。

该类与Throws类似。它需要一个MException实例作为输入,并检查被测试的函数句柄是否抛出类似的异常(检查错误ID和消息)。它可以与任何断言方法一起使用:

  • testCase.assertThat(@fcn, ThrowsErr(ME))
  • testCase.assumeThat(@fcn, ThrowsErr(ME))
  • testCase.fatalAssertThat(@fcn, ThrowsErr(ME))
  • testCase.verifyThat(@fcn, ThrowsErr(ME))

要从抽象类matlab.unittest.constraints.Constraint创建子类,我们必须实现接口的两个函数:satisfiedBygetDiagnosticFor。另请注意,我们继承自另一个抽象类FunctionHandleConstraint,因为它提供了一些辅助方法来处理函数句柄。

构造函数接受预期的异常(作为MException实例),以及一个可选的输入,指定用于调用正在测试的函数句柄的输出参数的数量。

代码:

classdef ThrowsErr < matlab.unittest.internal.constraints.FunctionHandleConstraint
    %THROWSERR  Constraint specifying a function handle that throws an MException
    %
    % See also: matlab.unittest.constraints.Throws

    properties (SetAccess = private)
        ExpectedException;
        FcnNargout;
    end

    properties (Access = private)
        ActualException = MException.empty;
    end

    methods
        function constraint = ThrowsErr(exception, numargout)
            narginchk(1,2);
            if nargin < 2, numargout = 0; end
            validateattributes(exception, {'MException'}, {'scalar'}, '', 'exception');
            validateattributes(numargout, {'numeric'}, {'scalar', '>=',0, 'nonnegative', 'integer'}, '', 'numargout');
            constraint.ExpectedException = exception;
            constraint.FcnNargout = numargout;
        end
    end

    %% overriden methods for Constraint class
    methods
        function tf = satisfiedBy(constraint, actual)
            tf = false;
            % check that we have a function handle
            if ~constraint.isFunction(actual)
                return
            end
            % execute function (remembering that its been called)
            constraint.invoke(actual);
            % check if it never threw an exception
            if ~constraint.HasThrownAnException()
                return
            end
            % check if it threw the wrong exception
            if ~constraint.HasThrownExpectedException()
                return
            end
            % if we made it here then we passed
            tf = true;
        end

        function diag = getDiagnosticFor(constraint, actual)
            % check that we have a function handle
            if ~constraint.isFunction(actual)
                diag = constraint.getDiagnosticFor@matlab.unittest.internal.constraints.FunctionHandleConstraint(actual);
                return
            end
            % check if we need to execute function
            if constraint.shouldInvoke(actual)
                constraint.invoke(actual);
            end
            % check if it never threw an exception
            if ~constraint.HasThrownAnException()
                diag = constraint.FailingDiagnostic_NoException();
                return
            end
            % check if it threw the wrong exception
            if ~constraint.HasThrownExpectedException()
                diag = constraint.FailingDiagnostic_WrongException();
                return
            end
            % if we made it here then we passed
            diag = PassingDiagnostic(constraint);
        end
    end

    %% overriden methods for FunctionHandleConstraint class
    methods (Hidden, Access = protected)
        function invoke(constraint, fcn)
            outputs = cell(1,constraint.FcnNargout);
            try
                [outputs{:}] = constraint.invoke@matlab.unittest.internal.constraints.FunctionHandleConstraint(fcn);
                constraint.ActualException = MException.empty;
            catch ex
                constraint.ActualException =  ex;
            end
        end
    end

    %% private helper functions
    methods (Access = private)
        function tf = HasThrownAnException(constraint)
            tf = ~isempty(constraint.ActualException);
        end

        function tf = HasThrownExpectedException(constraint)
            tf = metaclass(constraint.ActualException) <= metaclass(constraint.ExpectedException) && ...
                strcmp(constraint.ActualException.identifier, constraint.ExpectedException.identifier) && ...
                strcmp(constraint.ActualException.message, constraint.ExpectedException.message);
        end

        function diag = FailingDiagnostic_NoException(constraint)
            import matlab.unittest.internal.diagnostics.ConstraintDiagnosticFactory;
            import matlab.unittest.internal.diagnostics.DiagnosticSense;
            subDiag = ConstraintDiagnosticFactory.generateFailingDiagnostic(...
                constraint, DiagnosticSense.Positive);
            subDiag.DisplayDescription = true;
            subDiag.Description = 'The function did not throw any exception.';
            subDiag.DisplayExpVal = true;
            subDiag.ExpValHeader = 'Expected exception:';
            subDiag.ExpVal = sprintf('id  = ''%s''\nmsg = ''%s''', ...
                constraint.ExpectedException.identifier, ...
                constraint.ExpectedException.message);
            diag = constraint.generateFailingFcnDiagnostic(DiagnosticSense.Positive);
            diag.addCondition(subDiag);
        end

        function diag = FailingDiagnostic_WrongException(constraint)
            import matlab.unittest.internal.diagnostics.ConstraintDiagnosticFactory;
            import matlab.unittest.internal.diagnostics.DiagnosticSense;
            if strcmp(constraint.ActualException.identifier, constraint.ExpectedException.identifier)
                field = 'message';
            else
                field = 'identifier';
            end
            subDiag =  ConstraintDiagnosticFactory.generateFailingDiagnostic(...
                constraint, DiagnosticSense.Positive, ...
                sprintf('''%s''',constraint.ActualException.(field)), ...
                sprintf('''%s''',constraint.ExpectedException.(field)));
            subDiag.DisplayDescription = true;
            subDiag.Description = sprintf('The function threw an exception with the wrong %s.',field);
            subDiag.DisplayActVal = true;
            subDiag.DisplayExpVal = true;
            subDiag.ActValHeader = sprintf('Actual %s:',field);
            subDiag.ExpValHeader = sprintf('Expected %s:',field);
            diag = constraint.generateFailingFcnDiagnostic(DiagnosticSense.Positive);
            diag.addCondition(subDiag);
        end

        function diag = PassingDiagnostic(constraint)
            import matlab.unittest.internal.diagnostics.ConstraintDiagnosticFactory;
            import matlab.unittest.internal.diagnostics.DiagnosticSense;
            subDiag = ConstraintDiagnosticFactory.generatePassingDiagnostic(...
                constraint, DiagnosticSense.Positive);
            subDiag.DisplayExpVal = true;
            subDiag.ExpValHeader = 'Expected exception:';
            subDiag.ExpVal = sprintf('id  = ''%s''\nmsg = ''%s''', ...
                constraint.ExpectedException.identifier, ...
                constraint.ExpectedException.message);
            diag = constraint.generatePassingFcnDiagnostic(DiagnosticSense.Positive);
            diag.addCondition(subDiag);
        end
    end

end

以下是一个示例用法(使用与之前相同的功能):

t = matlab.unittest.TestCase.forInteractiveUse;

ME = MException('increment:NonNumeric', 'Input must be numeric.');
t.verifyThat(@()increment('5'), ThrowsErr(ME))

ME = MException('MATLAB:TooManyOutputs', 'Too many output arguments.');
t.verifyThat(@()increment(5), ThrowsErr(ME,2))

UPDATE:

自从我发布这个答案后,一些类的内部发生了一些变化。我更新了上面的代码以使用最新的MATLAB R2016a。如果您想要旧版本,请参阅修订历史记录。