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

ScalaTest 3.2.18 includes support for Scala-js. Following the steps in the Scala-js Basic Tutorial, you'll add the following to sbt build file to use ScalaTest:

libraryDependencies += "org.scalatest" %%% "scalatest" % "3.0.0" % "test"

The following is an example test suite written using ScalaTest:

package tutorial.webapp

import org.scalatest._
import org.scalajs.jquery.jQuery

class TutorialTestWithScalaTest extends AnyFunSpec {

  // Initialize App

  describe("TutorialApp") {
    it("should contain 'Hello World' text in its body") {
      assert(jQuery("p:contains('Hello World')").length == 1)

    it("should append 'You clicked the button!' text when the user clicks on the 'Click me!' button") {
      def messageCount =
        jQuery("p:contains('You clicked the button!')").length

      val button = jQuery("button:contains('Click me!')")
      assert(button.length == 1)
      assert(messageCount == 0)

      for (c <- 1 to 5) {
        assert(messageCount == c)


The first test uses jQuery to verify that the page contains exactly one <p> element containing the text “Hello World” after the UI has been set up. The second test checks the behavior that when 'Click me!' button is clicked, it will append a new <p> element containing the text “Hello World” in the body of the page.

You can run the test by issuing test command in the sbt:

> test
[info] TutorialTestWithScalaTest:
[info] TutorialApp
[info] - should contain 'Hello World' text in its body
[info] - should append 'You clicked the button!' text when the user clicks on the 'Click me!' button

Now we can take a look at some goodies ScalaTest has that can help you address specific problems, starting with using Inside.

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.