Java assertEquals,什么是实际的,什么是预期的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26102865/
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-11 01:51:10  来源:igfitidea点击:

assertEquals, what is actual and what is expected?

javaunit-testingtestingtestngjunit4

提问by insumity

I always wondered what exactly is the meaning of actual and expected in assertEqualsin libraries like TestNG.

我一直想知道assertEquals在像 TestNG 这样的库中实际和预期的确切含义是什么。

If we read the Java Docs we see:

如果我们阅读 Java Docs,我们会看到:

public static void assertEquals(... actual, ... expected)
Parameters:
    actual - the actual value
    expected - the expected value

From my understanding the expectedvalue is the known one, so the one we expect, and the actualone is the one we want to verify. For example, assume we want to test a function fooBarthat always has to return 56.

根据我的理解,expected值是已知的,所以我们期望的,actual也是我们想要验证的。例如,假设我们要测试一个fooBar总是必须返回的函数56

In such a case I would do: assertEquals(sth.fooBar(), 56). But with a quick search on GitHubit seems people do it the other way around, so assertEquals(56, sth.fooBar()). But how can the expected value be sth.fooBar()when we don't even know that value? It seems that sth.fooBar()is the actual value which we compare against the expected which we already know.

在这种情况下,我会做:assertEquals(sth.fooBar(), 56)。但是通过在GitHub 上快速搜索,人们似乎反其道而行之,所以assertEquals(56, sth.fooBar()). 但是,sth.fooBar()当我们甚至不知道该值时,预期值怎么可能呢?这似乎sth.fooBar()是我们将实际值与我们已经知道的预期值进行比较。

I know there is no difference of the correctness of a test but I would like to follow the "correct" way.

我知道测试的正确性没有区别,但我想遵循“正确”的方式。

采纳答案by Patrice Gahide

Most testing frameworks (the xUnit family) are based on the JUnit framework. The Assertfamily of functions in JUnit have the (expected, actual)format; it became a convention, and most of the other frameworks followed that convention.

大多数测试框架(xUnit 系列)都基于 JUnit 框架。将Assert在JUnit的系列函数有(expected, actual)格式; 它成为一项公约,大多数其他框架都遵循该公约。

Some frameworks (like TestNG or NUnit 2.4+ for .NET) reversed that order (with the use of a constraint-based model for NUnit) to increase readability ("make sure that the actual value is 56" feels more natural than "make sure that 56 is the actual value").

一些框架(如 TestNG 或 NUnit 2.4+ for .NET)颠倒了这个顺序(使用基于约束的 NUnit 模型)以提高可读性(“确保实际值为 56”感觉比“确保56 是实际值")。

The bottom line is: stick to the convention of your framework. If you use JUnit, put the expected value first. If you use TestNG, put the actual value first. You're right, that makes no difference in the test results when you accidentally reverse the arguments. But it makes a big difference in the default message you get from a failing test. When your reversedassertEquals(ShouldBeTrueButReturnsFalse(), true)in JUnit fails, the default message says "expected [false] but found [true]", where it should have said "expected [true] but found [false]". This is confusing, to say the least, and you shouldn't have to deal with a possible misdirection of that message.

底线是:坚持框架的约定。如果您使用 JUnit,请将期望值放在首位。如果您使用 TestNG,请将实际值放在首位。您是对的,当您不小心颠倒参数时,这对测试结果没有影响。但是它对您从失败的测试中获得的默认消息产生了很大的影响。当您在 JUnit 中反转assertEquals(ShouldBeTrueButReturnsFalse(), true)失败时,默认消息会显示“预期 [假] 但找到 [真]”,它应该说“预期 [真] 但找到 [假]”。至少可以说,这令人困惑,您不应该处理可能对该消息的误导。

Some of the unit tests in the Github link you provide don't follow the convention and have the same problem. Don't do that. Stick to the convention of your framework.

您提供的 Github 链接中的某些单元测试不遵循约定并且存在相同的问题。不要那样做。坚持框架的约定

回答by talex

Answer is simple. JUnit has reverse order of arguments. Refer the example below:

答案很简单。JUnit 的参数顺序相反。请参考以下示例:

JUnit:

单位:

void assertEquals(Object expected, Object actual)

void assertEquals(Object expected, Object actual)

TestNG:

测试NG:

void assertEquals(int actual, int expected)

void assertEquals(int actual, int expected)

回答by Alberto Cerqueira

You can use:

您可以使用:

String expectedTitles[] = {"value-1", "value-2", "value-3". "value-14")};
List<String> expectedTitlesList = Arrays.asList(expectedTitles);
Assert.assertTrue(expectedTitlesList.contains(("value-to-compare")));

with maven:

与 Maven:

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.4</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-all</artifactId>
    <version>1.3</version>
</dependency>

回答by sri

I too had the same confusion.

我也有同样的困惑。

But the confusion is because the Github search returns the assertEquals syntax for both TestNG and junit. You are correct with your expected & actual understanding.

但混淆是因为 Github 搜索返回 TestNG 和 junit 的 assertEquals 语法。您的预期和实际理解是正确的。

TestNG:assertEquals(Object actual, Object expected)

测试NG:assertEquals(Object actual, Object expected)

junit:assertEquals(Object expected, Object actual)

朱尼特:assertEquals(Object expected, Object actual)

Eg., in testNG result of below code

例如,在下面代码的 testNG 结果中

int x=1+2; assertEquals(x,2);

int x=1+2; assertEquals(x,2);

is:

是:

java.lang.AssertionError: expected [2] but found [3]
Expected :2
Actual   :3