5 dicas valiosas para quem comete erros no trabalho

Diz a sabedoria popular que errar é humano, mas cometer erros no trabalho pode atrapalhar sensivelmente seus planos de maiores vôos na carreira. Equívocos são oportunidades de alerta e é bom…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Find the best properties for Property Based Testing

Tips to build properties — Examples written using fast-check

Property based testing became famous thanks to QuickCheck in Haskell. It is another way to test your code which is fully complementary to classical unit-test methods.

It tries to discover inputs causing a property to be falsy by testing it against multiple — in general a hundred is enough — eligible inputs. A property can be seen as:

You may refer to https://medium.com/@nicolasdubien/introduction-to-property-based-testing-f5236229d237 for more details about property based testing.

The main difficulty when moving to property based testing is: finding the right properties, for a given algorithm. Most of the time we tend to test the code against… itself which is not the right way to do.

Basically fast-check itself uses properties to test itself, so you might be able to run properties on your own code too.

When? Some characteristics of the output are independent of its inputs. If those characteristics are strong enough they can be defined as properties. Being fully agnostic of the inputs is most of the time too restrictive to have a useful property but can constitute a first try.

Here are some examples of such properties:

When? The output of your algorithm is easy to check: it has a relationship with the input. The relationship might just be a set of traits you expect to observe in the output based on the input.

Property #1: The average of a and b must be between a and b

Property #2: The decomposition in prime numbers of n must be such that the product of all numbers in the decomposition equals n

Other examples:

When? Some inputs have a very simple output. In some cases, you might be able to extract some very specific inputs for which you can easily compute the output.

Property #3: removing duplicates of an array of unique values returns the array itself

Property #4: the concatenation of a, b and c always contains string b

Other examples:

When? Two or more functions can be combined to compute something easy to check.

Property #5: zipping then unzipping a file should result in the original file

Property #6: lcm(a,b) times gcd(a,b) must be equal to a times b

When? A simpler implementation of the algorithm exists.

Property #7: c is contained inside sorted array data for binary search is equivalent to c is contained inside data for linear search

Please leave a clap or a comment if you liked this article ;)

Add a comment

Related posts:

Love is strength.

Find and do things that move you. It’s easier to dedicate your time to things you love. It’s easier to find enjoyment and accomplishment in those things. Expand your horizon. Expand your hobbies…

How to Preserve Your Best Self During Low Moments

We get knocked off balance. We hit the wall. We have setbacks. We get sick. We run out of gas. Our identities take a hit. We fear that we’ve lost the best part of ourselves. Where did our strength…

Nearly Half of Republican Voters Believe They Need to Take the Law into Their Own Hands

A YouGov poll revealed some very disturbing details about our fellow citizens