S.Muse
S.Muse

Reputation: 115

How to write functional/integration tests in Selenium Python

I'm new to testing and I would like to

1) test the login

2) create a folder

3) add content (a page) into the folder

I have each of the tests written and they work but obviously I would like to build ontop of each other, eg, in order to do 3 I need to do 1 then 2. In order to do 2 I need to do 1. This is my basic test structure:

class TestSelenium(unittest.TestCase):
    def setUp(self):
        # Create a new instance of the Firefox driver
        self.driver = webdriver.Firefox()

    def testLogin(self):
        print '1'
        ...

    def testFolderCreation(self):
        print '2'
        ...

    def testContentCreation(self):
        print '3'
        ...

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

At first, I thought the tests would run in order and the 2nd function would continue off where the first one left off, but I've found this is not the case, it seems to be starting over with each test. I've also realized that they execute in reverse order. I get an output of 3,2,1 in the terminal. How should I achieve what I want? If I call the previous functions before I run the one I want, I feel like it's repetitively testing the same thing over and over since each one is a test (eg, in testContentCreation, I would call 'testLogin' then call testFolderCreation and inside testFolderCreation call testLogin. If I were to do more, the testLogin would've been called a number of times!). Should I instead turn the previous steps into regular non-test functions and in the final last one (the test function) call the previous ones in order? If I do it that way then I guess if any of the steps fail, the last one fails, there would be one big test function.

Any suggestions on how you should write this type of test? Also, why are the tests running in reverse order?

Thanks!

Upvotes: 4

Views: 1648

Answers (1)

Silas Ray
Silas Ray

Reputation: 26160

You are seeing what you are seeing, I think, because you are making some incorrect assumptions about the assumptions unittest makes. Each test case is assumed to be a self-contained entity, so there is no run order imposed. In addition, SetUp() and TearDown() operate before and after each individual case. If you want global setup/teardown, you need to make classmethods named SetUpClass() and TearDownClass(). You may also want to look in to the TestSuite class. More here: http://docs.python.org/library/unittest.html

Keep in mind that when the unittest library does test discovery (reflects your testcase class to find the test cases to run), it is essentially limited to looking at the .__dict__ and dir() values for the object, which are inherently unordered.

Upvotes: 3

Related Questions