To get started with ScalaTest, copy this AnyFlatSpec
into a file
named ExampleSpec.scala
:
import collection.mutable.Stack import org.scalatest._ import flatspec._ import matchers._ class ExampleSpec extends AnyFlatSpec with should.Matchers { "A Stack" should "pop values in last-in-first-out order" in { val stack = new Stack[Int] stack.push(1) stack.push(2) stack.pop() should be (2) stack.pop() should be (1) } it should "throw NoSuchElementException if an empty stack is popped" in { val emptyStack = new Stack[Int] a [NoSuchElementException] should be thrownBy { emptyStack.pop() } } }
You can compile this FlatSpec
(using this Jar file) like this:
$ scalac -cp scalatest-app_3-3.2.19.jar ExampleSpec.scala
To run it, you will need one more artifact,
the Jar file for Scala's XML module.
Once you've downloaded that Jar file, you can run ExampleSpec
like this:
$ CLASSPATH=scalatest-app_3-3.2.19.jar:scala-xml_3-2.3.0.jar $ scala -cp $CLASSPATH org.scalatest.run ExampleSpec Run starting. Expected test count is: 2 ExampleSpec: A Stack - should pop values in last-in-first-out order - should throw NoSuchElementException if an empty stack is popped Run completed in 76 milliseconds. Total number of tests run: 2 Suites: completed 1, aborted 0 Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0 All tests passed.
Your tests passed! As a reward, take a moment to install ScalaTest in your project.
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-2025 Artima, Inc. All Rights Reserved.