0

I am working on writing unit tests, but they needed to run as I added them to the TestSuite. So my problem is when I run the testrunner.py from the PyCharm I get what I need, but if I run the same file from the terminal the tests will be sorted alphabetically, and that is bad for me.

For example: I have 2 tests, test_dummy_01.py and test_dummy_01 in the "tests/" folder of my Framework. Also I created the testrunner.py file where I add the TestDummy02 class first and after the TestDummy01 class. So when I run the the testrunner.py from PyCharm the test_dummy_02.py test will run first and the test_dummy_01.py by second which is good for me. Now I need the same behavior when I run the same file in the terminal, but it will always run the test_dummy_01.py file first. So please if anyone can help me?

Folder hierarchy of my files:

Project/
       tests/
             test_dummy_01.py 
             test_dummy_02.py
       testrunner.py

The test_dummy_01.py test: - the test_dummy_02.py file is same as this

class TestDummy01(BaseTestCase):

    def setUp(self):
        super(TestDummy, self).setUp()

    def test_dummy_01(self):
        driver = self.driver
        login = LoginPage(driver)
        login.enter_username("Something")

    def tearDown(self):
        super(TestDummy, self).tearDown()

The testrunner.py file where I added the order

import sys
import unittest
from tests.test_dummy_01 import TestDummy01
from tests.test_dummy_02 import TestDummy02


def suite():
    suite_x = unittest.TestSuite()
    suite_x.addTest(unittest.makeSuite(TestDummy02)) # In PyCharm this test is runned first, but in Teminal this will run by second
    suite_x.addTest(unittest.makeSuite(TestDummy01))

    return suite_x


def run():
    result = unittest.TextTestRunner(verbosity=2).run(suite())
    if not result.wasSuccessful():
        sys.exit(1)


if __name__ == '__main__':
    run()

PyCharm terminal

Edvin
  • 1
  • Why do you need to run unit tests in order? – Melon Feb 25 '20 at 09:45
  • 1
    It is almost never a good idea to rely on test order. I usually would encourage to run tests in random order to find any unwanted dependencies. You probably want to remove these dependencies. – MrBean Bremen Feb 25 '20 at 09:46
  • And if you _really_ want to have that order, you could just name the tests alphabetically. – MrBean Bremen Feb 25 '20 at 09:48
  • Yes, that is correct, that usually it is not good idea to rely on test order, but I have now one situation where I need this, and if I name the tests alphabetically then maintaining these will not be possible, e.g. if new feature is added and it needs to be checked between to tests. So the TestSuite can be easily edited. – Edvin Feb 25 '20 at 12:33
  • From the [docs](https://docs.python.org/3/library/unittest.html): The order in which the various tests will be run is determined by sorting the test method names with respect to the built-in ordering for strings. I think if you want to change this, you have to write your own testrunner. – MrBean Bremen Feb 25 '20 at 13:20
  • Actually there is already [a similar question](https://stackoverflow.com/questions/5387299/python-unittest-testcase-execution-order) with helpful responses, so this one is probably a duplicate. Please check the answers there! – MrBean Bremen Feb 25 '20 at 13:27
  • Thanks, MrBean Bremen for your time, I already saw that question, and if you notice, I make my Suite exactly as in one of that answers. Now this is work in PyCharm, but unfortunately, not in terminal. An yes maybe I will must to write my own testrunner. – Edvin Feb 26 '20 at 07:30

0 Answers0