测试异常的单元测试 [英] unit tests that test for exceptions

查看:199
本文介绍了测试异常的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在尝试编写单元测试以测试抛出解析异常.由于抛出了解析异常,我该如何在单元测试中失败.

首先,这是引发解析异常的代码:

       公共异步Task< int> ImportAsync(StreamReader reader,int projectId,IProgress< ParseProgress>进度)
        {
...

            var nodes = ParseData(progress);

           如果(_errorMessages.Count> 0)
            {
               字符串fullErrorMessage = _errorMessages.Aggregate((x,y)=> x +"\ n" + y);
                _errorMessages.Clear();
               抛出新的ParsingException(string.Empty,fullErrorMessage);
            }
...
}

此方法解析CSV文件中的数据.对ParseData(...)的调用实际上是在进行解析.每当它在解析过程中遇到问题时,就会将一条消息记录到_errorMessages中,即List< string>.错误消息.当它从ParseData(...)返回时, 它检查列表中的Count> 0.如果存在错误消息,它将引发ParseException,并将完整的错误消息列表(由'\ n'连接)作为异常消息.

我的单元测试使用以下方法开始设置:

           受保护的重写无效When()
            {
                SUT.ImportAsync(TestData,1,new Progress< ParseProgress>()).Wait();
            }

它传入TestData,其中包含一个moc CSV文件,其中一行包含我要测试的错误(即,当它击到该行时应抛出ParseException).

但是,此时会抛出ParseException的事实导致根据此设置失败的任何单元测试.

我该如何设置单元测试,以便在引发异常时不会失败,而实际上会检测在引发异常时将其识别为通过.

解决方案

我该如何设置单元测试,以便在引发异常时不会失败,而实际上会检测在引发异常时将其识别为通过.

如果未引发预期的异常,则测试失败.如果引发了预期的异常,则测试通过.

https://msdn.microsoft.com/en-us /library/microsoft.visualstudio.testtools.unittesting.expectedexceptionattribute.aspx


Hello,

I'm trying to write unit tests to test throwing of parse exceptions. How can I do this with the unit test failing BECAUSE of the throwing of parse exceptions.

First of all, here is the code that throws the parse exception:

        public async Task<int> ImportAsync(StreamReader reader, int projectId, IProgress<ParseProgress> progress)
        {
...

            var nodes = ParseData(progress);

            if (_errorMessages.Count > 0)
            {
                string fullErrorMessage = _errorMessages.Aggregate((x, y) => x + "\n" + y);
                _errorMessages.Clear();
                throw new ParsingException(string.Empty, fullErrorMessage);
            }
...
}

This method parses data from a CSV file. The call to ParseData(...) is what actually does the parsing. Whenever it encounters a problem during the parsing, it logs a message to _errorMessages, a List<string> of error messages. When it returns from ParseData(...), it checks the list for Count > 0. If error messages exist, it throws a ParseException with the full list of error messages concatenated by '\n' as the exception message.

My unit tests begin their setup with the following method:

            protected override void When()
            {
               SUT.ImportAsync(TestData, 1, new Progress<ParseProgress>()).Wait();
            }

It passes in TestData which contains a moc CSV file, one row of which contains the error I'd like to test for (i.e. it should throw a ParseException when it hits this row).

But the fact that it throws a ParseException at this point results in any unit tests depending on this setup failing.

How can I setup unit tests such that rather than failing when exceptions are thrown, it actually detects when exceptions are thrown and recognizes this as a pass.

解决方案

How can I setup unit tests such that rather than failing when exceptions are thrown, it actually detects when exceptions are thrown and recognizes this as a pass.

If the expected exception is not thrown, the test fails. If the expected exception is thrown, the test passes. 

https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.expectedexceptionattribute.aspx


这篇关于测试异常的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆