What is the “right” way to check the output of Java methods?

In Python, I often have tests that look something like this:

tests = [
    (2, 4),
    (3, 9),
    (10, 100),
]
for (input, expected_output) in tests:
    assert f(input) == expected_output

What is the “correct” way to write such tests (where a set of test cases is given, then the loop starts each of them) in Java using JUnit?

Thanks!

Proactive response . I understand that I can do something like:

assertEquals(4, f(2))
assertEquals(9, f(3))
....

But ... I hope there is a better way.

+5
source share
7 answers

Same.

    int[][] tests = {
            {2, 4},
            {3, 9},
            {10, 100}
    };
    for (int[] test : tests) {
        assertEquals(test[1], f(test[0]));
    }

Of course, not as beautiful as python, but little can be done.

You can also look at JUnit Theories , a future feature ...

+11
source

assert, .

, , , ( ).

, , .

+5

...

int[][] tests = new int[][]{
        {2, 4},
        {3, 9},
        {10, 100},
    };

for(int[] i : tests)
{
    assertEquals(i[1], f(i[0]);
}

, . , Java , Object [], Tuple.

+1

, , , Python?

0

Java , Map /, , Python.

0

, , , (, NUnit-GUI #). , , , . , , .

0
source

All Articles