Java JUnit 中的失败和错误有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3425995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:41:04  来源:igfitidea点击:

What's the difference between failure and error in JUnit?

javatestingjunit

提问by froadie

I'm running JUnit tests on a large code base, and I've been realizing that sometimes I get "Errors" while other times I get "Failures". What's the difference?

我正在大型代码库上运行 JUnit 测试,并且我一直意识到有时会出现“错误”,而有时会出现“失败”。有什么不同?

采纳答案by froadie

Ok, I've just noticed a pattern and think I've figured it out (correct me if I'm wrong). It seems to me that failures are when your test cases fail - i.e. your assertions are incorrect. Errors are unexpected errors that occur while trying to actually run the test - exceptions, etc.

好的,我刚刚注意到一个模式,并认为我已经弄清楚了(如果我错了,请纠正我)。在我看来,失败是当您的测试用例失败时 - 即您的断言不正确。错误是在尝试实际运行测试时发生的意外错误 - 异常等。

回答by Neel

If your test throws an exception which does not get bubbled up through the Assertion framework in Junit, it gets reported as an error. For example, a NullPointer, or a ClassNotFound exception will report an error:

如果您的测试抛出了未通过 Junit 中的断言框架冒泡的异常,则会将其报告为错误。例如,一个 NullPointer,或者一个 ClassNotFound 异常会报错:

String s = null;
s.trim();

or,

或者,

try {

    // your code
} catch(Exception e) {
    // log the exception
    throw new MyException(e);
}

Having said that, the following will report a failure:

话虽如此,以下将报告失败:

Assert.fail("Failure here");

or,

或者,

Assert.assertEquals(1, 2);

or even:

甚至:

throw new AssertionException(e);

It depends on the Junit version you are using. Junit 4- will make the distinction between a failure and an error, but Junit 4 simplifies it as failures only.

这取决于您使用的 Junit 版本。Junit 4- 将区分失败和错误,但 Junit 4 仅将其简化为失败。

Following link provides more interesting inputs:

以下链接提供了更多有趣的输入:

http://www.devx.com/Java/Article/31983/1763/page/2

http://www.devx.com/Java/Article/31983/1763/page/2

回答by Matias Elorriaga

From "Pragmatic Unit Testing in Java 8 with JUnit":

来自“使用 JUnit 在 Java 8 中进行实用单元测试”:

Assertions (or asserts) in JUnit are static method calls that you drop into your tests. Each assertion is an opportunity to verify that some condition holds true. If an asserted condition does not hold true, the test stops right there, and JUnit reports a test failure.

JUnit 中的断言(或断言)是您放入测试中的静态方法调用。每个断言都是验证某些条件成立的机会。如果断言的条件不成立,则测试会立即停止,并且 JUnit 会报告测试失败。

(It's also possible that when JUnit runs your test, an exception is thrown and not caught. In this case, JUnit reports a test error.)

(也有可能在 JUnit 运行您的测试时抛出异常而未捕获。在这种情况下,JUnit 报告测试错误。)

回答by Deen John

Below test explains the difference between Test Error vs Test failure.

下面的测试解释了Test Error 与 Test failure之间的区别。

I have commented the line which throws test error and test failure.

我已经评论了引发测试错误和测试失败的行。

    @Test
    public void testErrorVsTestFailure() {

        final String sampleString = null;

        assertEquals('j', sampleString.charAt(0) );
        //above line throws test error as you are trying to access charAt() method on null reference

        assertEquals(sampleString, "jacob");
        //above line throws Test failure as the actual value-a null , is not equal to expected value-string "jacob"
        }

So Junit shows test error whenever you get an exception , and test failure when your expected result value doesn't match your actual value

因此,每当您遇到异常时,Junit 都会显示测试错误,并在您的预期结果值与您的实际值不匹配时显示测试失败

回答by Roushan

Source class : JUnitReportReporter.java

源类:JUnitReportReporter.java

public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String defaultOutputDirectory) {
//......

            for (ITestResult tr : (Set) entry.getValue()) {
                TestTag testTag = new TestTag();

                boolean isSuccess = tr.getStatus() == 1;
                if (!(isSuccess)) {
                    if (tr.getThrowable() instanceof AssertionError)
                        ++errors;
                    else {
                        ++failures;
                    }
                }
}

As you can see below line in above method

正如你在上面的方法中看到的下面一行

tr.getThrowable() instanceof AssertionError

tr.getThrowable() 实例断言错误

errors count is increased when it is instance of AssertionError otherwise(any Throwable) is counted as failures.

当它是 AssertionError 的实例时,错误计数增加,否则(任何 Throwable)被计为失败。

回答by MiguelMunoz

You are right that failures come from the AssertionErrors thrown by the JUnit assertion methods, or by throwing an AssertionError, or by throwing an exception that you declared in your @Testannotation, and Errors come from other, unexpected Exceptions. But there's an important distinction between them:

您是正确的,失败来自 JUnit 断言方法抛出的 AssertionErrors,或抛出 AssertionError,或抛出您在@Test注释中声明的异常,而错误来自其他意外异常。但它们之间有一个重要的区别:

A failure means your test ran correctly, and identified a defect in your code.

失败意味着您的测试运行正确,并确定了代码中的缺陷。

An error could mean a bug in your code, but one that you weren't even testing for. It could also mean that the bug is in the test itself.

错误可能意味着您的代码中存在错误,但您甚至没有对其进行测试。这也可能意味着错误在测试本身中。

In short, a failure means you need to rewrite the code that is being tested. An error means that it may be the unit test that you need to rewrite. It may mean this even if the failure was in your code, such as a NullPointerException, because you detected a flaw that you weren't even testing for, so it might be wise to test for that.

简而言之,失败意味着您需要重写正在测试的代码。错误意味着它可能是您需要重写的单元测试。这可能意味着即使失败在您的代码中,例如 a NullPointerException,因为您检测到一个您甚至没有测试的缺陷,因此测试它可能是明智的。