简体   繁体   English

单元测试是否应该默认使用“抛出异常”?

[英]Should unit test use 'throws Exception' by default?

In other words should I append throws Exception to all or most of my unit tests?换句话说,我应该将throws Exception附加到我的所有或大部分单元测试中吗? When you generate a unit test with Android Studio (command N -> Test Method) it adds throw Exception by default.当您使用 Android Studio(命令 N -> 测试方法)生成单元测试时,它默认添加 throw Exception。 ex:前任:

@Test
public void someMethod() throws 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 (ie 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.对于抛出特定异常的代码(即异常构成公共合同的一部分),您会期望测试用例解决那些特定的异常路径,并且使用根Exception可能导致断言被降级为简单化的东西。 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.可能类似于通过简单地断言返回任何数字而不是断言返回正确数字来测试计算数字的方法。

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.此外,对于抛出已检查异常的代码,throws throws Exception的默认语句将屏蔽被测试代码抛出的特定已检查异常。 This would deny you a valuable hint or clue when writing your test.在编写测试时,这将拒绝您提供有价值的提示或线索。 For example, the following code ...例如,下面的代码...

public void someMethod throws SomeMethodFailedException {
    ...
}

... might benefit from a test case which explicitly tests the path where SomeMethodFailedException is thrown. ...可能会受益于显式测试抛出SomeMethodFailedException的路径的SomeMethodFailedException 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.如果您的测试用例默认没有用throws Exception声明,那么编译器将有效地提醒您测试抛出SomeMethodFailedException的路径。

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.因此,总而言之,将所有测试声明为抛出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.当然,上述问题并不是使用throws Exception声明的不变结果,因为您可以在声明的同时测试异常路径,但是在所有测试中使用throws Exception确实暗示(并可能鼓励或验证)一种测试方法不将异常路径视为有意义的测试场景。

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.如果您担心 Android Studio 中的默认模板会以这种方式创建测试,那么您可以更改该默认值或创建您自己的“测试方法”快捷方式。

You should not use Exceptions too often in your tests, In fact you should not use them at all ( in most cases).你不应该在你的测试中经常使用异常,事实上你根本不应该使用它们(在大多数情况下)。

The unit test is for your functions to make sure that if they are working the way you want !单元测试是针对您的功能,以确保它们是否按您想要的方式工作!

So you should find a way to test your functions with assertations,which will be much more clear and effective test.所以你应该想办法用断言来测试你的函数,这样测试会更清晰有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM