ScalaTest Styles
FlatSpec and FunSuite.
ScalaTest Styles is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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" % TestWhy 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
% Testscope. AnyFunSuite:test("...") { }blocks.AnyFlatSpec: subjectshouldbehaviorin { }.AnyFunSpec: nesteddescribe/it.- Pick one style per project; run with
sbt test.
Frequently asked questions
Is the “ScalaTest Styles” lesson free?
Yes — the full text of “ScalaTest Styles” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.
What will I learn in “ScalaTest Styles”?
FlatSpec and FunSuite. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Scala for Backend Engineering & Functional Programming?
No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “ScalaTest Styles” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?
Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- ScalaTest Styles
- Matchers
- Property-Based Testing
- Mocking and Fixtures