0

I want run my tests in order of they are written not in alphabetical order that unittest does by default.

import unittest

class test2(unittest.TestCase):

   def test1(self):
      pass

   def test0(self):
      pass

class test1(unittest.TestCase):

   def testB(self):
      pass

   def testA(self):
      pass

In this example I want to set unittest or nosetests to run tests in order of test1, test0, testB and testA. When I run tests using command line with python -m unittest -v mytestmodule OR nosetests mytestmodule.

What command line argument should I use in order to do so?

Mahsa Mortazavi
  • 755
  • 3
  • 7
  • 23
  • 2
    Why do you want to do this? – Konstantin May 06 '15 at 17:42
  • There are some of the tests that need to run in order otherwise they will not make sense like Registration and Login, I want to Register first and then Login with the newly registered credential. @Alik – Mahsa Mortazavi May 06 '15 at 17:48
  • 2
    It is a bad practice when one test depends on another. – Konstantin May 06 '15 at 17:52
  • I know that, it does not happen on all of my tests. This is Regression test and I need it to be fast. Also, there is no harm in knowing how to change the order of tests. @Alik – Mahsa Mortazavi May 06 '15 at 17:58
  • 1
    @dbliss I already know about good and bad practices of writing tests, my question is something else and I need to know the answer to it. – Mahsa Mortazavi May 07 '15 at 00:48
  • 1
    Classes are glorified hash tables. Ordinarily, they don't have an order. Now, you can fake it using the [`__prepare__()`](https://docs.python.org/3/reference/datamodel.html#preparing-the-class-namespace) hook, but that's an awful lot of work to support something which isn't a best practice to begin with... so AFAIK unittest and friends don't actually do said work. – Kevin May 07 '15 at 01:37

2 Answers2

0

There is no such command-line argument. You have a couple of options:

1) Rename your tests. In your example, all you'd have to do is switch the names for test1 and test0. This suggestion has been made before.

2) Use the plug-in at this link.

Community
  • 1
  • 1
abcd
  • 10,215
  • 15
  • 51
  • 85
0

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

I ran tests using py.test MyTestModule.py -vv and the results were as follows and test were run in the order of their appearance:

MyTestModule.py::test2::test1 PASSED
MyTestModule.py::test2::test0 PASSED
MyTestModule.py::test1::testB PASSED
MyTestModule.py::test1::testA FAILED
Mahsa Mortazavi
  • 755
  • 3
  • 7
  • 23