4

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!

3
  • alphabetic ordering? C < F < L Commented May 30, 2012 at 14:37
  • @delnan how is it not a unit test? what am I doing wrong? Commented May 30, 2012 at 14:39
  • 1
    He's referring to the fact that it is not actually testing a unit of code; you are writing functional/integration tests. I think you are just confused by the fact that tools that do this leverage the unit test framework of Python. Commented May 30, 2012 at 14:41

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I ended up turning those 3 tests into Test classes where I can write more tests related to the class. Thanks also for your setUpClass() and tearDownClass(), I've used those as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.