我使用的是cocotb1.5.2版本,我想编写一个实用函数来创建每个测试的报告/图表。
MWE:实现get_test_name函数,以便下面的测试将打印my_wonderful_test。
import cocotb
@cocotb.test()
async def my_wonderful_test(dut):
print(get_test_name(dut));
def get_test_name(dut):
pass # how do I get the current test from here?发布于 2022-06-16 08:11:22
谢谢你的评论,并感谢你@FabienM试图给出一个答案。谢谢你@Tzane试图找到答案。你们关系很好。
如果你想知道一个线性答案
import cocotb;
def get_test_name():
return cocotb.regression_manager._test.name但是_test中的下划线前缀在将来可能会中断,但是因为我只关心1.5.2版本,所以这对我来说是可以的。
无论如何,我都可以实现另一种方法,即一次扫描堆栈一层,并检查框架是否位于cocotb.test修饰函数中。这也是cocotb用于测试的方法。
如果测试处于闭包状态,它将无法工作,但我从不使用它,我也不知道它是否被支持。
import cocotb
import inspect;
import sys
@cocotb.test()
async def test_get_testname(dut):
print('Runnign from test ', get_test_name())
def get_test_name():
try:
return cocotb.regression_manager._test.name
except:
pass
cocotbdir = '/'.join(cocotb.__file__.split('/')[:-1])
frame = sys._getframe();
prev_frame = None
while frame is not None:
try:
# the [documentation](https://docs.python.org/2/library/inspect.html#inspect.getmodule)
# says
# Try to guess which module an object was defined in.
# Implying it may fail, wrapp in a try block and everything is fine
module = inspect.getmodule(frame.f_code)
func_name = inspect.getframeinfo(frame).function
if hasattr(module, func_name):
ff = getattr(module, func_name)
if isinstance(ff, cocotb.test):
return func_name
except:
pass
prev_frame = frame;
frame = frame.f_back;
# return None if fail to determine the test name我不知道为什么我的问题被如此糟糕的接受
这是一件很简单的事,我更愿意接触更多的人。
发布于 2022-06-13 13:05:53
您可以使用"name“属性:
import cocotb
@cocotb.test()
async def my_wonderful_test(dut):
print(my_wonderful_test.name);但不确定你到底想要什么。
https://stackoverflow.com/questions/72599040
复制相似问题