https://kotlinlang.org logo
#kotest
Title
# kotest
d

dimsuz

08/03/2021, 11:25 PM
This might be more of a Junit question, but maybe kotest has something to help me too. I use Property Testing and I test on some graph-like inputs. Let's say I also have some function which can convert those graphs into a pretty printed text or a png file or a graphviz-dot file. These graphs might be quite big and so having them printed out would mess up output pretty badly. So when the test fails I'd like to see something like
Test failed for /tmp/file1.png (shrunk from /tmp/file2.png)
(links would be clickable in IDE) I see no such mechanism to provide "custom artifacts" either in JUnit or in Kotest, am I right? I found only junit xml reports which looks quite off-putting for me :)
s

sam

08/03/2021, 11:59 PM
So do you just want to do something when a test fails ?
t

Tom Haywood

08/04/2021, 5:14 AM
For a similar situation I convert long multiline text into a list of strings, and then compare each string in the list. It works, but not perfect.
d

dimsuz

08/04/2021, 10:20 AM
Yep, I want to perform an IO side-effect so to speak. And another thing I'd like is to have it somehow integrated into the output (file name I could click to open etc), but that's a separate thing I guess.
s

sam

08/04/2021, 10:22 AM
Would a test listener do what you want
d

dimsuz

08/04/2021, 10:27 AM
I will check that out thanks! I saw them in the documentation, didn't dig in.
Ok, test listeners look like the step in the right direction, but I still somehow have to pass them the "big-complex-object" (graph) for the listener to serialize it. I've tried to use "withClue(graph)", hoping that it'll somehow end up in the assert-exception instance, but it is just being toString-ed. Any advice perhaps? Basically imagine that I do this test:
Copy code
graph.someProperty shouldBe 3
and when it fails I want to print whole graph to the file inside a test listener.
s

sam

08/04/2021, 11:28 PM
you could capture the exception, use it, then re-throw
try { graph.someProperty shouldBe 3 } catch { use(graph) }
and wrap that in your own function
d

dimsuz

08/04/2021, 11:42 PM
Thank you! Will try this approach, I can invent some nice wrapper function indeed.