Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of tests in python unittest

I was looking at similar questions and I couldn't find an answer to my problem.

I wrote Tests in a python class that derives from unittest.TestCase

class TestEffortFormula(unittest.TestCase)

I need to give an order to the tests (please, do not tell me that I shouldn't rely on test's order, I just do).

Before I needed to give order to the tests the command I used to run the tests was:

unittest.main(testRunner=TeamcityTestRunner())

Then I wanted to make the order dissappear, so I tried the following:

loader = unittest.TestLoader()
loader.sortTestMethodsUsing(None)
loader.loadTestsFromTestCase(TestEffortFormula)
suite = loader.suiteClass()

but from here I don't know how to run the tests, specially with testRunner=TeamcityTestRunner() as I did before.

Appreciate your help

like image 368
slashms Avatar asked May 17 '15 11:05

slashms


People also ask

Does Unittest run tests in order?

Note that the order in which the various test cases will be run is determined by sorting the test function names with respect to the built-in ordering for strings.

What is the correct order of the unit test layout?

A typical unit test contains 3 phases: First, it initializes a small piece of an application it wants to test (also known as the system under test, or SUT), then it applies some stimulus to the system under test (usually by calling a method on it), and finally, it observes the resulting behavior.

Do Python unit tests run in parallel?

unittest-parallel is a parallel unit test runner for Python with coverage support. By default, unittest-parallel runs unit tests on all CPU cores available. To run your unit tests with coverage, add either the "--coverage" option (for line coverage) or the "--coverage-branch" for line and branch coverage.


1 Answers

Option 1.

One solution to this (as a workaround) was given here - which suggests writing the tests in numbered methods step1, step2, etc., then collecting and storing them via dir(self) and yielding them to one test_ method which trys each.

Not ideal but does what you expect. Each test sequence has to be a single TestClass (or adapt the method given there to have more than one sequence generating method).

Option 2.

Another solution, also in the linked question, is you name your tests alphabetically+numerically sorted so that they will execute in that order.

But in both cases, write monolithic tests, each in their own Test Class.

P.S. I agree with all the comments that say unit testing shouldn't be done this way; but there are situations where unit test frameworks (like unittest and pytest) get used to do integration tests which need modular independent steps to be useful. Also, if QA can't influence Dev to write modular code, these kinds of things have to be done.

like image 170
aneroid Avatar answered Oct 04 '22 14:10

aneroid