问题 在官方教程 中有这样一个事例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 from RandomGenerator import DUTRandomGeneratorimport randomfrom toffee import start_clock, create_dutfrom toffee_test import ToffeeRequestclass LFSR_16 : def __init__ (self, seed ): self.state = seed & ((1 << 16 ) - 1 ) def Step (self ): new_bit = (self.state >> 15 ) ^ (self.state >> 14 ) & 1 self.state = ((self.state << 1 ) | new_bit) & ((1 << 16 ) - 1 )@toffee_test.testcase async def test_with_ref (dut: DUTRandomGenerator ): ref = LFSR_16(seed) for i in range (65536 ): dut.Step() ref.Step() rand = dut.random_number.value assert rand == ref.state, "Mismatch" @toffee_test.fixture async def dut (toffee_request: ToffeeRequest ): dut = create_dut(DUTRandomGenerator, "clk" ) seed = random.randint(0 , 2 **16 - 1 ) dut.seed.value = seed dut.reset.value = 1 dut.Step() dut.reset.value = 0 dut.Step() return dut
这个在用 pytest . -sv
的时候会报错,因为:
1 2 3 4 >>> from toffee import start_clock, create_dut Traceback (most recent call last): File "<stdin>" , line 1, in <module> ImportError: cannot import name 'create_dut' from 'toffee' (/home/luyoung/.local/lib/python3.10/site-packages/toffee/__init__.py)
create_dut 定义在 ToffeeRequest 类中(toffee_test.request模块里),它是类的实例方法,不是模块的顶层函数
因此需要先获得 ToffeeRequest 的实例,再调用它的 create_dut 方法:
1 2 3 4 5 6 7 8 9 10 from toffee import start_clockfrom toffee_test import ToffeeRequestimport toffee_test@toffee_test.fixture async def dut (toffee_request: ToffeeRequest ): dut = toffee_request.create_dut(DUTRandomGenerator, "clk" ) ... return dut
这样例子就能运行通了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 pytest . -sv ================================== test session starts ================================== platform linux -- Python 3.10.12, pytest-8.4.0, pluggy-1.6.0 -- /usr/bin/python3 cachedir: .pytest_cache rootdir: /home/luyoung/chip_functional_verification/chapter2/example plugins: allure-pytest-2.14.3, asyncio-1.0.0, reporter-html1-0.9.3, xdist-3.7.0, anyio-4.8.0, reporter-0.5.3, toffee-test-0.3.1.dev18+g7ee81a0 asyncio: mode=strict, asyncio_default_fixture_loop_scope=function , asyncio_default_test_loop_scope=function collected 1 item test_with_toffee.py::test_with_ref PASSED =================================== warnings summary ==================================== <frozen importlib._bootstrap>:241 <frozen importlib._bootstrap>:241 <frozen importlib._bootstrap>:241: DeprecationWarning: builtin type SwigPyPacked has no __module__ attribute <frozen importlib._bootstrap>:241 <frozen importlib._bootstrap>:241 <frozen importlib._bootstrap>:241: DeprecationWarning: builtin type SwigPyObject has no __module__ attribute <frozen importlib._bootstrap>:241 <frozen importlib._bootstrap>:241: DeprecationWarning: builtin type swigvarlink has no __module__ attribute -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html ============================= 1 passed, 5 warnings in 0.24s ============================= sys:1: DeprecationWarning: builtin type swigvarlink has no __module__ attribute
还有一些警告,这是 Python 给出的弃用警告(DeprecationWarning),意思是:
这些类型(SwigPyPacked、SwigPyObject、swigvarlink)是由 SWIG 生成的 Python C++ 绑定类型(SWIG 是一种用来自动生成 Python 和 C/C++ 代码接口的工具)
它们是“内置类型”(builtin type),但是缺少 module 属性
module 属性用于标明某个对象属于哪个模块
Python 计划将来不再支持这种缺失 module 属性的内置类型行为,所以提示你这是个弃用的情况。