Python 方法调用方的Get模块

Python 方法调用方的Get模块,python,introspection,pytest,Python,Introspection,Pytest,到目前为止,我们使用它来获取方法调用方的模块: frame=inspect.currentframe().f_back module=inspect.getmodule(frame.f_code) 除了在py.test测试用例中,它工作得很好 test将文件名作为模块名返回 我们的测试代码布局: foomodule/lib/examplecode.py foomodule/tests/test_examplecode.py 如果在examplecode.py中调用,则结果为“foomodul

到目前为止,我们使用它来获取方法调用方的模块:

frame=inspect.currentframe().f_back
module=inspect.getmodule(frame.f_code)
除了在py.test测试用例中,它工作得很好

test将文件名作为模块名返回

我们的测试代码布局:

foomodule/lib/examplecode.py
foomodule/tests/test_examplecode.py

如果在
examplecode.py
中调用,则结果为“foomodule”。但是在
test\u examplecode.py
中调用时,它返回“test\u examplecode”。

py.test确实与被测模块的AST一起工作,因此它可能会更改此值(错误?)。如果是这样,这可能是一个bug

你能举一个失败的测试例子吗?例如,澄清检查代码是作为测试功能运行还是作为测试代码运行

您还可以指出是否存在
\uuuu init\uuuu.py
文件,因为这可能有助于明确软件包和模块是什么

尽管如此,以这种方式使用堆栈和检查通常是不受欢迎的