我正在从另一个 Python 脚本调用以下脚本:
test.py logfile
它应该运行测试并将结果保存在日志文件中。
但是由于某种原因,unittest.main(testRunner=runner)
之后的命令没有被执行。
我什至不确定文件在写入后是否关闭。
还有其他编写脚本的方法吗?!
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(testRunner=runner, exit=False)
...
关于python - 在 unittest.main() 之后执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26539316/