or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

core-specifications.mddsl-components.mdindex.mdintegration-features.mdmatcher-system.mdmutable-specifications.mdreporting.mdtest-execution.md
tile.json

core-specifications.mddocs/

Core Specifications

Core specification classes and traits provide the foundation for creating immutable (functional-style) test specifications in specs2. These are the main entry points for acceptance-style testing where the specification structure is defined declaratively.

Specification Classes

Specification

Main specification class for immutable specifications.

abstract class Specification extends SpecificationLike

Usage Example:

class MySpec extends Specification { def is = s2"""
  This is my specification
    example 1 $e1
    example 2 $e2
  """
  
  def e1 = 1 + 1 must beEqualTo(2)
  def e2 = "test" must have size(4)
}

SpecificationLike

Core trait that provides all specification functionality.

trait SpecificationLike extends ImmutableSpecificationStructure 
  with SpecificationCreation 
  with SpecificationFeatures

This trait combines:

  • ImmutableSpecificationStructure: Basic structure definition
  • SpecificationCreation: Methods for creating specification fragments
  • SpecificationFeatures: Advanced features like tagging, grouping, execution control

Spec

Lightweight specification class with minimal imports.

abstract class Spec extends SpecLike

Usage Example:

class SimpleSpec extends Spec { def is = s2"""
  Simple specification
    basic test $e1
  """
  
  def e1 = true must beTrue
}

SpecLike

Lightweight trait with essential functionality only.

trait SpecLike extends ImmutableSpecificationStructure 
  with S2StringContext1 
  with AcceptanceDsl1 
  with MustMatchers1
  with ArgumentsCreation 
  with ArgumentsShortcuts 
  with FormattingFragments 
  with StandardResults 
  with StandardMatchResults

Includes minimal imports:

  • S2StringContext1: Basic string interpolation for s2"" syntax
  • AcceptanceDsl1: Essential DSL methods for examples and structure
  • MustMatchers1: Core matchers only
  • ArgumentsCreation: Methods for creating Arguments configurations
  • ArgumentsShortcuts: Shortcut methods for common argument patterns
  • FormattingFragments: Support for formatting and layout fragments
  • StandardResults: Standard result types (Success, Failure, Error, etc.)
  • StandardMatchResults: Standard matcher result handling

Structure Traits

SpecificationStructure

Base trait defining the structure of all specifications.

trait SpecificationStructure {
  def is: SpecificationStructure
  def content: Fragments
  def header: String
  def arguments: Arguments
}

ImmutableSpecificationStructure

Structure trait for functional-style specifications.

trait ImmutableSpecificationStructure extends SpecificationStructure {
  def map(f: Fragment => Fragment): ImmutableSpecificationStructure
  def flatMap(f: Fragment => Fragments): ImmutableSpecificationStructure
}

Fragment System

Specifications are built from fragments - the basic building blocks.

sealed trait Fragment

case class Text(text: String) extends Fragment
case class Example(description: String, body: Execution) extends Fragment
case class Step(action: () => Any) extends Fragment  
case class Action(action: () => Any) extends Fragment
case class Tab(n: Int = 1) extends Fragment
case class Backtab(n: Int = 1) extends Fragment
case class Br() extends Fragment
case class End() extends Fragment

Fragments

Collection and manipulation of fragments.

case class Fragments(fragments: Vector[Fragment]) {
  def append(other: Fragments): Fragments
  def prepend(other: Fragments): Fragments
  def insert(index: Int, fragment: Fragment): Fragments
  def map(f: Fragment => Fragment): Fragments
  def filter(p: Fragment => Boolean): Fragments
}

Creation Traits

SpecificationCreation

Methods for creating specification content.

trait SpecificationCreation {
  def title(t: String): Fragment
  def text(t: String): Fragment
  def example(desc: String)(body: => Any): Fragment
  def step(action: => Any): Fragment
  def action(action: => Any): Fragment
}

DefaultFragmentFactory

Factory methods for creating fragments.

object DefaultFragmentFactory {
  def text(t: String): Fragment
  def example(desc: String, body: Execution): Fragment
  def step(action: => Any): Fragment
  def action(action: => Any): Fragment
  def break: Fragment
  def tab(n: Int = 1): Fragment
  def backtab(n: Int = 1): Fragment
  def end: Fragment
}

Advanced Features

SpecificationFeatures

Advanced specification capabilities.

trait SpecificationFeatures extends Groups 
  with Debug 
  with Level 
  with SpecificationInclusion

ArgumentsCreation

Creates Arguments configurations for controlling specification execution.

trait ArgumentsCreation {
  def args(
    plan: Boolean = false,
    skipAll: Boolean = false, 
    stopOnFail: Boolean = false,
    sequential: Boolean = false,
    isolated: Boolean = false,
    threadsNb: Int = Runtime.getRuntime.availableProcessors,
    specs2ThreadsNb: Int = Runtime.getRuntime.availableProcessors
  ): Arguments
}

ArgumentsShortcuts

Convenient shortcut methods for common argument patterns.

trait ArgumentsShortcuts {
  def sequential: Arguments
  def isolated: Arguments  
  def skipAll: Arguments
  def stopOnFail: Arguments
  def plan: Arguments
}

FormattingFragments

Support for formatting and layout fragments in specifications.

trait FormattingFragments {
  def tab(n: Int = 1): Fragment
  def backtab(n: Int = 1): Fragment  
  def br: Fragment
  def p: Fragment
  def end: Fragment
}

StandardResults

Standard result types used throughout specs2.

trait StandardResults {
  def success: Success
  def success(message: String): Success
  def failure: Failure  
  def failure(message: String): Failure
  def pending: Pending
  def pending(message: String): Pending
  def skipped: Skipped
  def skipped(message: String): Skipped
}

StandardMatchResults

Standard matcher result handling and utilities.

trait StandardMatchResults {
  def createMatchResult[T](
    expectable: Expectable[T], 
    message: String, 
    negatedMessage: String, 
    isSuccess: Boolean
  ): MatchResult[T]
}

Groups

Grouping examples for organization and selective execution.

trait Groups {
  def group(name: String): Fragment
  def section(name: String): Fragment
}

Context Support

Execution context for examples.

trait Context {
  def apply[T: AsResult](a: => T): Result
}

trait BeforeEach extends Context {
  def before: Any
}

trait AfterEach extends Context {
  def after: Any  
}

trait AroundEach extends Context {
  def around[T: AsResult](t: => T): Result
}

Usage Patterns

String Context Specifications

Using s2 string interpolation for embedded examples:

class UserSpec extends Specification { def is = s2"""
  User management system
    should create valid users     $createUser
    should validate email format  $validateEmail  
    should handle duplicates      $handleDups
  """
  
  def createUser = {
    val user = User("john", "john@example.com")
    user.name must beEqualTo("john")
  }
  
  def validateEmail = {
    User("jane", "invalid-email") must throwA[ValidationException]
  }
  
  def handleDups = {
    val users = List(User("bob", "bob@test.com"), User("bob", "bob@test.com"))
    users.distinct must have size(1)
  }
}

Sequential vs Parallel Execution

Control execution mode:

class SequentialSpec extends Specification { def is = sequential ^ s2"""
  These examples run one after another
    step 1 $step1
    step 2 $step2  
    step 3 $step3
  """
  
  // examples implementation...
}

Specification Arguments

Configure specification behavior:

class ConfiguredSpec extends Specification { def is = 
  args(sequential = true, stopOnFail = true) ^ s2"""
  Configured specification behavior
    example 1 $e1
    example 2 $e2
  """
  
  // examples implementation...
}

Including Other Specifications

Modular specification composition:

class MainSpec extends Specification { def is = s2"""
  Main specification
    ${"UserSpec"}
    ${"OrderSpec"}  
    ${"PaymentSpec"}
  """
}

Best Practices

  1. Use descriptive names: Make specification titles and example descriptions clear and readable
  2. Organize logically: Group related examples together using sections
  3. Keep examples focused: Each example should test one specific behavior
  4. Use appropriate contexts: Apply setup/teardown contexts when needed
  5. Leverage string interpolation: Use s2 strings for readable specification structure
  6. Control execution: Use sequential execution when examples have dependencies