or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

assertions-matchers.mdasync-testing.mdindex.mdscalactic-utilities.mdtest-execution.mdtest-styles.md
tile.json

tessl/maven-org-scalatest--scalatest-2-11

ScalaTest is a comprehensive testing framework for Scala and Java that provides multiple testing styles and sophisticated matcher libraries.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.scalatest/scalatest_2.11@3.2.x

To install, run

npx @tessl/cli install tessl/maven-org-scalatest--scalatest-2-11@3.2.0

index.mddocs/

ScalaTest

ScalaTest 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.

Package Information

  • Package Name: org.scalatest:scalatest_2.11
  • Package Type: Maven (SBT)
  • Language: Scala (cross-compiled for 2.10, 2.11, 2.12, 2.13)
  • Version: 3.2.19
  • Platforms: JVM, JavaScript (Scala.js), Native (Scala Native)
  • Installation (SBT): libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.19" % Test
  • Installation (Maven):
    <dependency>
      <groupId>org.scalatest</groupId>
      <artifactId>scalatest_2.11</artifactId>
      <version>3.2.19</version>
      <scope>test</scope>
    </dependency>

Core Imports

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.RefSpec

Basic Usage

Simple Function-Based Test (FunSuite)

import 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
    }
  }
}

BDD-Style Specification (FlatSpec)

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()
    }
  }
}

Scalactic Or Type for Error Handling

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")
}

Architecture

ScalaTest follows a modular architecture with several key components:

  • Core Framework: Base traits (Suite, Assertions) providing lifecycle and assertion capabilities
  • Test Styles: Eight different styles (FunSuite, FlatSpec, WordSpec, etc.) offering varied syntax and organization
  • Matcher System: Rich DSL for expressing expectations with natural language constructs
  • Scalactic Utilities: Functional programming helpers including the Or type for error handling
  • Runner System: Command-line and programmatic test execution with flexible reporting
  • Async Support: Future-based testing with built-in timeout and error handling

Version Information

val ScalaTestVersion: String  // "3.2.19"
val ScalacticVersion: String  // Scalactic version

Capabilities

Test Styles

ScalaTest 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 */ } } }

Test Styles

Assertions and Matchers

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")

Assertions and Matchers

Scalactic Utilities

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 Throwable

Scalactic Utilities

Test Execution and Configuration

Programmatic 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, ...)

Test Execution

Async Testing

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
}

Async Testing

Types

Core Types

// 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]
)

Configuration Types

// 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]
}

Scalactic Types

// 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