Desktop version

Home arrow Computer Science arrow Building Applications with Scala

Isolation and self-contained tests

Unit tests should be self-contained, which means that a unit test's classes should not depend on any particular order of execution. Let's say you have a unit test class with two test functions. So, each test should test just one function at a time, and both functions should be able to run in any order whatsoever. Otherwise, the tests will be fragile and hard to maintain in the long run.

Effective naming

Effective naming is essential. The test function needs to say exactly what the test does. This is important because, when the test fails, it is easier to figure out what went wrong and why. Following the same idea, when you do assertions, you should assert just one thing at a time. Imagine that you need to test whether a web service returns a valid JSON. This particular JSON could have two fields: first name and last name. So, you will make one assert for the name and an other for the last name. This way, it will be easier to understand what the test does, and to troubleshoot when the test fails.

Levels of testing

When we run tests, we often do it in layers. Unit testing is the basic level; however, there are other levels such as controller tests, integration tests, UI tests, End-to-End tests, stress tests, and so many others. For this book, we will cover unit tests, controller tests, and UI tests using Junit and ScalaTest,Play's framework support.

< Prev   CONTENTS   Source   Next >

Related topics