Ridwan Yusuf
1 min readJan 7, 2023


Implementation details in Testing

Recently I came across some fantastic guides when it comes to #testing.

It's a good practice to design your test cases to focus on the behaviour of the code being tested, rather than the implementation details.

I'll give an example shortly.

When you focus on the behaviour instead of the implementation details, your test become more robust and less prone to breaking during refactoring or changes.

sample python code:

def add(a, b):
return a + b

For any argument a and b passed to the add function, it must return the sum of the two number.

A good test case would look like this.

def test_add():
# Focusing on the behavior
# rather than the implementation details.
assert add(7, 3) == 10
assert add(1, 5) == 6
assert add(-2, 2) == 0

As you can see, the test focuses on the behaviour of add.

A bad test focusing on implementation below:

def test_add():
assert add.__code__.co_argcount == 2

.__code__.co_argcount retrieves the number of arguments the function expects.

In this bad test, we focused on the implementation because we are checking that two arguments must be passed to the add function.

So this test is less robust because refactoring add in the future to cater for more than two arguments would make it fail.

#python #testing #coding #goodpractices #growth #cleancode #seniordeveloper #juniordeveloper #codingtips



Ridwan Yusuf

RidwanRay.com -I provide engineers with actionable tips and guides for immediate use