Skip to content

NicolasT/hedgehog-stateful-demo

Repository files navigation

hedgehog-stateful-demo

This repository contains some code demonstrating testing of a stateful, database-backed application using Hedgehog, a library similar in spirit to the well-known QuickCheck library. Hedgehog comes with built-in functionality to test state machines but the API may be a slightly daunting, hence this demo.

It's based on a blog-post by Jacob Stanley: "How to use Hedgehog to test a real world, large scale, stateful app". Make sure to read it first!

Importing the Original Code

The first commit imports all code from the original blog-post up to the Generate commands section.

The second commit adds the code from the Generate commands and Execute commands sections.

The third commit adds code from the Delete some users section, which causes the tests to fail, as expected and explained in the original article.

The fourth commit applies the changes from the Fix the model section, which causes the tests to succeed again.

The fifth commit adds test-case labelling, as explained in the Check commands get used section of the original article.

The sixth commit adds business rule validation, which causes the tests to fail again, as expected. See the Assert business rules section in the original article.

The seventh commit reverts the previous one, since we don't want failing tests. Hence, the code is back to that of the Check commands get used section of the original article.

Reworking using Hedgehog State-Machine Testing

The above tests are re-implemented using the Hedgehog State-Machine testing functionality in similar commits:

  • The Generate commands and Execute commands functionality originally implemented in here is here (compare). Note these changes require a slight modification of the abort function, which clutters the diff somewhat.

  • The Delete some user functionality originally implemented here is here (compare).

  • The fixes from the Fix the model section originally implemented here is here (compare).

  • The code changes to label test-cases from the Check commands get used section originally implemented here is here (compare).

The final diff resembles the original one quite well!

Making Cabal Happy

This commit makes the changes required for cabal test to succeed.