1

How can I create django unitest from multiple instances? I want to fetch all objects that was created in another unitest class. I doing this because class TestClassA and TestClassB also instanced from BaseTestAuth.

class BaseTestAuth(TestCase):

    def setUp(self):
        self.user_data = {
            'username': 'john',
            'email': 'john@mail.com',
            'password': 'password2521'
        }
        self.user = User.objects.create_user(**self.user_data)
        self.token, created = Token.objects.get_or_create(user=self.user)
        self.client = APIClient()
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)

    def tearDown(self):
        self.user


class TestClassA(BaseTestAuth):

    def setUp(self):
        super().setUp()
        self.foo = Foo.objects.first()


class TestClassB(BaseTestAuth):

    def setUp(self):
        super().setUp()
        self.bar = Bar.objects.first()


class TestCombine(TestClassA, TestClassB):

    def setUp(self):
        super().setUp()
        self.baz = Baz.objects.first()

    def test_post(self):

        # self.bar => doesn't exist
        # it replaced with `TestClassA.setUp`

        print(self.foo, self.bar, self.baz)

        self.client.post(...)
binpy
  • 3,994
  • 3
  • 17
  • 54

0 Answers0