py.test 文档说我应该将 capsys 参数添加到我的测试方法中,但就我而言,这似乎不可能。
class testAll(unittest.TestCase):
def setUp(self):
self.cwd = os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe()))[0])
os.chdir(self.cwd)
def execute(self, cmd, result=0):
"""
Helper method used by many other tests, that would prevent replicating too much code.
"""
# cmd = "%s > /dev/null 2>&1" % cmd
ret = os.system(cmd) >> 8
self.assertEqual(ret, result, "`%s` returned %s instead of %s (cws=%s)\n\t%s" % (cmd, ret, result, os.getcwd(), OUTPUT)) ### << how to access the output from here
def test_1(self):
self.execute("do someting", 0)
您可以在继承的类中定义一个辅助函数capsys
夹具:
@pytest.fixture(autouse=True)
def capsys(self, capsys):
self.capsys = capsys
然后在测试中调用这个函数:
out,err = self.capsys.readouterr()
assert out == 'foobar'
感谢 Michał Krassowski 的解决方法,帮助我解决了类似的问题。
https://github.com/pytest-dev/pytest/issues/2504#issuecomment-309475790 https://github.com/pytest-dev/pytest/issues/2504#issuecomment-309475790
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)