ScalaTest is a comprehensive testing framework for Scala and Java programmers with multiple testing styles and powerful matcher support.
npx @tessl/cli install tessl/maven-org-scalatest--scalatest_2-13@3.2.0ScalaTest 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.
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.19" % "test"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.AnyFeatureSpecimport 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
}
}ScalaTest is built around several key components:
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 AssertionsPowerful 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]
}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
}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
}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]
}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
}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
}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
}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
)