I am troubleshooting an issue I'm having using Selenium and Python 3.5. I am trying to implement the unittest framework into my automation, but whenever I create a test suite the test opens multiple browser windows before actually executing a test case.
Through process of elimination, I've boiled it down to this portion of code:
def primary():
primary = unittest.TestSuite()
primary.addTest(unittest.makeSuite(PrimaryMethods, 'setUp'))
primary.addTest(unittest.makeSuite(PrimaryMethods, 'test_ModuleLoop_header'))
primary.addTest(unittest.makeSuite(PrimaryMethods, 'test_ModuleLoop_nonheader'))
primary.addTest(unittest.makeSuite(PrimaryMethods, 'tearDown'))
return primary
if __name__ == '__main__':
TEST_RUNNER = unittest.TextTestRunner()
TEST_SUITE = primary()
TEST_RUNNER.run(primary)
When I remove the section that creates the test suite and just use unittest.main() in the if statement, it runs fine with just one browser window. However, I know as my test cases become more complex I'll want to start using test suites. Does anyone have any idea what in there would make the setUp run 4 times by itself before moving on to the test cases?
Here is my setUp and tearDown sections:
def setUp(self):
driver = webdriver.Firefox
init () #for colorama
def tearDown(self):
driver = webdriver.Firefox
driver.close()
driver.quit()
I took out the code that was working and modified it back to include def primary() and the TEST_RUNNER.run(primary) code. I removed the init() call from the setUp section so that there is nothing in setUp other than loading the browser. When I place breakpoints throughout the script, this is the behavior I see:
- def setUp(self) immediately called at start
- def setUp(self) called again
- if name == 'main' and def primary() are called
- driver = webdriver.Firefox is called
- First browser opens
- driver = webdriver.Firefox is called a second time. The two test cases and tearDown were not touched.
- Second browser opens
- tearDown(self) is called.
- Second browser is closed
- driver = webdriver.Firefox is called a third time.
- Third browser opens
- tearDown(self) is called
- Third browser closes
- driver = webdriver.Firefox is called a fourth time.
- The first test case is finally started