我最近一直在修整测试,我从来没有测试过一个classfunction是否成功打印,所以我决定试一试。现在,测试是正确运行的,我甚至有几个版本的测试,但是下面这个特殊的实现是由我使用的linter引发了一个错误,即使代码运行。它说:
"在方法callpylint(no-value-for-parameter)中,参数'mock_stdout'没有值"
但是,当我运行测试时,该值被发现并运行,没有错误。
谁能解释一下为什么会这样?是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的一个bug限制。(请看 https:/github.comPyCQApylintissues323。 和 https:/github.comPyCQApylintissues3108。.)
我不想在pylint中全局禁用E1120,因为在其他情况下,我还是喜欢这个错误,也不想在pylint中添加 # 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
# ...