#Test depend on test data
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
I think this is not possible
Test methods are intended to be independent
But i think u can do
A variable
junit itself has no mechanism for test dependencies
u can use before/after/beforeeach/aftereach
I can't guarantee the order so that might fail
and create a fixed test order
its sth like @FixMethodOrder(MethodSorters.NAME_ASCENDING)
or similar
its ofc a bit hacky overall
yeah. u can probably make it somehowdo what u need. but firas is right that it smells and that test cases should be independent
so for example u could isolate the setup code
from that one test
and then call that at the beginning of both tests
so just in a separate method you mean?
thing is, people are supposed to also be able to run tests individually
instead of only the entire suite
Yeah that's fair
Okay, I think I'll do that then
I can imagine a test needing some expensive operation you don't wanna do twice
yeah. in this case u can try to cache it
But maybe in that case you could have something lazy initialize a variable
in the class
Very good expl from zabu
gotta be careful with caching things in tests though. u can easily run into situations where u influence tests by what u have saved
in a way that it would perhaps pass if executed in a different order
oh also 0_0
I guess the problem is here
that my setup code needs a test as well
maybe that doesn't matter, they just all fail
but the other tests utilizing the setup code may fail for a weird reason
e.g. if the setup code doesn't throw
but the return value is invalid (and that would need to be tested)
but the other tests don't test that return value
are u sure ur actually doing unit tests and not for example an integration test
in which case u might just be "abusing junit"
U gotta know the diffs
in a nutshell, unit tests are supposed to be really small in terms of what they test. usually they only involve a single method and merely check its edge cases
like "what if i call this method with null or with a negative value or ..."
they wouldnt include complex setups and dependencies
integration tests are higher level and would represent for example an entire flow of an user through ur app
and if they do use dependencies, mockito time
such as "user wants to purchase a product and expects it to work"
which includes many steps and touches multiple classes in ur code
in which case there might be better test frameworks then, giving u the flexibility u need
although a lot of people just try their way with junit anways in practice, lol
Tests should also run in isolation, and ideally the order should not matter.
To go back to your initial question @half lion you could use JUnit pioneer => https://junit-pioneer.org/docs/disable-if-test-fails/ and then assume the expected output is the next tests input.
I didn't ask the question, but thanks for this, I did not know this exists