没有断言的单元测试 [英] Unit Testing without Assertions

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

问题描述

有时候,我遇到一个没有断言任何内容的单元测试.我今天早上遇到的一个特定示例是测试是否在满足条件时写入了日志文件.假设是如果没有错误抛出,则测试通过.

Occasionally I come accross a unit test that doesn't Assert anything. The particular example I came across this morning was testing that a log file got written to when a condition was met. The assumption was that if no error was thrown the test passed.

我个人对此没有问题,但是编写没有任何断言的单元测试似乎有点代码味".

I personally don't have a problem with this, however it seems to be a bit of a "code smell" to write a unit test that doesn't have any assertions associated with it.

只是想知道人们对此有何看法?

Just wondering what people's views on this are?

推荐答案

这将是官方的方法:

// Act
Exception ex = Record.Exception(() => someCode());

// Assert
Assert.Null(ex);

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

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