unittest.main() 后执行命令 [英] Execution of commands after unittest.main()

查看:39
本文介绍了unittest.main() 后执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从另一个 Python 脚本调用以下脚本:

I am calling the following script from another Python script:

test.py 日志文件

test.py logfile

它应该运行测试并将结果保存在日志文件中.但是由于某种原因,unittest.main(testRunner=runner) 之后的命令没有被执行.我什至不确定文件在写入后是否被关闭.还有其他方法可以编写脚本吗?!

It should run the test and save the result in the logfile. But for some reason the commands after unittest.main(testRunner=runner) are not being executed. I am not even sure if the file gets closed after writing to it. Is there another way to script it?!

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re, sys

    class SeleniumYahooTest(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Firefox()
            self.driver.implicitly_wait(30)
            self.base_url = "https://www.yahoo.com"
            self.verificationErrors = []
            self.accept_next_alert = True
       . 
       .
       .

        def tearDown(self):
            self.driver.quit()
            self.assertEqual([], self.verificationErrors)


if __name__ == '__main__':

if len(sys.argv)>1:
   testcase_name = sys.argv[0]
   result_file = sys.argv[1]
   del(sys.argv[1:])
f = open(result_file, "a")
result_file.write("This is Test " + testcase_name + " " + time.strftime("%c"))
print(testcase_name," is running!!!!")
runner = unittest.TextTestRunner(f)
unittest.main(testRunner=runner)
f.close()
print(testcase_name," is finished!!!!")

推荐答案

unittest.main 接受一个可选参数 exit.它的默认值是 True;导致 sys.exit 被调用.显式传递 False 以防止这种情况发生.

unittest.main accepts an optional parameter exit. The default value for it is True; cause sys.exit to be called. Explicitly pass False to prevent that.

...
unittest.main(testRunner=runner, exit=False)
...

这篇关于unittest.main() 后执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆