subprocess.Popen(“ pytest”)返回2,尽管pytest返回1

时间:2019-06-18 12:48:55

标签: python python-2.7 subprocess pytest

我正在从subprocess.Popen运行pytest。

从下面的代码及其输出中可以看出,pytest以退出代码1退出,但进程认为它以退出代码2退出。为什么? (当前认为它是pytest错误[错误与描述匹配,但与使我认为他们忘记更新它的代码不匹配])

[错误本身不是问题(它来自拥有多个conftest文件,并且可以,因为我想创建错误)]

In [1]:     from subprocess import Popen
   ...:     p = Popen('pytest -s -k some_phrase'.split())
   ...:     p.communicate()
   ...:     print p.poll()
   ...: 



Test session starts (platform: linux2, Python 2.7.12, pytest 4.5.0, pytest-sugar 0.9.2)
rootdir: /...
plugins: timeout-1.3.3, sugar-0.9.2, repeat-0.8.0, metadata-1.8.0, html-1.20.0, cov-2.7.1
collecting ... Failed to import pybind11_tests from pytest:
  ImportError: No module named pybind11_tests

―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― test session ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
.../local/lib/python2.7/site-packages/py/_path/common.py:377: in visit
    for x in Visitor(fil, rec, ignore, bf, sort).gen(self):
.../local/lib/python2.7/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
.../local/lib/python2.7/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
.../local/lib/python2.7/site-packages/py/_path/common.py:429: in gen
    for p in self.gen(subdir):
.../local/lib/python2.7/site-packages/py/_path/common.py:419: in gen
    if p.check(dir=1) and (rec is None or rec(p))])
.../local/lib/python2.7/site-packages/_pytest/main.py:660: in _recurse
    ihook = self.gethookproxy(dirpath)
.../local/lib/python2.7/site-packages/_pytest/main.py:481: in gethookproxy
    my_conftestmodules = pm._getconftestmodules(fspath)
.../local/lib/python2.7/site-packages/_pytest/config/__init__.py:421: in _getconftestmodules
    mod = self._importconftest(conftestpath.realpath())
.../local/lib/python2.7/site-packages/_pytest/config/__init__.py:443: in _importconftest
    mod = conftestpath.pyimport()
.../local/lib/python2.7/site-packages/py/_path/local.py:668: in pyimport
    __import__(modname)
.../local/lib/python2.7/site-packages/_pytest/assertion/rewrite.py:301: in load_module
    exec(co, mod.__dict__)
...
    sys.exit(1)
E   SystemExit: 1

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Results (0.44s):
2

顺便说一句,我使用os.system('pytest -s -k bla')进行了检查,并得到了错误代码512。看来退出代码根本无法正确传播...


我再次尝试了新的预期错误-我现在希望没有收集到任何测试,并且看来错误代码是正确的:

In [3]:     from subprocess import Popen, PIPE, check_output
   ...:     p = Popen('pytest --disable-warnings -s -k some_phrase', shell=True)
   ...:     p.communicate()
   ...:     print p.poll()
   ...: 
...

Results (2.59s):
    4275 deselected
5 

根据pytest

,这是正确的代码

可能的退出代码 运行pytest可能会导致六个不同的退出代码:

Exit code 0:    All tests were collected and passed successfully
Exit code 1:    Tests were collected and run but some of the tests failed
Exit code 2:    Test execution was interrupted by the user
Exit code 3:    Internal error happened while executing tests
Exit code 4:    pytest command line usage error
Exit code 5:    No tests were collected   <----

0 个答案:

没有答案