Shorten your feedback loops by increasing the speed of your test suite


Python clean test tip:

Your tests should be fast. The faster the tests the faster the feedback loop.

Consider using mocks or test doubles when dealing with third-party APIs and other slow things.

Example:

import time


def fetch_articles():
    print("I'm fetching articles from slow API")
    time.sleep(10)
    return {"articles": [{"title": "Facebook is Meta now."}]}


# BAD
def test_fetch_articles_slow():
    assert fetch_articles() == {"articles": [{"title": "Facebook is Meta now."}]}


# GOOD
def test_fetch_articles_fast(monkeypatch):
    monkeypatch.setattr(time, "sleep", lambda timeout: None)
    assert fetch_articles() == {"articles": [{"title": "Facebook is Meta now."}]}