Jim Garrison
Jim Garrison

Reputation: 4276

pytest.raises(AssertionError) fails with cython modules

Define the files python_assert.py and cython_assert.pyx to be identical, each containing a simple function that raises AssertionError:

def raise_assertionerror():
    assert False

I would expect both of the following tests to succeed under pytest:

import pytest

import pyximport
pyximport.install()

from python_assert import raise_assertionerror as python_assert
from cython_assert import raise_assertionerror as cython_assert

def test_assertion():
    with pytest.raises(AssertionError):
        python_assert()

def test_cython_assertion():
    with pytest.raises(AssertionError):
        cython_assert()

However, the cython test fails:

===================================== FAILURES ======================================
_______________________________ test_cython_assertion _______________________________

    def test_cython_assertion():
        with pytest.raises(AssertionError):
>           cython_assert()

test_pytest_assertion.py:15: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

>   assert False
E   AssertionError

cython_assert.pyx:2: AssertionError
======================== 1 failed, 1 passed in 0.61 seconds =========================

This seems to be a problem with pytest because the equivalent unittest succeeds:

import unittest

import pyximport
pyximport.install()

from python_assert import raise_assertionerror as python_assert
from cython_assert import raise_assertionerror as cython_assert

class MyTestCase(unittest.TestCase):
    def test_cython(self):
        self.assertRaises(AssertionError, cython_assert)

    def test_python(self):
        self.assertRaises(AssertionError, python_assert)

if __name__ == "__main__":
    unittest.main()

Further, the pytest test succeeds if we call pytest.raises(Exception) instead of pytest.raises(AssertionError).

Any idea what is wrong?

Upvotes: 4

Views: 2178

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 4276

This has been recognized as a bug in pytest and resolved.

Upvotes: 4

Related Questions