sor*_*rin 6 python unit-testing pytest python-unittest
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)
Run Code Online (Sandbox Code Playgroud)
您可以在继承固定装置的类中定义一个辅助函数capsys:
@pytest.fixture(autouse=True)\ndef capsys(self, capsys):\n self.capsys = capsys\nRun Code Online (Sandbox Code Playgroud)\n然后在测试中调用这个函数:
\nout,err = self.capsys.readouterr()\n\nassert out == \'foobar\'\nRun Code Online (Sandbox Code Playgroud)\n感谢 Micha\xc5\x82 Krassowski 的解决方法,帮助我解决了类似的问题。
\nhttps://github.com/pytest-dev/pytest/issues/2504#issuecomment-309475790
\n