How to sort python unittest tests for detection?

I created my bunch of Python tests using the Python format unittest.

Now I can run them using

python -m unittest discover -s TestDirectory -p '*.py' -v

I find them all and run them.

Now there is a subtle difference if I run tests on Windows or Linux. Indeed, on Windows, tests are performed in alphabetical order, while on Linux, tests are performed without a visible human-specific detection order, even if they are always the same.

The problem is that I relied on the first two letters of the test file to sort the order in which the tests run. Not that they need to be run in a specific order, but to have some kind of information tests, showing the version data at its output, so that they appear first in the test run log.

Is there something I can do to run tests also in alphabetical order on Linux?

+5
source share
1 answer

I have not tried this, but I suppose that one thing you could do is to override the TestSuite class to add a sort function. You can then call the sort function before calling the unittest start function. So, in the "AllTests.py" script, you can add something like this:

class SortableSuite(unittest.TestSuite):
    def sort(self):
        self._tests.sort(cmp=lambda x,y: x._testMethodName < y._testMethodName)
    def run(self,testResult):
        #or if you don't want to run a sort() function, you can override the run
        #function to automatically sort.
        self._tests.sort(cmp=lambda x,y: x._testMethodName < y._testMethodName)
        return unittest.TestSuite.run(self,testResult)

loader = unittest.TestLoader()
loader.suiteClass = SortableSuite
suite = loader.loadTestFromTestCases(collectedTests)
suite.sort()
suite.run(defaultTestResult())
0
source

All Articles