ScalaTest User Guide

Getting started

Selecting testing styles

Defining base classes

Writing your first test

Using assertions

Tagging your tests

Running your tests

Sharing fixtures

Sharing tests

Using matchers

Testing with mock objects

Property-based testing

Asynchronous testing

Using Selenium

Using Scala-js

Other goodies

Philosophy and design

Migrating to 3.0

Defining base classes for your project

ScalaTest is a testing toolkit: it consists of focused, lightweight traits that you can mix together to solve the problem at hand. This approach minimizes the potential for naming and implicit conflicts and helps speed compiles.

Instead of duplicating code by mixing the same traits together repeatedly, we recommend you create abstract base classes for your project that mix together the features you use the most. For example, you might create a UnitSpec class (not trait, for speedier compiles) for unit tests that looks like:

package com.mycompany.myproject

import org.scalatest._

abstract class UnitSpec extends FlatSpec with Matchers with
  OptionValues with Inside with Inspectors

You can then write unit tests for your project using the custom base class, like this:

package com.mycompany.myproject

import org.scalatest._

class MySpec extends UnitSpec {
  // Your tests here
}

Most projects end up with multiple base classes, each focused on different kinds of tests. You might have a base class for integration tests that require a database (perhaps named DbSpec), another for integration tests that require an actor system (perhaps named ActorSysSpec), and another for integration test that require both a database and an actor system (perhaps named DbActorSysSpec), and so on. To get started, you can just create a base class for unit testing.

Note that in the rest of the user guide, we don't extend UnitSpec. Instead we show all traits involved to make it clear what is being used and make each example work independently.

With that, it is time to write your first test.

ScalaTest is brought to you by Bill Venners and Artima.
ScalaTest is free, open-source software released under the Apache 2.0 license.

If your company loves ScalaTest, please consider sponsoring the project.

Copyright © 2009-2017 Artima, Inc. All Rights Reserved.

artima