How to write better tests for your software?
As a long time engineer, one of the best practices I have picked up over the years, is to be able to implement tests that ensure the code I write produce reliable outputs. When I moved into a Data Engineering role, the same practices shifted to applying it for data arriving in various shapes, forms and sizes. Among the most compelling reasons to do that early on, was to maintain idempotency in the data pipelines. Eventually, as data modelling and data transformation were being developed, it moved into data quality tests that are essentially tap into two key concepts — correctness and freshness. Having recently stepped into a role responsible for a broader team, while also having to drive AI-DLC adoption for the teams — the conversations around where would we introduce HITL (Human in the Loop) and how might it impact the pace of delivery of software systems, have grown substantially. I have been deeply studying and exploring this area lately, including Agentic Evals and Verification. While, there are many more nuances to this conversation, I am going to explore just a teeny-tiny part of that in this article here. Testing.
Unit Testing
For anyone coming from an Engineering background, the term, Unit Testing — isn’t new! We’ve all been introduced to it, Senior engineers have denied reviewing PRs until the CI checks pass, PMs factor them into the scope of your deliverables. So what is it?
A Unit test basically defines a fixture — a pre-defined specific set of input values, executes code using that fixture, and then asserts that the result is as expected. Here is an example below:
# calculator.py
class Calculator:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b# test_calculator.py
import pytest
from calculator import Calculator
@pytest.fixture
def calculator():
return Calculator()
def test_add(calculator):
assert calculator.add(2, 3) == 5
def test_subtract(calculator):
assert calculator.subtract(5, 3) == 2And, this is how Pytest, a popular Python testing framework, executes it — by default, it creates a fresh instance for each test. Nifty!

Unit Testing follows a software testing pattern in Test Driven Development (TDD) called Arrange-Act-Assert (AAA) for clarity and maintainability. It often aligns with Behaviour-Driven Development’s (BDD) Given-When-Then structure. The main difference between these is that TDD is checking what the test code is doing, whereas BDD checks it based on what a user might do.
Now, Unit Testing itself is not new. In my experience, I’ve seen teams implementing Integration, Regression, Load, and Smoke testing alongside as well. What I haven’t come across much of is Property-based testing. It has been around for a while in Functional Programming landscape (Haskell, Clojure), but it only became mainstream among the Pythonistas about 10 years ago! Let’s explore — because I think it is awesome!
Property-based Testing (PBT)
Unlike a Unit Test, PBT is the approach where you describe the properties that should always be true for your software, and then check them against many generated inputs. It helps you find the failures you didn’t anticipate — for edge cases you might not have thought about, so you can then take on complex projects with much more confidence.
Now, lets me explain that with an example. Lets use the same application code as before:
# calculator.py
class Calculator:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - bFor PBT, you define the rules your software must adhere to:
Universal Properties
>> a + b = b + a
>> a + 0 = a
>> (a + b) - b = aThen, you write your tests using Hypothesis, a python testing library for this purpose:
# test_calculator.py
from hypothesis import given
from hypothesis import strategies as st
from calculator import Calculator
@given(a=st.integers(), b=st.integers())
def test_addition_is_commutative(a, b):
calculator = Calculator()
assert calculator.add(a, b) == calculator.add(b, a)
@given(a=st.integers())
def test_additive_identity(a):
calculator = Calculator()
assert calculator.add(a, 0) == a
@given(a=st.integers(), b=st.integers())
def test_addition_is_reversible(a, b):
calculator = Calculator()
assert calculator.subtract(calculator.add(a, b), b) == aHypothesis simply generates input values for a pre-defined range and runs your tests with it:

One of the applications in data engineering specifically is, testing properties of data that the code being tested is properly manipulating. For example, say you wrote a sorting function — you might want to test whether it changed the length of the list that you sort — in this case, you are testing the list.length() property.
# Unit test
def test_sort():
assert my_sort([1,3,2]) == [1,2,3]
assert my_sort([1,0,-5]) == [-5,0,1]
# property-based test in Hypothesis
from hypothesis import given, strategies as st
@given(st.lists(st.integers()))
def test_sort(lst):
result = my_sort(lst)
for i in range(len(result)-1):
assert result[i] <= result[i+1]So, replace Unit Testing with PBT then?
Property-based testing (PBT) is often better than Example-based unit testing because it shifts the focus from checking known scenarios to discovering unknown scenarios where your code might fail. However, PBT is not a replacement for unit testing. In fact, property-based tests can themselves be unit tests.
With traditional unit testing, developers tend to test scenarios they already understand. This introduces a natural cognitive bias.
Hypothesis can generate scenarios involving empty collections, negative numbers, extremely large integers, unexpected Unicode characters and unusual combinations of inputs.

This is arguably the most significant benefit beyond test coverage.
When writing traditional tests, you might ask:
What should the output be for this input?
When writing property-based tests, you might ask:
What must always be true about this system?
From a Data Engineering perspective, Below is a list of reusable patterns where designing property-based tests is worth doing:
- Idempotency
- Serialisations
- Aggregations
- Distributed data processing
- Data Integrity checks
- Data Transformations
- Sequencing
- Multi-tenancy
Ever since, I started dabbling in this area, every day has been a journey. Not down the rabbit hole, but through a discovery of new methods that are now becoming de facto! For anyone exploring, I would recommend considering PBT as another method to bring correctness, reliability and data integrity to your software testing framework!