我正在学习 pytest 并使用 pylint 对我的代码进行 lint。但是 pylint 仍然抱怨:
W0621: Redefining name %r from outer scope (line %s)
对于来自 pytest 的以下示例:
# test_wallet.py
@pytest.fixture
def my_wallet():
'''Returns a Wallet instance with a zero balance'''
return Wallet()
@pytest.mark.parametrize("earned,spent,expected", [
(30, 10, 20),
(20, 2, 18),
])
def test_transactions(my_wallet, earned, spent, expected):
my_wallet.add_cash(earned)
my_wallet.spend_cash(spent)
assert my_wallet.balance == expected
从外部范围重新定义名称 my_wallet
。
我找到了将 _
前缀添加到夹具名称的解决方法: _my_wallet
。
如果我想将固定装置与函数保存在同一个文件中,最佳做法是什么?
- 在所有灯具前加上
_
? - 禁用此
pylint
检查测试? - 更好的建议?
原文由 oglop 发布,翻译遵循 CC BY-SA 4.0 许可协议
我刚刚在我的测试文件中禁用了该规则: