I am trying to create test cases which only differ by which user is logged in.
Therefore I figured defining the tests in a base class and creating sub classes which log in the user during setUp() would be the way to go.
However I cannot find an elegant solution to excluding the tests in the base class from running.
from django.contrib.auth.models import User
from django.test import TestCase
class A(TestCase):
@classmethod
def setUpTestData(cls):
cls.user_a = User.objects.create_user("a", "", "a")
cls.user_b = User.objects.create_user("b", "", "b")
def setUp(self):
# global set up stuff here
...
def test_1(self):
self.assertEqual(self.user_a.username, "a")
class SubA(A):
def setUp(self):
super().setUp()
self.client.force_login(self.user_a)
class SubB(A):
def setUp(self):
super().setUp()
self.client.force_login(self.user_b)
In the code above I'd want only the inherited tests in the classes SubA and SubB to run, but not the one in A.
I have tried:
- extracting the base class to another module, which is not included by the default search pattern. => still runs the tests since the base class must be imported to inherit from it
- defining a
load_tests(...)function in the package which excludes the base class => same result as above - raising
unittest.SkipTestin the base classsetUp()and catching it in the sub classes'setUp=> works but adds all tests in the base class as "skipped"
So my question is:
Is there a way to exclude the tests in a base class (which must inherit from Django's TestCase) from running without recording them as skipped and, preferably, without the need for additional parameters to my ./manage.py test command?