#Test depend on test data

1 messages · Page 1 of 1 (latest)

half lion
#

What's the best way to make a test depend on the result of another?

One test parses a file and tests if no errors happened.
If no errors happened, I want to use the result in another test to see if it parsed correctly.
Otherwise both tests should fail I guess

Using JUnit btw

floral flickerBOT
#

<@&987246399047479336> please have a look, thanks.

hard condor
#

I think this is not possible

#

Test methods are intended to be independent

#

But i think u can do

#

A variable

lean dome
#

junit itself has no mechanism for test dependencies

#

u can use before/after/beforeeach/aftereach

half lion
lean dome
#

and create a fixed test order

half lion
#

Ah

#

Okay that sounds good

lean dome
#

its sth like @FixMethodOrder(MethodSorters.NAME_ASCENDING)

#

or similar

#

its ofc a bit hacky overall

half lion
#

And then use numbers

#

hmm

lean dome
#

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

half lion
lean dome
#

thing is, people are supposed to also be able to run tests individually

#

instead of only the entire suite

half lion
#

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

lean dome
#

yeah. in this case u can try to cache it

half lion
#

But maybe in that case you could have something lazy initialize a variable

lean dome
#

in the class

half lion
#

yeah

#

Okie

#

Reasonable

#

Thanks!

hard condor
#

Very good expl from zabu

lean dome
#

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

half lion
#

oh also 0_0

half lion
#

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

lean dome
#

are u sure ur actually doing unit tests and not for example an integration test

#

in which case u might just be "abusing junit"

half lion
#

I'm quite new to testing so idk

#

I'll look up intergration testing

hard condor
#

U gotta know the diffs

lean dome
#

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

merry shuttle
#

and if they do use dependencies, mockito time

lean dome
#

such as "user wants to purchase a product and expects it to work"

#

which includes many steps and touches multiple classes in ur code

half lion
#

Right

#

I'm eating lunch and looking up intergration testing xD

#

Thanks!

lean dome
#

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

merry shuttle
#

junit/assertj/mockito/equalsverifier

#

My usual toolbox

#

jsonassert is also common

hard condor
#

Testing with team be like

turbid bough
#

Tests should also run in isolation, and ideally the order should not matter.

merry shuttle
#

I didn't ask the question, but thanks for this, I did not know this exists

turbid bough
#

Sorry, I clicked the wrong T name.

#

The library has some nice extension options.