or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

async.mdfixtures.mdindex.mdmatchers.mdproperty.mdscalactic.mdtest-styles.md
tile.json

tessl/maven-org-scalatest--scalatest_2-13

ScalaTest is a comprehensive testing framework for Scala and Java programmers with multiple testing styles and powerful matcher support.

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

To install, run

npx @tessl/cli install tessl/maven-org-scalatest--scalatest_2-13@3.2.0

index.mddocs/

ScalaTest

ScalaTest is a comprehensive testing framework for Scala and Java programmers, providing multiple testing styles, powerful matcher capabilities, and extensive support for asynchronous testing. It includes Scalactic, a library for constraints, equality, and functional error handling.

Package Information

  • Package Name: org.scalatest:scalatest_2.13
  • Package Type: maven
  • Language: Scala
  • Installation: libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.19" % "test"

Core Imports

import org.scalatest._
import org.scalatest.matchers._
import org.scalactic._

For specific test styles:

import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.propspec.AnyPropSpec
import org.scalatest.featurespec.AnyFeatureSpec

Basic Usage

import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers

class BasicTestSuite extends AnyFunSuite with Matchers {
  test("basic math operations") {
    val result = 2 + 2
    result should equal (4)
    result should be > 3
    result should be < 5
  }
  
  test("string operations") {
    val message = "Hello, World!"
    message should include ("World")
    message should startWith ("Hello")
    message should have length 13
  }
}

Architecture

ScalaTest is built around several key components:

  • Test Styles: Multiple testing styles (FunSuite, FlatSpec, FunSpec, WordSpec, etc.) to match different preferences
  • Trait Stacking: Modular design allowing mixing of traits for different behaviors
  • Matcher System: Powerful matchers for assertions with readable error messages
  • Scalactic Integration: Built-in support for constraints, equality, and error handling
  • Async Support: First-class support for asynchronous testing with Futures
  • Property-Based Testing: Built-in generators and property checking capabilities

Capabilities

Test Styles

Different approaches to writing tests, each with their own syntax and organization patterns.

trait Suite {
  def run(testName: Option[String], args: Args): Status
  def testNames: Set[String]
  def tags: Map[String, Set[String]]
}

abstract class AnyFunSuite extends Suite with TestSuite with Assertions
abstract class AnyFlatSpec extends Suite with TestSuite with Assertions  
abstract class AnyFunSpec extends Suite with TestSuite with Assertions
abstract class AnyWordSpec extends Suite with TestSuite with Assertions
abstract class AnyFreeSpec extends Suite with TestSuite with Assertions
abstract class AnyPropSpec extends Suite with TestSuite with Assertions
abstract class AnyFeatureSpec extends Suite with TestSuite with Assertions

Test Styles

Matchers and Assertions

Powerful assertion and matcher system for readable test code and informative failure messages.

trait Assertions {
  def assert(condition: Boolean): Assertion
  def assert(condition: Boolean, clue: Any): Assertion
  def fail(): Nothing
  def fail(message: String): Nothing
  def pending: Assertion with PendingStatement
}

trait Matchers extends Assertions {
  def equal(right: Any): Matcher[Any]
  def be(right: Any): Matcher[Any]
  def contain(element: Any): Matcher[Any]
  def have(resultOfLengthWordApplication: ResultOfLengthWordApplication): Matcher[Any]
}

Matchers and Assertions

Scalactic Support

Functional programming utilities for equality, constraints, and error handling.

sealed abstract class Or[+G, +B] extends Product with Serializable
final case class Good[+G](get: G) extends Or[G, Nothing]
final case class Bad[+B](get: B) extends Or[Nothing, B]

def attempt[R](f: => R): R Or Throwable

trait Equality[A] {
  def areEqual(a: A, b: Any): Boolean
}

trait Equivalence[T] {
  def equiv(a: T, b: T): Boolean
}

Scalactic

Asynchronous Testing

Support for testing asynchronous code with Futures and custom execution contexts.

trait AsyncTestSuite extends Suite {
  type FixtureParam = Unit
  def withFixture(test: OneArgAsyncTest): FutureOutcome
}

class FutureOutcome(underlying: Future[Outcome]) {
  def map(f: Outcome => Outcome): FutureOutcome
  def flatMap(f: Outcome => FutureOutcome): FutureOutcome
  def transform(f: Try[Outcome] => Try[Outcome]): FutureOutcome
}

Asynchronous Testing

Property-Based Testing

Built-in support for property-based testing with generators and test data.

trait PropertyChecks {
  def forAll[A](gen: Generator[A])(fun: A => Assertion): Assertion
  def forAll[A](table: TableFor1[A])(fun: A => Assertion): Assertion
}

trait Generator[T] {
  def next(szp: SizeParam, edges: List[T], rnd: Randomizer): (RoseTree[T], Randomizer)
  def map[U](f: T => U): Generator[U]
  def flatMap[U](f: T => Generator[U]): Generator[U]
}

Property-Based Testing

Fixtures and Lifecycle

Support for test fixtures and suite lifecycle management.

trait BeforeAndAfterEach extends SuiteMixin {
  def beforeEach(): Unit
  def afterEach(): Unit
}

trait BeforeAndAfterAll extends SuiteMixin {
  def beforeAll(): Unit
  def afterAll(): Unit
}

trait FixtureTestSuite extends TestSuite {
  type FixtureParam
  def withFixture(test: OneArgTest): Outcome
}

Fixtures and Lifecycle

Test Execution and Events

Core test execution infrastructure and event reporting system.

trait Reporter {
  def apply(event: Event): Unit
}

sealed abstract class Event extends Serializable with Ordered[Event]
case class TestStarting(ordinal: Ordinal, suiteName: String, testName: String) extends Event
case class TestSucceeded(ordinal: Ordinal, suiteName: String, testName: String) extends Event
case class TestFailed(ordinal: Ordinal, suiteName: String, testName: String, throwable: Option[Throwable]) extends Event

trait Stopper {
  def apply(): Boolean
}

trait Distributor {
  def apply(suite: Suite, args: Args): Status
}

Suite Discovery and Tools

Test discovery and execution tools for integration with build systems.

object Suite {
  def getSimpleNameOfAnObjectsClass(o: AnyRef): String
  def checkForPublicNoArgConstructor(clazz: java.lang.Class[_]): Unit
}

trait Finders extends Annotation {
  def value(): String
}

Types

type Assertion = compatible.Assertion

sealed abstract class Outcome extends Product with Serializable
case object Succeeded extends Outcome
final case class Failed(exception: Throwable) extends Outcome
final case class Canceled(exception: Throwable) extends Outcome
case object Pending extends Outcome

trait Status {
  def succeeds(): Boolean
  def isCompleted(): Boolean
  def waitUntilCompleted(): Unit
}

case class Args(
  reporter: Reporter,
  stopper: Stopper = Stopper.default,
  filter: Filter = Filter(),
  configMap: ConfigMap = ConfigMap.empty,
  distributor: Option[Distributor] = None,
  tracker: Tracker = Tracker.default,
  chosenStyles: Set[String] = Set.empty,
  runTestInNewInstance: Boolean = false,
  distributedTestSorter: Option[DistributedTestSorter] = None,
  distributedSuiteSorter: Option[DistributedSuiteSorter] = None
)