0Pricing
Scala for Backend Engineering & Functional Programming · Leçon

Styles de ScalaTest

FlatSpec et FunSuite

Styles de ScalaTest est une leçon Scala for Backend Engineering & Functional Programming gratuite sur CoddyKit. Ceci est la leçon 1 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Scala for Backend Engineering & Functional Programming, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Scala for Backend Engineering & Functional Programming comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

What is ScalaTest?

ScalaTest is the most widely used testing framework for Scala. A distinctive feature is that it offers multiple testing styles, letting teams choose the syntax that fits their background.

Adding the Dependency

You add ScalaTest as a test dependency in build.sbt. The % Test scope keeps it out of your production artifact.

libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.18" % Test

Why Multiple Styles?

Different teams prefer different conventions: xUnit veterans like FunSuite; behavior-driven teams like FlatSpec or WordSpec. ScalaTest supports them all on one engine, so you pick a style per project for consistency.

AnyFunSuite

AnyFunSuite uses a test("name") { ... } form, familiar to JUnit users. Each test block is one test case.

import org.scalatest.funsuite.AnyFunSuite

class MathSuite extends AnyFunSuite {
  test("addition works") {
    assert(1 + 1 == 2)
  }

  test("multiplication works") {
    assert(2 * 3 == 6)
  }
}

AnyFlatSpec

AnyFlatSpec reads like a specification: "A Stack" should "pop values" in { ... }. The subject and behavior form a readable sentence.

import org.scalatest.flatspec.AnyFlatSpec

class CalculatorSpec extends AnyFlatSpec {
  "A Calculator" should "add two numbers" in {
    assert(2 + 3 == 5)
  }

  it should "subtract two numbers" in {
    assert(5 - 2 == 3)
  }
}

should vs must

In FlatSpec you can use should, must, or can as the verb. They are interchangeable in meaning; teams pick one for consistency.

import org.scalatest.flatspec.AnyFlatSpec

class ParserSpec extends AnyFlatSpec {
  "A Parser" must "handle empty input" in {
    assert("".isEmpty)
  }
}

it and they

After naming a subject once, follow up with it (or they) to add more behaviors for the same subject without repeating its name.

import org.scalatest.flatspec.AnyFlatSpec

class ListSpec extends AnyFlatSpec {
  "A List" should "report its size" in {
    assert(List(1, 2, 3).size == 3)
  }
  it should "reverse correctly" in {
    assert(List(1, 2).reverse == List(2, 1))
  }
}

AnyFunSpec

AnyFunSpec groups tests with nested describe blocks and it clauses, giving hierarchical, readable output.

import org.scalatest.funspec.AnyFunSpec

class SetSpec extends AnyFunSpec {
  describe("A Set") {
    describe("when empty") {
      it("has size 0") {
        assert(Set.empty[Int].size == 0)
      }
    }
  }
}

Pending and Ignored Tests

Mark unfinished tests as pending, or skip one by using ignore instead of test/in. Both keep the suite green while signaling work to do.

import org.scalatest.funsuite.AnyFunSuite

class WipSuite extends AnyFunSuite {
  test("feature not done") {
    pending
  }
  ignore("flaky test") {
    assert(false)
  }
}

Choosing a Style

Guidance:

  • FunSuite — minimal, xUnit-like, great for unit tests.
  • FlatSpec — readable behavior sentences.
  • FunSpec/WordSpec — nested, spec-style grouping.

Pick one per codebase and stay consistent.

Running the Tests

Run all tests with sbt test, or a single suite with sbt "testOnly *CalculatorSpec". ScalaTest reports each test name and outcome.

sbt test
sbt "testOnly *CalculatorSpec"

Quick Check

Test your knowledge of ScalaTest styles.

Recap

You learned ScalaTest styles:

  • Add ScalaTest with % Test scope.
  • AnyFunSuite: test("...") { } blocks.
  • AnyFlatSpec: subject should behavior in { }.
  • AnyFunSpec: nested describe/it.
  • Pick one style per project; run with sbt test.

Questions Fréquemment Posées

La leçon « Styles de ScalaTest » est-elle gratuite ?

Oui — le texte complet de « Styles de ScalaTest » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Scala for Backend Engineering & Functional Programming, passe à CoddyKit PRO. Le cours Scala for Backend Engineering & Functional Programming comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Styles de ScalaTest » ?

FlatSpec et FunSuite Tu pratiques Scala for Backend Engineering & Functional Programming avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Scala for Backend Engineering & Functional Programming ?

Aucune expérience préalable n'est requise. Scala for Backend Engineering & Functional Programming sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 1 sur 4.

Combien de temps prend la leçon « Styles de ScalaTest » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Scala for Backend Engineering & Functional Programming ?

Oui. Chaque leçon Scala for Backend Engineering & Functional Programming inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Styles de ScalaTest
  2. Matchers
  3. Tests fondés sur les propriétés
  4. Simulations et appareils de test
← Retour à Scala for Backend Engineering & Functional Programming