Write Better Tests With the “Quick” Library in Go | by Metarsit Leenayongwut | Jun, 2022

Know what is the magic Quick library?

“My unit tests are passing!” — famous last word

The package quick implements utility functions to help with black box testing.

Quick library from testing package can be used for Property-based Testing. If you are familiar with QuickCheck from Haskell, they are very similar.

Let’s align what some of these terms mean…

Property-based Testing validates an expected behavior of a component or system against a range of autogenerated inputs and ensure it holds true.

Black box testing involves testing a component or system without any prior knowledge of its internal workings.

By putting all these pieces together, it means that if we know what are the properties of the functions, we can do a black box testing against them using this magic library!

Okay, now that we get what it is capable of. Let’s try it out with a simple example.


Let’s use the example above and come up with a few examples of its properties:

  1. if a > b, the result will be positive
  2. if b > a, the result will be negative
  3. if a == b, the result will be zero
  4. if b < 0, the result will be larger than a
  5. and the list goes on…

I think number 4 is the most interesting example! Let’s use that!

Traditionally, we will use Example-based Testing where we pre-define some inputs and their respective outputs and assert them and ensure they tally. For example:

We now have a simple example of Example-based Testing that tests Property Number 4.

However, is that really enough? Now, let’s try using Property-based Testing on the same property! 🔍

This seems reasonable. We only care about test cases where b is lesser than 0. Therefore, we ignore all test cases where b is positive.

Alright, our time has come! Let’s run this!

--- FAIL: TestSubtractPropertyFour (0.00s)
sub_test.go:19: #1: failed on input 9071749008449132817, -404711346371707334

Let me do this manually…

If a is 9071749008449132817 and b is -404711346371707334, that means the mathematically speaking, it is 9071749008449132817 — (-404711346371707334) which means 9071749008449132817 + 404711346371707334 should be the expression. This clearly means our Property number 4 is true.

Okay… so something must be wrong, time to find that problem!

There are plenty of ways to debug this but I went a little wild and use compiler to tell me what went wrong by declaring a constant of the subtraction of the above inputs.

This is the result from the Go compiler…

cannot use 9071749008449132817 - (-8970283718888711465) (untyped int constant 18042032727337844282) as int value in constant declaration (overflows)

🌊🌊🌊🌊🌊🌊 Ah… So it overflowed! 🌊🌊🌊🌊🌊🌊

There you go! We have found a test case that will blow up our innocent-looking subtraction function.

We have witnessed how incredible Property-based Testing is. Instead of using Example-based Testing, we can easily generate tens of thousands of randomized test cases easily with this library. Of course, it is not always a simple task to find the property for every function you are writing.

Even though you might not be able to apply this now, I am sure it will come in handy someday!

News Credit

%d bloggers like this: