Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django TestCase testing order

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 
like image 609
zs2020 Avatar asked Apr 05 '10 20:04

zs2020


People also ask

How do you write test cases in Django?

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 ...

How does Django test work?

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.

How do I test my Django site?

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.


1 Answers

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.

like image 123
unutbu Avatar answered Sep 21 '22 14:09

unutbu