8 Writing tests

 

This chapter covers:

  • Writing code and development process with testing in mind
  • Unit-testing, property-testing and other approaches to testing
  • Improving code quality

Every customer is happy to get a correct software. It is often claimed that Haskell type system provides software correctness. Unfortunately, there are some limitations to what the current type system is able to guarantee. That’s why we still need to test our code. Haskell supports many approaches to code correctness assurance ranging from unit-testing and property testing to lightweight formal verification. We don’t have to apply all of them, but it’s a good idea to apply at least some. Once we have tests, it’s much easier to refactor the code or make it run faster.

In this chapter, I present testing with an example of IP addresses processing built from scratch. We’ll continue working with this example later in this book, to explore code behavior and tune performance.

8.1  Setting a scene: IPv4 filtering application

We work on a small utility program, namely an IPv4 filtering application. It should check whether a given IPv4 address falls into some range from a large IP addresses range database (given as a text file). This utility may be a part of a large project on implementing IP firewall in Haskell.

[Caution] Example: IPv4 filtering application

🗋 ip/lookup/

iplookup

👉 It’s too easy to introduce errors into the code without tests

8.1.1  Development process overview

 
 
 

8.1.2  Initial implementation

 
 
 

8.2  Testing the IPv4 filtering application

 
 

8.2.1  Overview of approaches to testing

 
 
 

8.2.2  Testing Cabal-projects with tasty

 
 
 

8.2.3  Specifications writing and checking with Hspec

 
 

8.2.4  Property-based testing with Hedgehog

 
 

8.2.5  Golden tests with tasty-golden

 
 
 

8.3  Other approaches to testing

 
 
 
 

8.3.1  Testing functions a-la REPL with doctest

 
 
 
 

8.3.2  Lightweight verification with Liquid Haskell

 
 

8.3.3  Code quality with hlint

 
 

8.4  Summary

 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest