1

Is there any way I can execute the tests in the order in which they were written?

What happens in PyUnit is whenever I run tests it run in alphabetical order. This means even if I have written TestA after TestB, TestA will run before TestA. Which is creating problems for me.

import unittest

class SimpleTestCase(unittest.TestCase):

    def testB(self):
        print "Test B"

    def testA(self):
        print "Test A"

I want testB to execute before testA.

Georgy
  • 12,464
  • 7
  • 65
  • 73
Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137
  • And btw, you should use search feature more extensively in the future, because the question has been asked numerous times at SO. http://stackoverflow.com/questions/3843171/unit-testing-with-dependencies-between-tests http://stackoverflow.com/questions/4095319/unittest-tests-order – Michal Jul 09 '13 at 09:21

3 Answers3

1

I found a solution for it using PyTest ordering plugin provided here.

Try py.test YourModuleName.py -vv in CLI and the test will run in the order they have appeared in your module (first testB and then testA)

I did the same thing and works fine for me.

Note: You need to install PyTest package and import it.

Mahsa Mortazavi
  • 755
  • 3
  • 7
  • 23
0

If these are unit tests then they should be completely isolated, so you should check for design flaws in those tests.

If you really need for some reason, to use a specific order, than you have three ways of achieving this in python:

  1. Using unittest - change sorting method, described here.
  2. Using Proboscis - use the following decorator @test(depends_on=[list of dependecies]), found here.
  3. Using nose - nose executes its unit tests in the order in which they appear in the module file. More info here.
Community
  • 1
  • 1
Michal
  • 6,411
  • 6
  • 32
  • 45
  • Hi Guys, I gone through the Answers And this is what i got. 1. use unittest.TestLoader.sortTestMethodsUsing = lambda _, x, y: cmp(y, x) or 2. Give the name such a way it gets executed in order. However I want don't want to change the name and I am not able to find any function for first solution though which i can execute the order in which it has been written. Any solution ??? – Gaurang Shah Jul 09 '13 at 10:55
  • @GaurangShah And why can't you use the other two solutions? – Michal Jul 09 '13 at 17:08
-1

PyUnit uses TestLoaded which collects all testcases in a suite and runs them in alphabetical order

For example Test A runs before TEST B

In case you want to run TEST B first we have to create function and add Test B and then Test A

def suite():
    suite = unittest.TestSuite()
    suite.addTest(SimpleTestCase('test_B'))
    suite.addTest(SimpleTestCase('test_A'))
    return suite      
brennan
  • 3,392
  • 24
  • 42
  • Whenever you link to a blog post, library, or other external resource you're involved in, [you must clearly indicate this](https://stackoverflow.com/help/promotion). – Nathan Tuggy Apr 08 '17 at 04:38