https://kotlinlang.org logo
Title
i

Ioannis Mavroukakis

02/17/2023, 12:45 PM
Hello folks! I have a question with regards to clues - certain other frameworks, most notably Spock and Hamcrest, when an assertion fails they show the data under test that failed, by default. Is there any way to do this in Kotest without having to wrap everything in clues?
c

CLOVIS

02/17/2023, 12:58 PM
Isn't this already the case by default? Do you have an example of a test that doesn't behave like this?
i

Ioannis Mavroukakis

02/17/2023, 1:08 PM
test("test list contains all elements"){
            val strings = listOf("One", "two", "three")
            strings.shouldContainAll("One", "two", "three","four")
        }
gives me Collection should contain all of ["One", "two", "three", "four"] but was missing ["four"]
to make it into a better example,
test("test list contains all elements"){
            val strings = listOf("One", "two", "three","five")
            strings.shouldContainAll("One", "two", "three","four")
        }
kotest tells me that
four
is missing, but I have no clue that a
five
has snuck in
on the other hand `
shouldContainExactly
behaves in the expected way
am I the fool here?
c

CLOVIS

02/17/2023, 1:11 PM
Well, you're asking Kotest to check that these elements are in the list, you did not say you don't want any other elements
In this case I think what you want to test is
shouldContainExactly
, or even just
shouldBe
i

Ioannis Mavroukakis

02/17/2023, 1:16 PM
that's not quite what I want though 🙂 I want feedback on what is there, irrespective of the matcher.
(without having to write clues boilerplate)
c

CLOVIS

02/17/2023, 1:17 PM
If you use
assertSoftly
it creates the clue automatically IIRC
i

Ioannis Mavroukakis

02/17/2023, 1:23 PM
you mean like so?
assertSoftly(strings){
                shouldContainAll("One", "two", "three","four")
            }
c

CLOVIS

02/17/2023, 1:23 PM
Yes, I think this prints the entire list? I'm not 100% sure
i

Ioannis Mavroukakis

02/17/2023, 1:23 PM
trying it out..
no 😞
c

CLOVIS

02/17/2023, 1:24 PM
Ah, it could probably be a feature request to have
assertSoftly
imply
withClue
i

Ioannis Mavroukakis

02/17/2023, 1:27 PM
yeah probably.even with
asClue
the output is nowhere near as nice by default as with the
shouldContainExactly
[One, two, three, five]
java.lang.AssertionError: [One, two, three, five]
just trying to save keystrokes and frustration 😆
thanks Ivan, appreciate the insight
c

CLOVIS

02/17/2023, 1:53 PM
I created an issue for it, please upvote 🙂 https://github.com/kotest/kotest/issues/3418