scalatest

How to use ScalaCheck in the SBT REPL (command line)

If you add ScalaCheck to an SBT project like this:

libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.13.4" % "test"

it’s only available in the SBT “test” scope. This means that when you start a Scala REPL session inside of SBT with its console command, the ScalaCheck library won’t be available in that scope.

To use ScalaCheck with the SBT console (REPL), don’t use its console command — use test:console instead. A complete example looks like this:

$ sbt

> test:console

scala> import org.scalacheck.Gen.choose

Note that after you type test:console your project may be compiled, so that step may take a few moments.

In summary, use SBT’s console command to start a “normal” Scala REPL inside SBT, and use test:console to start a REPL that you can run tests inside of. (Note that this same advice also applies to using ScalaTest or specs2.)

A collection of ScalaTest BDD examples using FunSpec

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.

How to run Scala project tests with SBT and ScalaTest

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.3, “How to run tests with SBT and ScalaTest.”

Problem

You want to set up an SBT project with ScalaTest, and run the tests with SBT.

Solution

Create a new SBT project directory structure as shown in Recipe 18.1, and then add the ScalaTest library dependency to your build.sbt file, as shown here:

How to use a String like it’s a File in Scala (such as in testing)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.7, “How to use a String like it's a file in Scala.”

Problem

Typically for the purposes of testing, you want to pretend that a String is a file.

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 112 - How to run ScalaTest unit tests in Eclipse

Problem: How do I use ScalaTest in Eclipse (or, How do I run my ScalaTest unit tests from Eclipse?)

Solution

I do a lot of work from the command line with Ant builds and similar things, but there are times I like to do things through Eclipse. Today I wanted to run my ScalaTest unit tests in Eclipse, and found this to be a straightforward task.

Besides Scala, Eclipse, and an Eclipse project, you'll need:

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 108 - How to test expected exceptions with ‘intercept’

Problem: Using ScalaTest, you want to test a portion of your code that should throw an exception under certain conditions.

Solution

Use the intercept method to verify the exception occurs. In the following example, the boom method will always throw an exception. The intercept method lets you catch that exception, so you can verify that this portion of the method works as desired: