所以我这样做的方法是pytest https://pytest.org
Example: (做作的)
from subprocess import Popen, PIPE
def test():
p = Popen(["echo", "Hello World!"], stdout=PIPE)
stdout, _ = p.communicate()
assert stdout == b"Hello World!\n"
Output:
$ py.test -x -s test_subprocess.py
======================================= test session starts ========================================
platform linux2 -- Python 2.7.9 -- py-1.4.28 -- pytest-2.7.1
rootdir: /home/prologic/work/circuits, inifile:
plugins: cov
collected 1 items
test_subprocess.py .
===================================== 1 passed in 0.01 seconds =====================================
或者使用标准库unittest https://docs.python.org/2/library/unittest.html:
Example:
#!/usr/bin/env python
from unittest import main, TestCase
from subprocess import Popen, PIPE
class TestProcess(TestCase):
def test(self):
p = Popen(["echo", "Hello World!"], stdout=PIPE)
stdout, _ = p.communicate()
self.assertEquals(stdout, b"Hello World!\n")
if __name__ == "__main__":
main()
Output:
$ python test_subprocess.py
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK