hello! i was wondering how i would go about giving a
kotlin.test.assertEquals
a list of valid expected outputs, like can i just pass it a list and if the output of whatever im testing is part of that list itll say the test passed?
v
Vampire
09/29/2020, 12:53 PM
No, a list is not equal to one of its members, that wouldn't really make much sense
Vampire
09/29/2020, 12:55 PM
You probably want
assertTrue
or
expect
y
yogi
09/29/2020, 1:05 PM
I believe the problem is to accept the output if it is a valid subset of the list of expected outputs.
eg: expectedOutput = [ 1, 2, 3, 4, 5]
resultOutput = [ 3, 4 ]
In such a case, one would want the test to pass.
If that is the problem statement, one quick way to do it would be to
Copy code
assertEquals (expectedOutput.intersect(resultOutput), resultOutput, "Expected $resultOutput to be a subset of $expectedOutput" )
v
Vampire
09/29/2020, 1:28 PM
Even if that would be the case (which I doubt) I think
assertTrue
would be better.
There is no need to do a full intersect when you just want to know whether one list contains the other. So it should be
assertTrue(expectedOutput.containsAll(resultOutput), "my message")
which can abort as soon as it found a discrepancy.
Or in the case how I understood it
assertTrue(expectedOutput.contains(resultOutput), "my message")
👍 2
g
Grian
09/29/2020, 1:36 PM
Thanks for the help guys! What i intended to ask originally was how to check if the output(a single int) was part of a list of valid answers, assertTrue works for what i need.
v
Vampire
09/29/2020, 1:37 PM
Yes, that's what I thought any my last line shows 🙂
c
Cedrick Cooke
09/29/2020, 4:43 PM
If this is a case that comes up often in your testing,
assertContains
and
assertContainsAll
with automatic error messages are likely worthwhile functions to create.