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 Scala-js

Using Inside

Using OptionValues

Using EitherValues

Using PartialFunctionValues

Using PrivateMethodTester

Using WrapWith

Philosophy and design

Migrating to 3.0

Using ScalaTest with Eclipse

For Eclipse users, ScalaTest offers a powerful plugin that provides seamless support for testing in the Scala IDE for Eclipse. Not only does this plugin give Scala programmers the level of test-framework/IDE integration that Java programmers have enjoyed with JUnit, it goes quite a bit farther:

  1. You can right click on any test or collection of tests and run them.
  2. You can run just the tests you select in code, run tests you select in reported results, rerun all tests or just previously failed tests, or run tests in a selected class, file, or package.
  3. The results pane mirrors the structure of the specification (i.e., if your BDD-style specification text is nested in the source, it will appear nested in the results pane).
  4. You can hop from results to test, scope, class, or line of failed code.
  5. You can unfold the top of that stack that is automatically folded so only the offending line of code is shown.
  6. And, because ScalaTest is a platform that can support different styles of testing, the plugin can be extended to grant full IDE support for non-ScalaTest-native styles, such as a ScalaCheck Properties classes, Specs2 Specifications, or custom styles.

You can use the plugin with any release of ScalaTest, but you'll enjoy the most seamless IDE integration if you use ScalaTest 3.2.18. You can plug it into the latest stable release of the Scala IDE for Eclipse (version 3.0.x). For a nightly build, you'll need to build it by hand by following the steps given in https://github.com/scalatest/scalatest-eclipse-plugin

Screenshot of ScalaTest Eclipse Plugin with ScalaTest 3.2.18

Screenshot using ScalaTest 3.2.18

Installing the ScalaTest Eclipse Plugin

You can install the plugin together with Scala IDE using latest update sites listed at http://scala-ide.org/. For Scala IDE 3.0.x, tick 'ScalaTest for Scala IDE' as shown in the figure below:

Screenshot using ScalaTest 3.2.18

Information on the features of the integration (and some screenshots (and the source code)) is here:

https://github.com/scalatest/scalatest-eclipse-plugin

A video demo of the plugin that I gave back at ScalaDays is here:

http://skillsmatter.com/podcast/scala/scalatest-scalamock-subcut

Info on ScalaTest 3.2.18 is here:

http://www.scalatest.org/download

With the release notes for ScalaTest 3.2.18 here:

http://www.scalatest.org/release_notes/2.0

Using ScalaTest in a Scala project

To use ScalaTest in your Scala project, you must download ScalaTest and include it in build path of your project.

You can use any ScalaTest 1.x release or the latest 3.2.18 (recommended). Using ScalaTest 2.0 enables the following:

  • Test result view built in the eclipse workspace.
  • Running of selected specific test or scope.

When using ScalaTest 1.x, the GUI Runner provided by ScalaTest will be used instead of the built-in test result view.

Running a Selected Suite

To run a selected suite, you can select the suite using 2 different ways:-

  • By choosing the suite source in an opened Scala source file within the editor.
  • By choosing the suite class from Project Explorer/Navigator/Outline view.

After you choose the target suite element, just right click and choose:-

  Run As -> ScalaTest - Suite

A Run Configuration with the suite name will be created automatically.

Running a Selected Test

To run a selected test, click on the target test element in the editor, right click and choose:-

  Run As -> ScalaTest - Test

A Run Configuration with the test name will be created automatically.

Running a Selected Scope

To run a selected scope, click on the target scope element in the editor, right click and choose:-

  Run As -> ScalaTest - Test

A Run Configuration with the scope name will be created automatically.

Running All Suites in a Selected File

To run all ScalaTest suites in a selected file, you can select the file using 2 different ways:

  • By choosing an opened Scala source file containing ScalaTest suite(s) in the editor.
  • By choosing the Scala source file containing ScalaTest suite(s) from Project Explorer/Navigator.

After you choose the target Scala source file, just right click and choose:-

  Run As -> ScalaTest - File

All ScalaTest suites in the selected Scala source file will be run.

A Run Configuration with the file name will be created automatically.

Running All Suites in Selected Package

To run all ScalaTests suites in a package, you can right click on a package in Project Explorer and choose:-

  Run As -> ScalaTest Package

All ScalaTest suites in the selected package (not nested package) will be run. To include ScalaTest suites in nested packages, you'll need to select the 'Include Nested' option in the Run Configuration.

A Run Configuration with the package name will be created automatically.

Run Configuration Types

  • Suite - You specify Suite class name (mandatory) and test name(s) to run. If no test name is specified, all test(s) in the suite will be run.
  • File - You specify Suite file (mandatory) to run, all ScalaTest suites in the selected file will be run.
  • Package - You specify Package name (mandatory) and whether to include nested package, all ScalaTest suites in the selected package will be run. If 'Include Nested' is selected, all ScalaTest suites in nested package will be run as well.

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-2024 Artima, Inc. All Rights Reserved.

artima