import sys import pexpect import pytest @pytest.fixture def proc_factory(): procs = [] def call_proc(cmd, args, logfile=None, encoding=None): if logfile is None: logfile = sys.stdout if encoding is None: encoding = "utf-8" proc = pexpect.spawn(cmd, args, logfile=logfile, encoding=encoding) procs.append(proc) return proc try: yield call_proc finally: for proc in procs: proc.close()