0
votes

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:

  1. def setUp(self) immediately called at start
  2. def setUp(self) called again
  3. if name == 'main' and def primary() are called
  4. driver = webdriver.Firefox is called
  5. First browser opens
  6. driver = webdriver.Firefox is called a second time. The two test cases and tearDown were not touched.
  7. Second browser opens
  8. tearDown(self) is called.
  9. Second browser is closed
  10. driver = webdriver.Firefox is called a third time.
  11. Third browser opens
  12. tearDown(self) is called
  13. Third browser closes
  14. driver = webdriver.Firefox is called a fourth time.
  15. The first test case is finally started
1
post the rest of the code, or at least enough to provide relevant context. you mention the problem is "launching browsers", where is that code? - Corey Goldberg
I added my setUp to the main question. - tinneko
i suggest posting a full example of runnable code that shows the problem, rather than bits and pieces of it - Corey Goldberg

1 Answers

0
votes

I think I have found a solution. I removed the lines in def primary() where setUp and tearDown are added to the test suite and now it runs without opening the browser repeatedly. I'm guessing it was trying to run setUp and tearDown as their own test cases, but also still calling setUp and tearDown in the testsuite.

I also modified the test suite creation code to better fit what the Python documentation suggested. Based on the documentation I've seen, it looks like Makesuite should be used when gathering test cases across test case classes. Apparently it will still run without giving any errors however.

All of that leaves me with this:

def primary():
    primary = unittest.TestSuite()
    primary.addTest(PrimaryMethods('test_ModuleLoop_header'))
    primary.addTest(PrimaryMethods ('test_ModuleLoop_nonheader'))
    return primary