gpt4 book ai didi

Python Linter 表示方法调用中的值在已修补的单元测试中丢失,但测试运行正常。为什么?

转载 作者:行者123 更新时间:2023-12-04 17:35:00 25 4
gpt4 key购买 nike

我最近一直在修补测试,但我从来没有测试过一个类/函数是否已成功打印,所以我决定尝试一下。现在,测试运行正常,我什至有多个版本的测试,但下面的这个特定实现在我使用的 linter 引发错误,即使代码运行也是如此。它说:

"No value for argument 'mock_stdout' in method callpylint(no-value-for-parameter)"

然而,当我运行测试时没有错误地发现并运行该值。

有人能解释一下为什么会这样吗?这是 linter 中的错误吗?有没有一种方法可以优化我的代码以便不显示错误?

    class StringManipulationTwo(object):
def __init__(self):
self.s=""

def getstring(self):
self.s = input("insert values here")

def printstring(self):
print(self.s.lower(),end='')

@patch('builtins.input', lambda *args: 'testing')
@patch('sys.stdout', new_callable=io.StringIO)
def assert_stdout(self, expected_output, mock_stdout):
xobj = StringManipulationTwo()
xobj.getstring()
xobj.printstring()
self.assertEqual(mock_stdout.getvalue(), expected_output)

def test_printstringtwo(self):
self.assert_stdout('testing')

最佳答案

这是 pylint 中的错误/限制。 (参见 https://github.com/PyCQA/pylint/issues/323https://github.com/PyCQA/pylint/issues/3108。)

我不想在 pylint 中全局禁用 E1120,因为在其他情况下我仍然喜欢这个错误,我也不想添加 # pylint: disable=no-value-for-parameter 对于每个调用点。

相反,我通过使假定缺少的参数成为可选参数然后检查它们是否在运行时提供来解决这个问题:

@patch('builtins.input', lambda *args: 'testing')
@patch('sys.stdout', new_callable=io.StringIO)
def assert_stdout(self, expected_output, mock_stdout=None):
assert mock_stdout

# ...

关于Python Linter 表示方法调用中的值在已修补的单元测试中丢失,但测试运行正常。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57015023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com