vendredi 6 novembre 2020

Django tests - a test in classA fail when i run classB (which is subclass of classA)

I have a test suite with multiple classes - ClassA and ClassB. ClassB is a subclass of classA and when I run classA tests, they all pass. However when i run ClassB, a classA test fails. I'm not sure how or why this is happening.

 class ClassA(TestCase):
      def test1(self):
            # run test

      def test2(self):
            # run test


class ClassB(ClassA):

      def test3(self):
            # run test

      def test4(self):
            # run test

python manage.py test app.tests.test_views.ClassA

all pass

python manage.py test app.tests.test_views.ClassB

FAIL: test1 (app.tests.test_views.ClassA)
Test 1
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/path/to/test_views.py", line 1435, in test1
    obj.evidence, "This is the evidence entered by user."
AssertionError: 'Generic evidence' != 'This is the evidence entered by user.'
    - Generic evidence
+ This is the evidence entered by user.

This is obviously a simplified example, and I know the best thing would be to not subclass ClassA in ClassB, however in my actual code there are ClassB tests that require some methods of ClassA. I understand this would be better handled with fixtures, but this is how it is set up at the moment and it would be a big revamp to rework this.

My understanding is that all ClassA tests are run when I execute ClassB test method, as it is subclassed:

python manage.py test app.tests.test_views.ClassA

runs test1 and test2

python manage.py test app.tests.test_views.ClassB

runs test1, test2, test3 and test4

However, I thought each time a test was run, the database was emptied and re-instated. Therefore why would test1 pass when running ClassA, but fail when ClassA tests are run as part of ClassB?

Aucun commentaire:

Enregistrer un commentaire