简体   繁体   English

断言导致JUnit测试停止

[英]Assert statement causing JUnit tests to stop

In the class I am testing, there are a few assert statements that check for various conditions. 在我正在测试的类中,有一些assert语句可以检查各种条件。

One of the methods is 方法之一是

GetNames(string id){
    assert(! id.Equals("")); // Causes all junit tests to stop
    ...
}

and there is an assert statement to check if id is not blank. 并且有一个assert语句来检查id是否为空。

In my unit tests, I have one test where I pass it a blank string, and a few others. 在单元测试中,我有一个测试,在该测试中将空白字符串传递给它,还有一些其他测试。 The problem is that when the assert statement gets executed in the class, the JUnit tests stop running after that test case. 问题在于,当在该类中执行assert语句时,JUnit测试将在该测试用例之后停止运行。

How can I change/setup the unit tests so that if there is an assertion failure in the class, the tests do not stop. 如何更改/设置单元测试,以便在类中断言失败时,测试不会停止。

I have to enable the assertions in order for the code to run properly since there are cases where variables are incremented in the assertions. 我必须启用断言以使代码正常运行,因为在某些情况下,变量在断言中会增加。 Unfortunately, I cannot change any of the code. 不幸的是,我无法更改任何代码。

I am using the JUnit plugin in eclipse. 我在Eclipse中使用JUnit插件。 I do not have any code in the setup and teardown sections. 在设置和拆卸部分中没有任何代码。

If you have assert statements in the code being tested, and assertions are enabled , then those statements will be causing an AssertionError to be thrown. 如果在要测试的代码中有assert语句,并且启用了 assert ,则这些语句将导致引发AssertionError You can catch that in your test code and ignore it: 您可以在测试代码中捕获它并忽略它:

try {
    xxx.GetNames("");
} catch (AssertionError e) {
    // this is expected - ignore it
}

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

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