gpt4 book ai didi

cython - pytest.raises(AssertionError) 使用 cython 模块失败

转载 作者:行者123 更新时间:2023-12-01 01:16:20 25 4
gpt4 key购买 nike

将文件 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 测试成功。而不是 pytest.raises(AssertionError) .

知道有什么问题吗?

最佳答案

这是recognized as a bug in pytest并解决。

关于cython - pytest.raises(AssertionError) 使用 cython 模块失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11857953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com