Reputation: 4813
This is a best practice question.
I'm maintaining a web application which is a proxy around another web application whose source code I do not have access to. My project's function is to make the other server's API easy to use and mobile friendly.
I'm in the process of adding unit tests and I find myself wanting to break standard best practices like Tests should be independent
.
To test login
, I first need to register
. If register
fails, login
would fail (or be pointless.) What is the best way to handle this? Using a setUp
function to register and then fail login
because the expected user is missing? Or enforce strict test ordering and make sure register
is called first?
I tend to lean towards using a setUp
function, but I'd be curious to know what the community thinks.
The test framework is JUnit, although I imagine this question would apply to other frameworks.
Upvotes: 1
Views: 96
Reputation: 19661
You're describing integration tests, not unit tests. Unit tests simulate dependencies and exercise a single class. It's not necessary to register first because you mock the results of a successful registration (if necessary, depending on your class design).
For the integration tests, yes, you would have a complicated setup. If that's too slow to run each time, you may find it easier to create a single test that performs a number of tests each dependent on the previous tests.
Upvotes: 1