4

将文件 python_assert.py 和 cython_assert.pyx 定义为相同的,每个文件都包含一个引发 AssertionError 的简单函数:

def raise_assertionerror():
    assert False

我希望以下两个测试都能在 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()

但是,cython 测试失败:

===================================== 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 =========================

这似乎是 pytest 的问题,因为等效的 unittest 成功:

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()

此外,如果我们调用pytest.raises(Exception)而不是pytest.raises(AssertionError).

知道有什么问题吗?

4

1 回答 1

4

这已被识别为 pytest 中的错误并已解决。

于 2012-12-12T17:37:07.523 回答