If there are several methods in the test class, I found that the order to execute is alphabetical. But I want to customize the order of execution. How to define the execution order?
For example: testTestA will be loaded first than testTestB.
class Test(TestCase): def setUp(self): ... def testTestB(self): #test code def testTestA(self): #test code
To write a test you derive from any of the Django (or unittest) test base classes (SimpleTestCase, TransactionTestCase, TestCase, LiveServerTestCase) and then write separate methods to check that specific functionality works as expected (tests use "assert" methods to test that expressions result in True or False values ...
With Django's test-execution framework and assorted utilities, you can simulate requests, insert test data, inspect your application's output and generally verify your code is doing what it should be doing. The preferred way to write tests in Django is using the unittest module built-in to the Python standard library.
Django comes with a small set of its own tools for writing tests, notably a test client and four provided test case classes. These classes rely on Python's unittest module and TestCase base class. The Django test client can be used to act like a dummy web browser and check views.
A tenet of unit-testing is that each test should be independent of all others. If in your case the code in testTestA must come before testTestB, then you could combine both into one test:
def testTestA_and_TestB(self): # test code from testTestA ... # test code from testTestB
or, perhaps better would be
def TestA(self): # test code def TestB(self): # test code def test_A_then_B(self): self.TestA() self.TestB()
The Test
class only tests those methods who name begins with a lower-case test...
. So you can put in extra helper methods TestA
and TestB
which won't get run unless you explicitly call them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With