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 Selenium Using Scala-js Using Inside Using OptionValues Using EitherValues Using PartialFunctionValues Using PrivateMethodTester Using WrapWith Philosophy and design Migrating to 3.0 |
Writing your first test
1. In ScalaTest, you define tests inside classes that extend a style class such as import org.scalatest.FlatSpec
2. Each test in a "A Stack" should "pop values in last-in-first-out order"
If you have multiple tests about the same subject, you can use
it should "throw NoSuchElementException if an empty stack is popped"
After the sentence you put the word import collection.mutable.Stack import org.scalatest._
3. Place this in a file called $ scalac -cp scalatest-app_2.12.4-3.0.5.jar StackSpec.scala 4. You can run it from the command line by invoking ScalaTest's simple Runner: $ scala -cp scalatest-app_2.12.4-3.0.5.jar org.scalatest.run StackSpec Run starting. Expected test count is: 2 StackSpec: A Stack - should pop values in last-in-first-out order - should throw NoSuchElementException if an empty stack is popped Run completed in 96 milliseconds. Total number of tests run: 2 Suites: completed 1, aborted 0 Tests: succeeded 2, failed 0, ignored 0, pending 0 All tests passed. 5. Or you can run it from the Scala interpreter using the ScalaTest shell:
$ scala -cp scalatest_2.12.4-3.0.5.jar
scala> import org.scalatest._
import org.scalatest._
scala> run(new StackSpec)
StackSpec:
A Stack
- should pop values in last-in-first-out order
- should throw NoSuchElementException if an empty stack is popped
Next, learn about using assertions. |
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-2019 Artima, Inc. All Rights Reserved.