Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

If we read the Java Docs we see:

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. For example, assume we want to test a function fooBar that always has to return 56.

In such a case I would do: 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()). 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.

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

like image 622
insumity Avatar asked Sep 29 '14 14:09

insumity


People also ask

What is expected and actual in JUnit?

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.

What is expected actual?

The ratio of actual deaths in a specific population to those predicted by a defined mortality table. The A/E ratio is a common measure of goodness of fit of a model to a given set of data and is a key component of an experience analysis.

What is the correct syntax to check whether the expected and actual value are equal?

Above method must be used if arrays have the same length, for each valid value for i, you can check it as given below: assertEquals(expected[i],actual[i]) assertArrayEquals(expected[i],actual[i])

Which assertion should you use to compare an expected value to an actual value?

#1) assertEquals This method is used to assert if two data values are equal. We can compare the values of different data types like string, boolean, integer. etc. Whenever the expected and actual values are the same, then the assertion passes with no exception.


2 Answers

Most testing frameworks (the xUnit family) are based on the JUnit framework. The Assert family of functions in JUnit have the (expected, actual) format; 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").

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 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]". 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. Don't do that. Stick to the convention of your framework.

like image 84
Patrice Gahide Avatar answered Oct 12 '22 02:10

Patrice Gahide


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

JUnit:

void assertEquals(Object expected, Object actual)

TestNG:

void assertEquals(int actual, int expected)

like image 9
talex Avatar answered Oct 12 '22 01:10

talex