我在下面有一个简单的测试,我用pytest. 它通过内置函数工作exec:
def test_exec() -> None:
foo = "" # Causes test to fail if not updated by exec
exec("foo = 5")
assert isinstance(foo, int)
在pytest(via ipython) 之外运行时,代码可以正常工作。通过运行时pytest,该exec语句似乎没有正确运行。
如何将exec函数作为 a 的一部分运行pytest?
版本
Python==3.8.5
pytest==6.1.1