Nose로 통합 테스트를 실행하고 있습니다. 내 테스트 중 하나에서 출력을 가져 와서 특정 문자열을 찾기 위해 구문 분석해야합니다.Python - 외부 프로그램을 실행하는 함수 호출에서 모든 출력 캡처
def test_diagnostic_tool():
with Capturing() as output:
failures = run_integration_testing_exe('*', test_exe='DiagnosticTool.exe', use_default_composed_filter=False)
eq_(failures, 0)
class Capturing(list):
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._stringio = StringIO()
return self
def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
sys.stdout = self._stdout
이것은 작동하지 않습니다. 'output'변수는 호출 구조의 어딘가에있는 작은 문자열 만 포함합니다.
'run_integration_testing_exe'함수가 다른 함수를 호출 중이고 외부 exe 프로그램이 호출되었습니다. 셸에서는 everthing 출력이 좋으므로 모든 내용을 캡처하고 구문 분석 할 수있는 방법이 있는지 궁금합니다.
;-) 패치 할 수 있습니다 exe를 실행중인 경우'subprocess.Popen'이 호출됩니다. – deinonychusaur
그럴 수 있으면 좋겠다. 이 함수는 테스트 프레임 워크의 일부이며 수정할 수 없습니다 (테스트 스크립트에서 가져옵니다). – Amaranth
누락 된 항목이 stdout이 아닌 stdout에 실제로 쓰여졌습니까? 그렇지 않으면'DiagnosticTool.exe'를 시작하는 코드를 얻을 수 없다면 아마 파이썬 안에서 행운을 빕니다. – deinonychusaur