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 the ScalaTest shellTraitorg.scalatest.Shell provides a run method and configuration fields that implement
ScalaTest's DSL for the Scala interpreter.
The main command of the ScalaTest shell is
The default configuration is
All of these commands are fields of trait
Two other useful "commands"
to know about, though not technically part of the shell, are the Using the ScalaTest shell
The package object of the
scala> import org.scalatest._
import org.scalatest._
One thing importing
scala> class ArithmeticSuite extends AnyFunSuite with matchers.should.Matchers {
| test("addition works") {
| 1 + 1 should equal (2)
| }
| ignore("subtraction works") {
| 1 - 1 should equal (0)
| }
| test("multiplication works") {
| 1 * 1 should equal (2)
| }
| test("division works") (pending)
| }
defined class ArithmeticSuite
But importing scala> run(new ArithmeticSuite) ArithmeticSuite: - addition works - subtraction works !!! IGNORED !!! - multiplication works *** FAILED *** 1 did not equal 2 ( Configuring a single runTo configure a single run, you can prefix run by one or more configuration commands, separated by dots. For example, to enable durations during a single run, you would write: scala> durations.run(new ArithmeticSuite) ArithmeticSuite: - addition works (102 milliseconds) - subtraction works !!! IGNORED !!! - multiplication works *** FAILED *** (36 milliseconds) 1 did not equal 2 ( To enable statistics during a single run, you would write: scala> stats.run(new ArithmeticSuite) Run starting. Expected test count is: 3 ArithmeticSuite: - addition works - subtraction works !!! IGNORED !!! - multiplication works *** FAILED *** 1 did not equal 2 ( And to enable both durations and statistics during a single run, you could write: scala> durations.stats.run(new ArithmeticSuite) Run starting. Expected test count is: 3 ArithmeticSuite: - addition works (102 milliseconds) - subtraction works !!! IGNORED !!! - multiplication works *** FAILED (36 milliseconds)*** 1 did not equal 2 (
The order doesn't matter when you are chaining multiple configuration commands. You'll get the same
result whether you write
To disable color, use
scala> nocolor.run(new ArithmeticSuite)
ArithmeticSuite:
- addition works
- subtraction works !!! IGNORED !!!
- multiplication works *** FAILED ***
1 did not equal 2 (
To enable short stack traces during a single run, use scala> shortstacks.run(new ArithmeticSuite) ArithmeticSuite: - addition works (101 milliseconds) - subtraction works !!! IGNORED !!! - multiplication works *** FAILED *** (33 milliseconds) 1 did not equal 2 ( Changing the default configuration
If you want to change the default for multiple runs, you can import the members of your favorite
scala> import stats.durations._
import stats.durations._
Now anytime you run statistics and durations will, by default, be enabled:
scala> run(new ArithmeticSuite) Run starting. Expected test count is: 3 ArithmeticSuite: - addition works (9 milliseconds) - subtraction works !!! IGNORED !!! - multiplication works *** FAILED *** (10 milliseconds) 1 did not equal 2 ( Running multiple suites
If you want to run multiple suites, you can use the factory methods in either the
scala> run(Suites(new ArithmeticSuite, new ArithmeticSuite, Suites(new ArithmeticSuite, new ArithmeticSuite))) Run starting. Expected test count is: 12 Suites: ArithmeticSuite: - addition works (0 milliseconds) - subtraction works !!! IGNORED !!! - multiplication works *** FAILED *** (1 millisecond) 1 did not equal 2 ( Running a single test
The
scala> run(new ArithmeticSuite, "addition works") ArithmeticSuite: - addition works |
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.