It is common to have three levels of tests:
Unit Tests – Checking if your objects do the right thing, if they work correctly. Usually tests methods/classes/small clusters of classes.
Integration Tests – Testing if your code works against code that you can’t change (3rd party or internal code that you can’t influence). Are your abstractions over that 3rd party code correct and used as intended?
End-to-End Tests – Checking if the whole system works and composes correctly.
When developing using TDD, I find it very useful to be able to choose which type of tests to run. Each kind of testing has its own trade-offs, and I sometimes find myself wanting to run a specific group of tests (for example: only quickly running unit tests and not end-to-end tests, which take far longer to complete).
To achieve this, here is how I like to structure my projects:
This is the typical Maven directory layout , with the additional testing source roots. I place my unit tests under the
test/ source directory and name them
*Test, my integration tests under
it/ and named
*IntegrationTest, and – unsurprisingly – end-to-end tests under
e2e/ and named
This separation clearly divides the types of tests and allows granularity in running them. Here is how to configure this in SBT using
.scala build configuration:
When defining your projects, add the new configs and settings:
Then to run only a specific test configuration (end-to-end for example):
We also added the
testAll task, so we can run all tests together:
If you need more examples you can check out my scala-e2e-testing sample project, which shows how to test your Scala apps end-to-end, and uses this project structure.