COMP1531
🐶Software Engineering
8.2 - Testing - Property based testing
Author: Hayden Smith 2021
In this lecture
Why?
- Good testing is important, but good testing can take a lot of time to write.
- It's easy to miss edge cases, and this problem compounds with larger code bases
What?
- Property based testing
Can the machines do it for us?
-
To write a program that generates tests for us requires:
- A means of generating test data
- Knowing what behaviour is correct?
- Some way for the computer to report test failures to us in an understandable way
Property-based testing
- A method of testing where tests are defined as general properties (i.e. parameterised predicates)
- Test input is generated automatically by supplying a strategy for generating that input
- The testing framework runs the test many times to ensure the properties are true for each input
- In the event of a test failure, the framework will shrink the generated input to find the smallest value that still fails the test
Hypothesis
- Hypothesis is the name of property-based testing framework for python
-
Can be installed via: pip3 install hypothesis
- Parameterised tests are decorated with @given to supply strategies for generating test input
- See bubblesort.py
What properties to test?
- It's not always easy to find testable properties
- Can you think of one for the Zune bug example?
- Software designs with testable properties tend to be good designs...
def bubblesort(numbers):
numbers = numbers.copy()
for _ in range(len(numbers) - 1):
for i in range(len(numbers) - 1):
if numbers[i] > numbers[i+1]:
numbers[i], numbers[i+1] = numbers[i+1], numbers[i]
return numbers
Feedback
COMP1531 21T3 - 8.4 - SDLC Testing - Property-based testing
By haydensmith
COMP1531 21T3 - 8.4 - SDLC Testing - Property-based testing
- 1,442