• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Codemotion Magazine

We code the future. Together

  • Discover
    • Events
    • Community
    • Partners
    • Become a partner
    • Hackathons
  • Magazine
    • Backend
    • Frontend
    • AI/ML
    • DevOps
    • Dev Life
    • Soft Skills
    • Infographics
  • Talent
    • Discover Talent
    • Jobs
    • Manifesto
  • Companies
  • For Business
    • EN
    • IT
    • ES
  • Sign in
ads

Luca FerrettiDecember 11, 2019

How to Challenge Your Code with Property-Based Testing – Part 1

QA/Testing
How to Challenge Your Code with Property-Based Testing - Part 1
facebooktwitterlinkedinreddit

What does Property-Based Testing mean for developers? Well, a debate persists in software as to the exact nature, division and scope of “testing” activities. We need to check that a particular software component produces the expected output with the expected inputs provided. Yet we also need to expose the behaviour of this component against unforeseen or unpredictable inputs.

The first activity is usually referred to as “checking”, while the second is called “exploring” and both contribute to defining the software components as “tested”.

The boundary that divides these two different activities is often linked to the level of knowledge of the component being examined. However, we must be very careful not to confuse the knowledge of the issue under analysis with the illusion of knowledge.

the illusion of knowledge in property based testing
The illusion of knowledge

Introducing the Black Swan Theory

A common example of the negative effects of the illusion of knowledge is the so-called “black swan theory” introduced by Nassim Nicholas Taleb in 2007. In short, if it is assumed that all swans are white and that, therefore, black swans cannot exist, it means the existence of a single black swan can bring down the whole theory.

In the field of testing, the illusion of knowledge is linked to the cognitive bias that, want it or not, we apply both when we write code and when we design and structure the various tests and test cases necessary to confirm the functionality of the code itself.

Some types of automatic tests, such as unit tests, only allow confirmation of the behaviour. An example is the confirmation of the occurrence of a certain condition. What could happen unexpectedly in code like the following?

<script src=”https://gist.github.com/elleuca/267b5db40dd72c03234c5eb76b9835c5.js”></script>

It is preferable to add the various limit cases of interest. However,  the illusion of knowing the range within the price we will pass to the function will vary. This could lead to the belief that it will never provide a value that can produce an unexpected result.

Have you ever tried to use Shakespeare’s entire work in Japanese and Korean as an input to your method that takes a valid string as a parameter? It’s an impossible case, so why should we use it as input for a test?

Qualities of Good Testing

What we could expect from good testing? At Codemotion Rome 2019, Kenny Baas and João Rosa shared their ideas about how testing should work to shift from just confirming to learning. When your software breaks, it is caused by at least one mistake. It happens, but you can leverage those mistakes to learn more about what you built or the actual context of the problem you are trying to solve.

Single abstraction examples in property based unit tests
Abstraction as Unit testing

Learn and Challenge Code Through Property Based Testing

If you want to learn more about the context, or the domain you are working on, your tests should describe and communicate what is happening. Domain-Driven Design can help you to write code that is based on the same language developers speak with each other and other domain experts. Tests should help you to document how your software behaves, not assert that it can pass a pile of anonymous data points.

To challenge the code you wrote, you need to move away from the comfort zone of known valid inputs and feed it unforeseen data that can disprove your illusion of safety. If you can do so, failures in tests will reveal an actual problem in your system. If not, the tests will only assert that your system will work when it flows as expected.

Such kind of testing is called “property based testing” and helps to introduce a degree of fuzziness in test inputs, allowing to move from single abstraction examples (a.k.a. unit tests) to an active scanning of system behaviour.

In the next article of this series, we’ll explore more about how to start building property based testing. Stay tuned…

Related Posts

automated security testing, devops, software, applications

Automating Security Testing with SCA, SAST and DAST

Gilad David Maayan
November 4, 2022
Software Quality Assurance Analyst, automated testing

Automated Testing Guide: What And What NOT to Automate When Testing Software

Matthew Cooper
April 13, 2022

Top 7 QA Testing Best Practices to Ensure Overall Software Quality

Kate Priestman
November 17, 2021

How to Challenge Your Code with Property-Based Testing – Part 3

Luca Ferretti
February 14, 2020
Share on:facebooktwitterlinkedinreddit

Tagged as:Codemotion Rome Testing

Luca Ferretti
Affezionato al caro vecchio C, passato non troppo recentemente alle più arzigogolate frontiere del Web, Luca Ferretti ha da poco scelto il suo motto su Twitter: I break stuff, I build relationships. È così, tra una ispezione del DOM e una apparizione nella stanza accanto per discutere con il team del frontend di un pixel messo storto, tra una traduzione di Ubuntu e un rebuild dei sorgenti di GNOME (rigorosamente di notte), che trascorre le sue giornate nell'incessante ricerca della perfezione ;-)
Refactoring an Angular application through a live coding session
Previous Post
Jobs in artificial intelligence and robotics top LinkedIn list for 2020
Next Post

Footer

Discover

  • Events
  • Community
  • Partners
  • Become a partner
  • Hackathons

Magazine

  • Tech articles

Talent

  • Discover talent
  • Jobs

Companies

  • Discover companies

For Business

  • Codemotion for companies

About

  • About us
  • Become a contributor
  • Work with us
  • Contact us

Follow Us

© Copyright Codemotion srl Via Marsala, 29/H, 00185 Roma P.IVA 12392791005 | Privacy policy | Terms and conditions