简体   繁体   English

为什么TestNG允许多个预期的例外?

[英]Why does TestNG allow several expected exceptions?

Why does TestNG have the possibility to check if one of several exceptions are thrown? 为什么TestNG有可能检查是否抛出了几个异常中的一个 As far as I know, JUnit only supports one expected exception. 据我所知,JUnit只支持一个预期的异常。 Consider the following TestNG dummy examples where both tests will pass: 考虑以下两个测试都将通过的TestNG虚拟示例:

@Test(expectedExceptions = { NullPointerException.class, IllegalArgumentException.class })
public void throwsNullPointer() {
    throw new NullPointerException();
}

@Test(expectedExceptions = { NullPointerException.class, IllegalArgumentException.class })
public void throwsIllegalArgument() {
    throw new IllegalArgumentException();
}

My initial feeling is that it should be possible to derive from the code under test exactly which exception that is expected. 我最初的感觉是,应该可以从测试中的代码中精确地推导出预期的异常。 However, there must be some design decision from the people behind TestNG. 但是,TestNG背后的人必须做出一些设计决定。

Is it perhaps support for testing code with random features that cannot be mocked away? 它是否支持测试具有无法模拟的随机功能的代码? Does anybody have an idea, and preferably a real-life scenario? 有没有人有想法,最好是现实生活场景?

Why does TestNG allow several expected exceptions? 为什么TestNG允许多个预期的例外?

I think that the most likely reason is that people asked for the feature ... and it is a reasonable thing to provide. 我认为最可能的原因是人们要求提供这个功能......这是合理的。

I can think of a few use-cases. 我可以想到一些用例。

  • It may be needed when writing tests for code that is non-deterministic, and the non-determinism affects the exceptions thrown. 在编写非确定性代码的测试时可能需要它,而非确定性会影响抛出的异常。

  • It may be needed when testing multiple implementations of an API which may behave differently wrt to thrown exceptions. 在测试API的多个实现时可能需要它,这些实现可能与抛出异常的行为不同。 The implementations could be different classes or different versions of the same class. 实现可以是不同的类或同一类的不同版本。

  • It may be needed when testing code that depends on 3rd-party software, and has to cope with multiple versions of that software with different behaviour that is visible to the test. 在测试依赖于第三方软件的代码时可能需要它,并且必须处理具有测试可见的不同行为的该软件的多个版本。

  • It may be needed when doing "black box" testing of an API where the interface specification is unclear. 在对接口规范不清楚的API进行“黑盒子”测试时可能需要它。


My initial feeling is that it should be possible to derive from the code under test exactly which exception that is expected. 我最初的感觉是,应该可以从测试中的代码中精确地推导出预期的异常。

That assumes that you have access to the source code. 这假设您可以访问源代码。 Also, adapting the tests to the code is missing the point that you should be testing against the specification rather than the code. 此外,使测试适应代码缺少您应该针对规范而不是代码进行测试的要点。

If you have the option of allowing multiple exceptions, you avoid this. 如果您可以选择允许多个例外,则可以避免这种情况。 (And you don't have this option, then you (the test writer) have to catch and test for exceptions within the testcase ... if multiple exceptions are possible.) (并且您没有此选项,那么您(测试编写者)必须捕获并测试测试用例中的异常...如果可能存在多个异常。)

It's not uncommon for code to be able to throw several types of exceptions, which is one of the reasons why it's possible to collapse such exceptions in one catch statement: 代码能够抛出几种类型的异常并不罕见,这也是为什么可以在一个catch语句中崩溃这些异常的原因之一:

catch(IOException | InterruptedException ex) {

Therefore, it felt natural to me to allow expectedExceptions to allow multiple exceptions. 因此,我觉得允许expectedExceptions允许多个异常是很自然的。

I don't use this very often, to be honest, and it complicates a feature I added a little later which lets you test that the message of the exception matches a regular exception. 我不经常使用它,说实话,它使我稍后添加的功能变得复杂,这使您可以测试异常消息是否与常规异常匹配。 It's probably one of these features that I would implement differently if I could redo it. 如果我可以重做它,我可能会以不同的方式实现这些功能之一。

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

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