liveBook · Manning

Hspec

As explained in Functional Design and Architecture: Examples in Haskell

hspec is a Haskell testing library for expressing specifications and organizing test cases.

hspec is a general-purpose Haskell testing library for organizing specifications, test cases, and test suites. It distinguishes suites from cases, supports setup for each case, assertions, integration with QuickCheck, SmallCheck, and HUnit, parallel evaluation, and automatic test discovery. section 15.1.1

A practical hspec test keeps one responsibility and follows arrange, act, and assert:

  • Arrange the required data or environment. section 15.1.1
  • Act by performing the operation under test. section 15.1.1
  • Assert the result with an assertion function, while keeping tests independent so parallel execution does not introduce races. section 15.1.1

Specifications for a pure function

A specification groups cases under a topic with describe and expresses expectations using shouldBe. Tests can cover positive, negative, and zero inputs for square without sharing mutable state.

Testing a composed device

A specification can act as a final consumer of an opaque Device: construct it from vendor components, retrieve a named part through getDevicePart, and fail when the component is absent. Fake APIs make this boundary test runnable before production implementations exist.

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Demonstrates suite structure, assertions, AAA, pure-function cases, mutable-state hazards, and integration-boundary tests without detailing runner internals.
Examples
Uses square cases, IORef-based AAA tests, and a composed hardware-device check backed by fake vendor APIs.
What it assumes
Assumes basic Haskell and testing knowledge, while teaching suite structure, AAA organization, assertions, and test independence.
What you can do after
Apply it via hspec to write grouped specifications, assert behavior, isolate test setup, and check composed systems through public interfaces.
How it connects
Connects test suites, test cases, assertions, QuickCheck, SmallCheck, HUnit, parallel testing, TDD, and opaque interfaces.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.