pythonunittest:打开并等待程序关闭

2024-09-27 21:23:29 发布

您现在位置:Python中文网/ 问答频道 /正文

目前,我尝试创建一个单元测试来打开一个文件(使用相应的应用程序),然后测试运行应该等到程序关闭。在

def test_HFG(self):
#....
print "please edit this file"
os.chdir(r'C:\test\a')
os.startfile("myfile.vdx")
# here I need a "stop until the program is closed"-function
#....

有人知道如何实现(尽可能简单)我的计划吗?在


Tags: 文件testself程序应用程序osdef单元测试
3条回答

从文件中:

startfile() returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application’s exit status.

如果您知道要打开文件的应用程序的路径,则可以使用子流程.Popen()让您可以等待。在

参见: http://docs.python.org/library/os.html#os.startfile

http://docs.python.org/library/subprocess.html#subprocess.Popen

os.startfile的文档明确指出:

startfile() returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application’s exit status

因此,我建议使用另一种方法,例如通过subprocess.Popen启动它,这允许您等待子进程完成。在

当然,os.startfile是完全非阻塞的,没有等待的选项。在

我建议使用subprocess模块,调用Windows“start”命令打开带有关联对象的文件,这与os.startfile的操作相同,但允许您等待进程完成。在

例如:

subprocess.call(["start", my_file])

相关问题 更多 >

    热门问题