8

I need to set an order of execution for my tests, because I need some data verified before the others. Is possible to set an order?

class OneTestCase(unittest.TestCase):
    def setUp(self):
        # something to do
    def test_login (self):
        # first test
        pass
    def test_other (self):
        # any order after test_login
    def test_othermore (self):
        # any order after test_login
if __name__ == '__main__':
    unittest.main()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Carlos
  • 4,299
  • 5
  • 22
  • 34
  • 4
    unit test means tests are unitary. they are not supposed to depend on one another. – njzk2 May 03 '13 at 17:24
  • You are right, but then, could be the best way to do a test when I need to have this kind of behavior, could be better if I call login each time to do a test? It is for a server which need to start a login before do anything else, and the other methods read information based on login – Carlos May 03 '13 at 17:28
  • 3
    If you're actually interacting with a real server over the wire, you're not doing *unit* testing. –  May 03 '13 at 17:31
  • 8
    Unitary is nice in philosophy, but quite unhandy in practice sometimes. Imagine a long setup, loading files from disk, network connections ... You don't want your test cases to take an eternity. The slower they are, the less you'll run them, so the less useful they are. – Michael May 03 '13 at 17:31
  • 2
    http://stackoverflow.com/questions/8389639/unittest-setup-teardown-for-several-tests – Moj May 03 '13 at 17:32
  • 1
    Delnan, I am sure of that, but I need to know a way to test that, at least the best way to test that, when a method depend on other, the what could be the better? please – Carlos May 03 '13 at 17:34
  • 1
    Michael, great, you near what I am looking for, then my question will be, what will be the best way on doing test for this kind of behaviors. Unit testing would be wrong? – Carlos May 03 '13 at 17:35
  • No, why should it be wrong? It's still true, that it's best to test code in small bits. Nevertheless, there are reasons to test it in bigger chunks, may it be a lack of time to write more detailed code, or may it be, to test the overall behavior of something, that can't be tested in parts. See below my suggestion for this. – Michael May 03 '13 at 17:54
  • I mean "Would be wrong that I am using Unit Testing for this kind of test?" – Carlos May 03 '13 at 17:59
  • 4
    Well, it's still the best option to do it. You just shouldn't be too dogmatic about it. I have a lot of unit tests for lengthy mathematical computations, without saving temporary results and setup code in `setUpClass` for a whole set of tests, it would be painfully slow. So as long as your tests proof, what you want to proof, who cares about dogma? – Michael May 03 '13 at 18:04
  • 1
    Better try without sorting, if there is really no way around, I would also accept it. – User May 03 '13 at 18:10
  • " without saving temporary results and setup code in " Yes, in my case I need to save some infor temporary, I have some ideas to work on it – Carlos May 03 '13 at 18:20
  • Does this answer your question? [Python unittest.TestCase execution order](https://stackoverflow.com/questions/5387299/python-unittest-testcase-execution-order) – ggorlen Jun 18 '20 at 05:33

2 Answers2

29

You can do it like this:

class OneTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # something to do
        pass

    def test_01_login (self):
        # first test
        pass
    def test_02_other (self):
        # any order after test_login
    def test_03_othermore (self):
        # any order after test_login

if __name__ == '__main__':
    unittest.main(failfast=True, exit=False)

Tests are sorted alphabetically, so just add numbers to get your desired order. Probably you also want to set failfast = True for the testrunner, so it fails instantly, as soon as the first test fails.

Michael
  • 7,316
  • 1
  • 37
  • 63
  • 3
    Where are they sorted? Can one rely on that? – User May 03 '13 at 17:30
  • 1
    @User See Python docs: "Note that the order in which the various test cases will be run is determined by sorting the test function names with respect to the built-in ordering for strings." – mloskot Nov 15 '15 at 17:11
1

Better do not do it.

Tests should be independent.

To do what you want best would be to put the code into functions that are called by the test.

Like that:

def assert_can_log_in(self):
    ...

def test_1(self):
    self.assert_can_log_in()
    ...

def test_2(self):
    self.assert_can_log_in()
    ...

Or even to split the test class and put the assertions into the setUp function.

class LoggedInTests(unittest.TestCase):
    def setUp(self):
        # test for login or not - your decision

    def test_1(self):
        ...

When I split the class I often write more and better tests because the tests are split up and I can see better through all the cases that should be tested.

User
  • 14,131
  • 2
  • 40
  • 59
  • also login must be tested, that could be ok? – Carlos May 03 '13 at 17:30
  • Sorry, I do not understand. – User May 03 '13 at 17:31
  • 9
    I read lengthy discussions about how to write tests properly. Fact is, some tests depend on some certain state, which e.g. can be created through a previous test, which tests the creation of that state (e.g. being logged in). Creating this state may be a time consuming procedure, so reusing it and chaining tests may be worth it, to keep the duration of your tests short. Else you would end up testing the loging procedure 100 times, just to cover the tests, that you do with the logged in state. So, being too philosophical about test separation does not make sense. – Michael Jan 22 '16 at 09:25