this post was submitted on 18 Dec 2023
14 points (93.8% liked)

Python

6368 readers
16 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS
top 1 comments
sorted by: hot top controversial new old
[–] sugar_in_your_tea@sh.itjust.works 3 points 11 months ago

While I largely agree with the net result, I think this mantra kind of misses the point and could easily lead to dogmatic programming.

Mocking external tools is bad because it indicates poor code and testing structure. We want our tests to be predictable and indicate issues only if something directly related to the test changes behavior. If a bug is introduced, ideally only one or a small handful of tests will fail. If the network fails, ideally no tests fail. It's all about limiting the ways your test could fail to just the thing you're testing.

For example, if that client library expects an extra argument (maybe auth token), every function that uses that call could fail if it's not mocked, whereas if it's mocked, only the one or two tests that hit the API more completely would fail.

This leaves code untested, but that's fine because it'll be caught with integration testing. Unit testing tests units, and we want that units to be as small and independent as possible.

So it's not about whether you own the code, it's about being very clear about what it is you're testing. Let's say I have the following setup:

def a():
   b()

def b():
   c()

def c():
   external_library()

In this case, I'd mock b when testing a, mock c when testing b, and mock external_library when testing c. It has nothing to do with whether I own it, only whether I'm testing it. If I'm mocking external_library everywhere, that's a code smell that my code is too tightly coupled to that external library. The same applies to b or c. It has nothing to do with whether mocking an external thing is good or bad, but whether I want that coupling in my code.

Well structured code doesn't need this "rule," and and poorly structured code should be caught before getting to the point of writing tests (unless you're doing TDD, but that's a dev strategy, not a testing strategy per se).