r/Python • • Mar 07 '24

Discussion Tests should have 100% coverage

No, I'm not saying what you probably think I am. Keep reading: I'll be brief.

Here's what I found today in one of the tests:

for bad_fp in _bad_fps:
    with pytest.raises(BadArgumentError):
        follow_path(bad_fp, '.')
        follow_path('.', bad_fp)

The correct code should've been

for bad_fp in _bad_fps:
    with pytest.raises(BadArgumentError):
        follow_path(bad_fp, '.')
    with pytest.raises(BadArgumentError):
        follow_path('.', bad_fp)

Is that a stupid mistake? Yes, it is, but stupid mistakes do happen, unfortunately.

How did I catch it? The coverage of the testing file wasn't 100% like it usually is! I got suspicious and I saw that there was a glaring red line (vscode, coverage gutter) on the second call to follow_path.

P.S. In case you're wondering, the code uses a custom hierarchy of exceptions instead of reusing the standard ones. The code is internal and we like it that way :)

0 Upvotes

37 comments sorted by

View all comments

29

u/ToddBradley Mar 08 '24

Also, I think you (or someone else) would've noticed this bug sooner if there was only one assertion in the test and you fed the scenarios in via pytest.mark.parametrize.

3

u/Kiuhnm Mar 08 '24

In this case, it would've certainly prevented the bug, but, in general, I'm not sure having small and specific tests is without downsides. We have no strict guidelines about this.

For instance, my code is usually not so straightforward to test because it involves algorithms on graphs, so I end up generating random graphs and manipulating them while asserting that everything is behaving as it should. This results in test functions that can have up to 20 assertions each. (I also write formal proofs of course.)

The 1-assertion-per-test limitation would make my life much harder and, I think, my tests less reliable. Fixtures do help, but are not enough.

Let me know if you have any suggestions!

9

u/ToddBradley Mar 08 '24

"generating random graphs" and "everything is behaving as it should" makes me wonder if you're writing unit tests or something higher on the test automation pyramid.

1

u/Kiuhnm Mar 08 '24

I still consider them unit tests because there's nothing below them, but a unit may be a small class instead of a single function or method.

If I'm trying to prove 10 different behaviors of a class, I'll write 10 different test functions.

I'm under the impression that many programmers want to know what's wrong with their test just by looking at the logs. I'm just happy to know that there's a problem with a particular graph and sequence of operations. Then I analyze the whole thing (we're still talking of a single class) and find out what's wrong.

I guess different problems require different approaches.

The mistake I talked about wasn't mine :), but I dislike relying on "advanced" pytest features as well. I feel that they're too limited for my taste. For instance, pytest.mark.parametrize only supports a single sequence of args. Repeating the decorator results in the cartesian product of the args, which is not what I want.

Why should I try to combine big lego blocks to get what I want when I can just use Python itself?

Also, if I change testing library, most of what I wrote will still work.

I'm probably the only guy who organizes his notes as a tree of small txt files, so maybe it's just me. I like to keep things simple. Luckily for me, I have lots of freedom.

1

u/eveninghighlight Mar 08 '24

I think you're right about the purpose of tests, in my mind the ideal testing dashboard is a big green button if they all pass and then a big red button if any fail

then you have to do some digging to find out what failed

2

u/Kiuhnm Mar 08 '24

"Something bad happened! Come home!" and hangs up.

I'm just joking :)

Some dashboards organize tests in a tree so that, when it's completely collapsed, you get your single big green/red button.

2

u/eveninghighlight Mar 08 '24

Yeah in my mind you could click on the button to see more, find which test failed, the logs etc but the headline message is that something broke