Tôi đang sử dụng nose để kiểm tra ứng dụng sử dụng subprocess.Popen()
để gọi tập lệnh. Không cần plugin capture hoặc logcapture để thu được đầu ra của tập lệnh này. Có cách nào dễ dàng để đưa đầu ra này vào mũi không?Ghi lại đầu ra của subprocess.Popen() bằng mũi
Đây là những gì tôi đã cố gắng cho đến thời điểm này; lưu ý "Chụp cho tôi" không chiếm được:
example.py:
if __name__ == '__main__':
print 'Capture me'
test.py:
import subprocess
import sys
def test_popen():
# nose's capture plugin replaces sys.stdout with a StringIO instance.
# subprocess.Popen() expects stdout to have a fileno property, which
# StringIO doesn't, so fake it.
sys.stdout.fileno = lambda: 1
subprocess.Popen(['python', 'example.py'], stdout=sys.stdout)
assert False # Force test to fail so we see nose output
đầu ra:
$ nosetests test.py
F
======================================================================
FAIL: test.test_popen
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/pmdarrow/Envs/example/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/pmdarrow/Code/example/API/test.py", line 8, in test_popen
assert False
AssertionError
----------------------------------------------------------------------
Ran 1 test in 0.004s
FAILED (failures=1)
Capture me
Tại sao tham số 'stdout' tùy chỉnh nếu bạn gửi nó tới' sys.stdout' bằng cách nào? Bạn đã thử không? – Wolph
Nó chỉ là một nỗ lực để buộc 'Popen()' để gửi đầu ra của nó để mũi. Đầu ra là như nhau mà không có nó. – pmdarrow
cách khác, nếu bạn đang tìm đầu ra, hãy thử 'subprocess.check_output()' –