30

I have to write unit tests and integration tests for a project.

  • Should all tests be put into a single tests folder?
  • Or should unit tests and integration tests each be in a separate tests folder?
  • Or should I even put them into separate projects?

If I keep them together, are there any advantages or drawbacks with this approach?

amon
136k27 gold badges295 silver badges386 bronze badges
asked Jan 17, 2017 at 13:26
2

1 Answer 1

28

In general: yes, you should put integration tests and unit tests into different folders. Often, programmers don't draw a clear line between these two kinds of tests and just write whatever kind of test is useful. But integration tests tend to be slower, because they often involve:

  • Database queries
  • Network requests
  • Time-dependent behaviour
  • Large amounts of data

In contrast, an unit test would mock any expensive operations, so unit tests tend to run quickly (in fact, the slowest part of running the test is often the test framework itself).

When a programmer is working on the system they are in an edit–test cycle. The faster they get test feedback and the shorter the cycle is, the more productive they can be. So there we want to only run important test that complete quickly. The complete test suite would only be executed as part of a QA process, e.g. on a CI server.

This means that large test suites should be categorized. Can we only select unit tests for a particular component? Can we exclude slow tests? One simple way to do this is to maintain different test suites in different directories. If you only have very few tests, a single directory would also be OK as long as a programmer can easily select a subset of tests.

Whatever allows a programmer to get feedback quickly is good. The most comprehensive test suite doesn't matter if it isn't executed regularly.

Further reading:

answered Jan 17, 2017 at 18:43
2
  • When separating the folders, do you keep each of them in the same package as the production code (so you can test protected methods) ? Example, you make a maven module for unit tests and another maven module for the integration tests. Like that, you can separate them and still easily test protected methods. Commented May 7, 2019 at 15:26
  • @ihebiheb In Java, using package visibility is quite frowned upon. In some cases it can be helpful to use it and to access these APIs from unit tests. But in general, that's unnecessary. Integration-style tests never need access to package-private members. Whether or not to put the tests into a separate Maven project depends on how you want to run and deploy them. I'm not that into Java/Maven to be able to tell you what is sensible here. Commented May 8, 2019 at 14:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.