小编Arm*_*ius的帖子

在更改的测试文件上重复运行 pytest

我想在 Python 中多次运行 pytest 而无需重新启动脚本/解释器。

问题是 pytest 正在缓存测试内容/结果。也就是说,如果您在两次运行之间修改测试文件,pytest 不会接受更改,并显示与之前相同的结果。(除非您重新启动脚本/退出解释器,当您从命令行使用 pytest 时,您自然会这样做。)

再生产

test_foo.py:

def test_me():
    assert False
Run Code Online (Sandbox Code Playgroud)

在 Python shell 中:

>>> import pytest
>>> pytest.main(['test_foo.py'])
(...)
    def test_me():
>       assert False
E       assert False

test_foo.py:2: AssertionError
Run Code Online (Sandbox Code Playgroud)

目前很好。现在不要退出解释器,而是将测试更改为assert True并重新运行 pytest。

>>> pytest.main(['test_foo.py'])
(...)
    def test_me():
>       assert True
E       assert False

test_foo.py:2: AssertionError
Run Code Online (Sandbox Code Playgroud)

预期结果

Pytest 应该已经发现文件中的更改并通过重写的测试。

不起作用的解决方案

  • 用于importlib.reload(pytest)在运行之间重新加载 pytest。

  • 运行 pytest 并清除缓存:pytest.main(['--cache-clear', test_foo.py'])

(不能选择将 pytest 作为子进程运行,因为我想从应用程序中引用 pytest 模块。)

有任何提示如何让 pytest 接受这些更改或如何正确重新加载模块吗?

python pytest python-3.x

5
推荐指数
1
解决办法
2280
查看次数

在理解的symtable中,这些额外的符号是什么?

我正在使用symtable一段代码的符号表.奇怪的是,当使用理解(listcomp,setcomp等)时,我还没有定义一些额外的符号.

复制(使用CPython 3.6):

import symtable

root = symtable.symtable('[x for x in y]', '?', 'exec')
# Print symtable of the listcomp
print(root.get_children()[0].get_symbols())
Run Code Online (Sandbox Code Playgroud)

输出:

[<symbol '.0'>, <symbol '_[1]'>, <symbol 'x'>]
Run Code Online (Sandbox Code Playgroud)

符号x是预期的.但是什么.0_[1]

请注意,对于任何其他非理解构造,我正好得到了我在代码中使用的标识符.例如,lambda x: y仅产生符号[<symbol 'x'>, <symbol 'y'>].

此外,文档说这symtable.Symbol是......

SymbolTable与源中的标识符对应的条目.

......虽然这些标识符显然没有出现在源代码中.

python list-comprehension python-3.x python-internals

3
推荐指数
1
解决办法
149
查看次数