52 Testing with ScalaTest

 

After reading this lesson, you’ll be able to

  • Write and run tests using ScalaTest
  • Test synchronous and asynchronous code

You’ve been learning how to create a fully working Scala application that connects to a database, processes data asynchronously, and exposes an HTTP API. Your application must include tests to check that it behaves as expected, implements its business requirements correctly, and prevents the introduction of bugs with future code changes. This lesson completes the picture by introducing you to the basics of writing tests in Scala. You’ll write tests using a popular library called ScalaTest, and you’ll run them using sbt. After writing and running your first test, you’ll learn how to test code that runs asynchronously. In the capstone, you’ll write tests for your quiz application to check the correctness of its business logic.

52.1 Project setup

The first step in writing tests in Scala is to pick a testing library to use. In this lesson, you’ll use ScalaTest, which is among the most popular. Define an empty sbt project and specify it as one of your library dependencies in your build.sbt file by adding a configuration.

52.2 Your first test

52.3 Asynchronous testing

Summary

Answers to quick checks