Hacker News new | ask | show | jobs
by erezsh 249 days ago
Because my hate for pytest is ardent and dear to my heart, I will try to explain.

I wrote a very simple function:

  def test_whatever():
    a = range(10)
    b = (10 / x for x in a)
    c = list(b)
When I run it with normal Python, this is the exception:

    Traceback (most recent call last):
      File "test_test.py", line 10, in <module>
        test_whatever()
      File "test_test.py", line 6, in test_whatever
        c = list(b)
            ^^^^^^^
      File "test_test.py", line 5, in <genexpr>
        b = (10 / x for x in a)
             ~~~^~~
    ZeroDivisionError: division by zero

It's compact and simple to understand. It pinpoints the exact location of the error, and I easily scan the text to find the function call-stack.

Now here's the pytest error:

    ___________________________________ test_whatever ___________________________________

        def test_whatever():
            a = range(10)
            b = (10 / x for x in a)
    >       c = list(b)

    test_test.py:6:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    .0 = <range_iterator object at 0x107695e90>

    >   b = (10 / x for x in a)
    E   ZeroDivisionError: division by zero

    test_test.py:5: ZeroDivisionError

It doesn't pinpoint the error, it adds code lines that might be irrelevant, and extra information I don't care about.

I will say using `--tb=short` fixes most of it, at least in this example, and sometimes it's even preferable, because it's shorter. But it still doesn't pinpoint the error like normal Python exceptions do.

1 comments

Thank you for sharing this example! You've perfectly captured one of my main frustrations: pytest tries to be "helpful" by adding extra context, but ends up obscuring the actual error.