Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering F#

You're reading from   Mastering F# A comprehensive and in-depth guide to writing functional programs using F#

Arrow left icon
Product type Paperback
Published in Nov 2016
Publisher
ISBN-13 9781784393434
Length 264 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Suhaib Fahad Suhaib Fahad
Author Profile Icon Suhaib Fahad
Suhaib Fahad
Alfonso García-Caro Núñez Alfonso García-Caro Núñez
Author Profile Icon Alfonso García-Caro Núñez
Alfonso García-Caro Núñez
Arrow right icon
View More author details
Toc

Property-based testing

Unit tests are a tremendously useful tool to verify the robustness of our program. However, as we have seen in the earlier examples, we are always checking against single cases, and, even when automatically generating parameters for our tests, it is always difficult to be sure that there are no edge cases we may be missing.

The solution is not to test against the results of a function but against properties. This way, we can feed any value to the function, and verify that the properties are always fulfilled. We can also think of the properties as the requirements of the application. For example, we may want to verify that our add function, given earlier, fulfills the following three properties of addition:

  • Commutative property: add x y should be the same as add y x
  • Associative property: add x y |> add z should be the same as add y z |> add x
  • Identity property: add x 0 should be the same as x

Thanks to F#'s expressiveness, it is really easy to write these conditions...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime