How can I order tests?

When I create my tests via appium the order of my tests is not work correctly.
In my example:

import unittest


class TestSum(unittest.TestCase):

    def test_remove(self):
        print("1")

    def test_add(self):
        print("2")


if __name__ == '__main__':
    loader = unittest.TestLoader()
    loader.sortTestMethodsUsing = None
    unittest.main(testLoader=loader)

Test test_remove need to be the first test and not a second.
I try to cancel the alphabet sort of unittest (use sortTestMethodsUsing ) but I still have the problem to order my test.

@dron4ik85 appium cant control order of test. It is problem of framework you are using to run appium test. In your case it is https://stackoverflow.com/questions/4095319/unittest-tests-order

The problem is that sortTestMethodsUsing not working for me…:frowning:

not much you can do. unit tests where not supposed to work in any order. some workarounds mentioned here -> https://stackoverflow.com/questions/30286268/order-of-tests-in-python-unittest

@Aleksei I found some solution here:


The problem is that I need to run 2 functions that can help me with my problem in another file. I asked from the user that create this solution but the problem is that he still not answer to me. Maybe you know how can I do that?
Thank you!!!

in python unittest framework, according to it’s help doc,you can see sentences like this:
Note:
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.

this means you can start your testcase name with nums or letters to help you control which runs first. just like this:
def test_1_casename:
pass
def test_2_casename:
pass

@johny the problem is that I have many tests and I need every time add or remove some test case and then I need to update the index of all tests

sorry, i don’t understand why you have to update the whole index when you change you test case.
if you add a case, plus one num or choose a bigger letter according to your prior case name ,
if you remove one, the former case name always smaller than the latter ,it won’t make any influence, so , what’s the problem.

@johny I found the solution to my problem:

import unittest

unittest.TestLoader.sortTestMethodsUsing = lambda self, a, b: (a < b) - (a > b)


class TestStringMethods(unittest.TestCase):

    def test_remove(self):
        print("1")

    def test_add(self):
        print("2")


if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestStringMethods)
    unittest.TextTestRunner(verbosity=2).run(suite)