• 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 FerrettiFebruary 12, 2020

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

QA/Testing
facebooktwitterlinkedinreddit
Table Of Contents
  1. Introduction to property-based testing
  2. Implementing Tests
  3. Code as Your Domain Speaks

Property-based testing suggests a new way to test software, going beyond the example-based approach and stressing your code with random, realistic inputs. Follow this 3-issue mini serie about property-based testing and Kenny Baas and João Rosa speech at Codemotion Rome 2019.

Introduction to property-based testing

In the previous issue of this mini-series about property-based testing, we introduced some basic ideas from which property-based testing is derived. To sum up, property-based testing introduces a degree of fuzziness in test inputs, allowing to spot cases when the system under test behaves in unexpected way.

Recommended article
OOMKilled Error in Linux and Kubernetes and How to Solve It
January 13, 2023

What is the OOMKilled Error in Linux and Kubernetes and How to Solve It

Gilad David Maayan

QA/Testing

Let’s see a trivial but concrete case, showing how to convert check for a simple method from unit testing style to a property-based approach. Following example will use Java language (and JUnit testing framework) and QuickCheck implementation for Java/Junit.

Implementing Tests

Our mission is to write and verify a method that checks if a discount can be applied. A really naive implementation of such method and related tests can be something like the following one:

Everytime we run those unit tests, we’ll repeat the same two checks again and again and again. Moreover, we are checking how our method is behaving using two values with no meaning or reason.

First step is write a rationally meaningful check. For example, using  common testing techniques such as boundary-value analysis and equivalence partitioning we can choose a set of relevant values to test. At the same time, we can use some features provided by testing framework to parametrize our test run and avoid to have one separate test method for each chosen value.

So we can rewrite our tests in the following way:

Great! It’s a step forward, but it still lacks two valuable features of the kind of testing we are aiming for. First, our tests are still performed against some pre-chosen and fixed value on every execution. Second, our tests output will we a sequence of true and false, with no particular legibility or meaningfulness about the actual behavior.

Let’s try to rewrite those checks using a property-based framework such as QuickCheck and its runner for JUnit:

JUnit QuickCheck syntax should be simple to understand: basically, we are asking to perform 500 trials (i.e. runs) of each test method, providing each time a different value randomly chosen from the given ranges.

Starting from a trivial unit test, we have built a super test that will able to warn us if out code is doing an undesired behavior when feeded with an unexpected input.

When this happens, QuickCheck provides info about the faulty input and a seed to reproduce it and debug it.

Code as Your Domain Speaks

To be honest, the actual benefit of the example provided so far is a little deceptive.

Of course, it is useful to show how to move from unit testing to property-based testing, but it lacks some prominent traits from a production code perspective.

In fact, In order to isolate the DiscountService class and simplify our test code, we deliberately took no account for any other object in the domain that could interact with our discount check. In a real world scenario, the price we check against could be the sum of prices of different items in your basket, and maybe in your business logic you have to exclude some items from discount calculation.

Moreover, we used the Java double type to represent money and we set a test range up to 49.99. This means that, for example, 49.991 can be a valid input for our method, but it will never occur as test input (if you consider, for example, fuel prices per liter in Europe, 49.991 can be a valid price that must be rounded).

In the next issue we’ll see how property-based testing can use generators feature from QuickCheck library to create random input for our own data types and domain/business logics.

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 Java

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 ;-)
Why Remote Work Is the New Normal for Devs
Previous Post
So you want to work in cybersecurity?
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