ScalaTest is a comprehensive testing framework for Scala and Java that provides multiple testing styles and sophisticated matcher libraries.
npx @tessl/cli install tessl/maven-org-scalatest--scalatest-2-11@3.2.0ScalaTest is a comprehensive testing framework for Scala that provides multiple testing styles, powerful assertion capabilities, and extensive matcher libraries. It offers eight different testing styles to accommodate different preferences and requirements, from function-based tests to full BDD specifications. ScalaTest also includes Scalactic, a library for functional programming idioms and better error messages.
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.19" % Test<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.11</artifactId>
<version>3.2.19</version>
<scope>test</scope>
</dependency>import org.scalatest._
import org.scalatest.matchers.should.Matchers
import org.scalactic._For specific test styles:
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.featurespec.AnyFeatureSpec
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.propspec.AnyPropSpec
import org.scalatest.refspec.RefSpecimport org.scalatest.funsuite.AnyFunSuite
class CalculatorSuite extends AnyFunSuite {
test("addition should work correctly") {
assert(1 + 1 === 2)
}
test("division should handle zero denominator") {
assertThrows[ArithmeticException] {
1 / 0
}
}
}import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class StackSpec extends AnyFlatSpec with Matchers {
"A Stack" should "pop values in last-in-first-out order" in {
val stack = Stack()
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 = Stack()
a [NoSuchElementException] should be thrownBy {
emptyStack.pop()
}
}
}import org.scalactic._
def divide(x: Int, y: Int): Int Or String = {
if (y == 0) Bad("Cannot divide by zero")
else Good(x / y)
}
val result = divide(10, 2)
result match {
case Good(value) => println(s"Result: $value")
case Bad(error) => println(s"Error: $error")
}ScalaTest follows a modular architecture with several key components:
Suite, Assertions) providing lifecycle and assertion capabilitiesOr type for error handlingval ScalaTestVersion: String // "3.2.19"
val ScalacticVersion: String // Scalactic versionScalaTest offers eight distinct testing styles to accommodate different preferences and use cases.
// Function-based testing
class MyFunSuite extends AnyFunSuite
test("test name") { /* test code */ }
// BDD flat specification
class MyFlatSpec extends AnyFlatSpec
"subject" should "behavior" in { /* test code */ }
// Word-based specification
class MyWordSpec extends AnyWordSpec
"subject" when { "condition" should { "behavior" in { /* test code */ } } }Comprehensive assertion capabilities with macro-enhanced error messages and natural language matcher DSL.
// Core assertions
def assert(condition: Boolean): Assertion
def assertResult(expected: Any)(actual: Any): Assertion
def assertThrows[T <: AnyRef](code: => Any): Assertion
// Should matchers DSL
value should equal(expected)
value should be > 5
collection should contain("element")Functional programming utilities including the Or type for railway-oriented programming and equality abstractions.
// Or type for error handling
sealed abstract class Or[+G, +B]
case class Good[G](value: G) extends Or[G, Nothing]
case class Bad[B](value: B) extends Or[Nothing, B]
// Safe execution
def attempt[R](f: => R): R Or ThrowableProgrammatic and command-line test execution with extensive configuration options and reporting formats.
// Test execution
def run(testName: Option[String], args: Args): Status
object Runner { def main(args: Array[String]): Unit }
// Configuration
type ConfigMap = Map[String, Any]
case class Args(reporter: Reporter, stopper: Stopper, filter: Filter, ...)Future-based asynchronous testing support with automatic timeout handling and assertion integration.
// Async test suites return Future[Assertion]
class MyAsyncFunSuite extends AsyncFunSuite
test("async test") {
Future { assert(true) }
}
// ScalaFutures trait for Future handling
trait ScalaFutures {
def whenReady[T](future: Future[T])(fun: T => Unit): Unit
}// Result of assertions
type Assertion = compatible.Assertion
val succeed: Assertion
// Test lifecycle
trait Suite {
def run(testName: Option[String], args: Args): Status
def testNames: Set[String]
def nestedSuites: IndexedSeq[Suite]
}
// Test metadata
case class TestData(
name: String,
configMap: ConfigMap,
scopes: Vector[String],
text: String,
tags: Set[String],
pos: Option[source.Position]
)// Test execution arguments
case class Args(
reporter: Reporter,
stopper: Stopper,
filter: Filter,
configMap: ConfigMap,
distributor: Option[Distributor],
tracker: Tracker,
chosenStyles: Set[String],
runTestInNewInstance: Boolean,
distributedTestSorter: Option[DistributedTestSorter],
summaryCounter: SummaryCounter
)
// Test filtering
class Filter(
tagsToInclude: Option[Set[String]] = None,
tagsToExclude: Set[String] = Set.empty,
excludeNestedSuites: Boolean = false,
dynaTags: DynaTags = DynaTags(Map.empty, Map.empty)
)
// Test execution status
sealed trait Status {
def isCompleted: Boolean
def succeeds(): Boolean
def unreportedException: Option[Throwable]
}// Error handling type
sealed abstract class Or[+G, +B] {
def isGood: Boolean
def isBad: Boolean
def get: G
def getOrElse[H >: G](alternative: => H): H
def map[H](f: G => H): H Or B
def flatMap[H, C >: B](f: G => H Or C): H Or C
}
// Equality abstractions
trait Equality[T] {
def areEqual(a: T, b: Any): Boolean
}
// Error message type
type ErrorMessage = String