bdd

A collection of ScalaTest BDD examples using FunSpec

Table of Contents1 - Getting started2 - First steps3 - Adding Given/When/Then behavior (and ‘And’)4 - More on Given, When, Then, and And5 - Add more tests within ‘describe’6 - Testing Option/Some/None in a BDD test7 - Nesting describe blocks8 - Using ‘before’ and ‘after’9 - Mark tests as pending10 - Temporarily disabling tests11 - Testing expected exceptions12 - Assertions13 - Using matchers14 - Tagging your BDD tests15 - Other16 - More information

This page is very much a work in progress, but it currently shows a small collection of ScalaTest BDD examples. I’ll keep adding more BDD examples as time goes on. Also, in this article I assume that you are familiar/comfortable with Scala and SBT, and are at least slightly familiar with using ScalaTest.

12 ScalaTest tutorials

As I’ve written before, when I finished writing the Scala Cookbook it ended up being about 140 pages longer than my editor wanted, and I had to cut some content from the book. Unfortunately the chapter on “Logging & Testing” was one of the victims of the cut, but I’m glad to say that I’ve finally taken the time to convert that material to HTML. As a result, here are links to the 12 ScalaTest tutorials in that chapter:

ScalaTest 111: How to use Mock objects with ScalaTest

Problem: You want to use a mock object framework in your ScalaTest tests, such as Mockito.

Solution

ScalaTest offers support for the following mock testing frameworks:

  • ScalaMock
  • EasyMock
  • JMock
  • Mockito

Because the support for each framework is similar, let’s take a look at using Mockito.

Before starting, imagine that you have a login web service for your application, and rather than call the real web service during your tests, you just want to mock one up.

ScalaTest 110: Temporarily disabling unit tests

Problem: When using ScalaTest, you want to temporarily disable one or more tests, presumably until you can get them working again.

Solution

When using BDD-style tests, change it method calls to ignore:

ScalaTest 109: Mark your tests with tags to include or exclude them

Problem: You want a way to label your individual ScalaTest unit tests, so you can easily choose to include or exclude them when running your test suite. For instance, you may want to tag long-running tests like database or web service tests, because they take a long time to run, and you don’t want to run them all the time.

Solution

Create one or more custom tags, then include those tags in your test specifications. When you run your tests, declare which tests you want to run, or not run.

ScalaTest 103: Writing a first BDD test with ScalaTest

Problem: You want to write your ScalaTest tests using a behavior-driven development (BDD) style.

Solution

Extend the ScalaTest FunSpec trait, typically with the BeforeAndAfter trait. Then use the approach shown in the following PizzaSpec test class.

A series of tests begins with the describe method, with individual tests declared in it methods: