跳至内容

等待成功退出

等待退出状态码为 0。

successful_exit.py

import sys
sys.stdout.write("hello")
sys.stdout.flush()
unsuccessful_exit.py

import sys
sys.stderr.write("something went wrong!")
sys.stderr.flush()
sys.exit(255)

带有代码

from icommandlib import ICommand
from commandlib import python

没有错误

process = ICommand(python("successful_exit.py")).run()
process.wait_for_successful_exit()

with open("finalscreenshot.txt", "w") as handle:
    handle.write(process.screenshot())

assert process.exit_code == 0, process.exit_code
  • 当代码运行完成时。

然后,finalscreenshot.txt 的文件内容将是

hello

不成功退出

ICommand(python("unsuccessful_exit.py")).run().wait_for_successful_exit()

将引发类型为 icommandlib.exceptions.ExitWithError 的异常,消息为

Process exited with non-zero exit code 255. Output:
something went wrong!

可执行规范

wait-until-successful-exit.story storytests 自动生成的文档。