Reputation: 412
I am calling the following script from another Python script:
test.py logfile
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!!!!")
Upvotes: 4
Views: 966
Reputation: 369074
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)
...
Upvotes: 6