默认情况下,单元测试是否应使用“引发异常"? [英] Should unit test use 'throws Exception' by default?

查看:67
本文介绍了默认情况下,单元测试是否应使用“引发异常"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

换句话说,我应该在所有或大多数单元测试中附加throws Exception吗?当您使用Android Studio生成单元测试(命令N->测试方法)时,默认情况下会添加throw Exception.例如:

In other words should I append throws Exception to all or most of my unit tests? When you generate a unit test with Android Studio (command N -> Test Method) it adds throw Exception by default. ex:

@Test
public void someMethod() throws Exception {

}

推荐答案

我不认为有理由要求每个测试用例都声明要抛出Exception.

I don't think a compelling case could be made for demanding that every test case be declared to throw Exception.

对于引发特定异常的代码(例如,异常构成公共合同的一部分),您希望测试用例能够解决这些特定异常路径,并且使用根Exception可能会导致断言被简化为某种形式.类似,也许与测试通过简单地断言返回任何数字而不是断言返回正确数字的方法来计算数字的方法类似.

For code which throws specific exceptions (i.e. where the exceptions form part of the pubic contract) you would expect test cases to address those specific exception paths and use of the root Exception could result in assertions being demoted to something simplistic. Similar, perhaps, to testing a method which calculates a number by simply asserting that any number is returned rather than asserting that the correct number is returned.

此外,对于抛出检查异常的代码,默认语句throws Exception将掩盖被测代码抛出的特定检查异常.这会在编写测试时拒绝您提供有价值的提示或线索.例如,以下代码...

In addition, for code which throws a checked exception a default statement of throws Exception would mask specific, checked exceptions thrown by the code-under-test. This would deny you a valuable hint or clue when writing your test. For example, the following code ...

public void someMethod throws SomeMethodFailedException {
    ...
}

...可能会受益于一个测试案例,该案例明确测试了抛出SomeMethodFailedException的路径.如果默认情况下未使用throws Exception声明测试用例,则编译器将有效地提醒您测试抛出SomeMethodFailedException的路径.

... might benefit from a test case which explicitly tests the path where SomeMethodFailedException is thrown. If your test cases are not declared by default with throws Exception then the compiler will effectively act as a reminder to you to test for the path where SomeMethodFailedException is thrown.

因此,总而言之,声明所有测试都抛出Exception可能(无论是否有意地)导致测试用例侧步执行异常约定或忽略异常路径,并且一般而言,这两种策略都不可取.

So, in summary, declaring all tests to throw Exception could (whether intentionally or not) result in your test cases side stepping the exception contract or ignoring exception paths and, generally speaking, neither of those strategies would be desireable.

当然,上述问题并不是用throws Exception声明的必然结果,因为您可以在进行异常路径测试的同时拥有该声明,但是在所有测试中使用throws Exception确实会暗示(并且可能鼓励或验证) )一种不会将异常路径视为有意义的测试场景的测试方法.

Of course, the issues described above are not invariable outcomes of declaring with throws Exception since you can have that declaration whilst also testing exception paths but the use of throws Exception on all tests does hint at (and possibly encourage or validate) a test approach which does not treat exception paths as meaningful test scenarios.

如果您担心Android Studio中的默认模板会以这种方式创建测试,则可以更改该默认设置或创建自己的测试方法"快捷方式.

If you are concerned that the default template in Android Studio creates a test in this manner then you can change that default or create your own 'test method' shortcut.

这篇关于默认情况下,单元测试是否应使用“引发异常"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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