简体   繁体   English

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

[英]assertEquals, what is actual and what is expected?

I always wondered what exactly is the meaning of actual and expected in assertEquals in libraries like TestNG. 我总是想知道像TestNG这样的库中assertEquals的实际和预期的含义究竟是什么。

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 expected value is the known one, so the one we expect, and the actual one is the one we want to verify. 根据我的理解, expected值是已知的值,因此我们期望的值和actual是我们想要验证的值。 For example, assume we want to test a function fooBar that always has to return 56 . 例如,假设我们要测试一个总是必须返回56的函数fooBar

In such a case I would do: assertEquals(sth.fooBar(), 56) . 在这种情况下,我会这样做: assertEquals(sth.fooBar(), 56) But with a quick search on GitHub it seems people do it the other way around, so assertEquals(56, sth.fooBar()) . 但是通过快速搜索GitHub ,似乎人们会assertEquals(56, sth.fooBar())这样做,所以assertEquals(56, sth.fooBar()) But how can the expected value be sth.fooBar() when we don't even know that value? 但是当我们甚至不知道这个值时,期望值怎么能是sth.fooBar()呢? It seems that sth.fooBar() is the actual value which we compare against the expected which we already know. 似乎sth.fooBar()是我们与我们已经知道的预期值进行比较的实际值。

I know there is no difference of the correctness of a test but I would like to follow the "correct" way. 我知道测试的正确性没有区别,但我想遵循“正确”的方式。

Most testing frameworks (the xUnit family) are based on the JUnit framework. 大多数测试框架(xUnit系列)都基于JUnit框架。 The Assert family of functions in JUnit have the (expected, actual) format; JUnit中的Assert函数族具有(expected, actual)格式; it became a convention, and most of the other frameworks followed that convention. 它成为一种惯例,大多数其他框架都遵循该惯例。

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. 如果使用JUnit,请将期望值放在第一位。 If you use TestNG, put the actual value first. 如果您使用TestNG,请先将实际值放入。 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 reversed assertEquals(ShouldBeTrueButReturnsFalse(), true) in JUnit fails, the default message says " expected [false] but found [true] ", where it should have said " expected [true] but found [false] ". 当你在JUnit中反转的 assertEquals(ShouldBeTrueButReturnsFalse(), true)失败时,默认消息显示“ expected [false]但是找到[true] ”,它应该说“ expected [true]但是找到[false] ”。 This is confusing, to say the least, and you shouldn't have to deal with a possible misdirection of that message. 至少可以说这是令人困惑的,你不应该处理可能误导的消息。

Some of the unit tests in the Github link you provide don't follow the convention and have the same problem. 您提供的Github链接中的某些单元测试不符合约定并且具有相同的问题。 Don't do that. 不要那样做。 Stick to the convention of your framework . 坚持你的框架的惯例

Answer is simple. 答案很简单。 JUnit has reverse order of arguments. JUnit具有相反的参数顺序。 Refer the example below: 请参考以下示例:

JUnit: JUnit的:

void assertEquals(Object expected, Object actual)

TestNG: TestNG的:

void assertEquals(int actual, int expected)

I too had the same confusion. 我也有同样的困惑。

But the confusion is because the Github search returns the assertEquals syntax for both TestNG and junit. 但混淆是因为Github搜索返回了TestNG和junit的assertEquals语法。 You are correct with your expected & actual understanding. 您的预期和实际理解是正确的。

TestNG: assertEquals(Object actual, Object expected) TestNG: assertEquals(Object actual, Object expected)

junit: assertEquals(Object expected, Object actual) junit: assertEquals(Object expected, Object actual)

Eg., in testNG result of below code 例如,在testNG下面的代码结果

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

is: 是:

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

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>

暂无
暂无

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

相关问题 当实际值和预期值匹配时,在什么情况下 AssertEquals 会失败? - Under what circumstances would AssertEquals fail when the actual and expected values match? 在jUnit中计算assertEquals()方法中expected_value的最佳方法是什么 - what is the best way to calculate the expected_value in assertEquals() method in jUnit assertEquals(expected, actual, delta) 比较矩阵中的浮点数 - assertEquals(expected, actual, delta) to compare floating-point numbers in a matrix 当使用assertEquals时,应该捕获哪种异常? - What kind of exception should go in the catch with assertEquals? 在数组中测试“ getCustomer”方法时,期望值和实际值是什么? - What should be expected and actual values when testing the “getCustomer” method in the array? Selenium Web驱动程序-Java-TestNG-Assert.assertEquals如何将实际结果与预期结果之间的范围进行比较 - Selenium Web Driver - Java - TestNG - Assert.assertEquals how to compare actual result with the range between expected result 如何知道什么导致assertEquals方法在单元测试中失败? - How to know what causes an assertEquals method to fail in unit tests? 断言列表时assertEquals究竟检查了什么? - What exactly does assertEquals check for when asserting lists? 当我们使用 WebElement 的 obj 方法时,TestNG 的 SoftAsert.assertEquals(actual,expected) 方法无法正常工作。getCssValue(“font-family”) - SoftAsert.assertEquals(actual,expected) method of TestNG is not functioning properly when we use WebElement's obj method .getCssValue(“font-family”) “批处理更新从更新[0]返回意外行数”的含义是什么? 实际行数:2; 预期:1”平均 - What does “Batch update returned unexpected row count from update [0]; actual row count: 2; expected: 1” mean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM