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)
...