Skip to content

Commit

Permalink
Convert setUpClass to setUpTestData and drop tearDownClass()
Browse files Browse the repository at this point in the history
for all test cases which use the database. This speeds up things
a bit and is more compatible with how Django is supposed to handle
tests that need objects from the database.

Fix #161
  • Loading branch information
atodorov committed Mar 8, 2017
1 parent c696e62 commit ddbcf2e
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 525 deletions.
45 changes: 8 additions & 37 deletions tcms/testcases/tests.py
Expand Up @@ -6,7 +6,6 @@

from fields import MultipleEmailField
from tcms.testcases.models import TestCaseBugSystem
from tcms.testcases.models import TestCasePlan
from tcms.tests import BasePlanCase
from tcms.tests.factories import ComponentFactory
from tcms.tests.factories import TestBuildFactory
Expand Down Expand Up @@ -69,22 +68,15 @@ class TestCaseRemoveBug(BasePlanCase):
"""Test TestCase.remove_bug"""

@classmethod
def setUpClass(cls):
super(TestCaseRemoveBug, cls).setUpClass()
def setUpTestData(cls):
super(TestCaseRemoveBug, cls).setUpTestData()
cls.build = TestBuildFactory(product=cls.product)
cls.test_run = TestRunFactory(product_version=cls.version, plan=cls.plan,
manager=cls.tester, default_tester=cls.tester)
cls.case_run = TestCaseRunFactory(assignee=cls.tester, tested_by=cls.tester,
case=cls.case, run=cls.test_run, build=cls.build)
cls.bug_system = TestCaseBugSystem.objects.get(name='Bugzilla')

@classmethod
def tearDownClass(cls):
cls.case_run.delete()
cls.test_run.delete()
cls.build.delete()
super(TestCaseRemoveBug, cls).tearDownClass()

def setUp(self):
self.bug_id_1 = '12345678'
self.case.add_bug(self.bug_id_1, self.bug_system.pk,
Expand Down Expand Up @@ -142,8 +134,8 @@ class TestCaseRemoveComponent(BasePlanCase):
"""Test TestCase.remove_component"""

@classmethod
def setUpClass(cls):
super(TestCaseRemoveComponent, cls).setUpClass()
def setUpTestData(cls):
super(TestCaseRemoveComponent, cls).setUpTestData()

cls.component_1 = ComponentFactory(name='Application',
product=cls.product,
Expand All @@ -157,14 +149,6 @@ def setUpClass(cls):
cls.cc_rel_1 = TestCaseComponentFactory(case=cls.case, component=cls.component_1)
cls.cc_rel_2 = TestCaseComponentFactory(case=cls.case, component=cls.component_2)

@classmethod
def tearDownClass(cls):
cls.cc_rel_1.delete()
cls.cc_rel_2.delete()
cls.component_1.delete()
cls.component_2.delete()
super(TestCaseRemoveComponent, cls).tearDownClass()

def test_remove_a_component(self):
self.case.remove_component(self.component_1)

Expand All @@ -181,18 +165,12 @@ class TestCaseRemovePlan(BasePlanCase):
"""Test TestCase.remove_plan"""

@classmethod
def setUpClass(cls):
super(TestCaseRemovePlan, cls).setUpClass()
def setUpTestData(cls):
super(TestCaseRemovePlan, cls).setUpTestData()

cls.new_case = TestCaseFactory(author=cls.tester, default_tester=None, reviewer=cls.tester,
plan=[cls.plan])

@classmethod
def tearDownClass(cls):
TestCasePlan.objects.filter(plan=cls.plan, case=cls.new_case).delete()
cls.new_case.delete()
super(TestCaseRemovePlan, cls).tearDownClass()

def test_remove_plan(self):
self.new_case.remove_plan(self.plan)

Expand All @@ -206,21 +184,14 @@ class TestCaseRemoveTag(BasePlanCase):
"""Test TestCase.remove_tag"""

@classmethod
def setUpClass(cls):
super(TestCaseRemoveTag, cls).setUpClass()
def setUpTestData(cls):
super(TestCaseRemoveTag, cls).setUpTestData()

cls.tag_rhel = TestTagFactory(name='rhel')
cls.tag_fedora = TestTagFactory(name='fedora')
TestCaseTagFactory(case=cls.case, tag=cls.tag_rhel, user=cls.tester.pk)
TestCaseTagFactory(case=cls.case, tag=cls.tag_fedora, user=cls.tester.pk)

@classmethod
def tearDownClass(cls):
cls.case.tag.clear()
cls.tag_rhel.delete()
cls.tag_fedora.delete()
super(TestCaseRemoveTag, cls).tearDownClass()

def test_remove_tag(self):
self.case.remove_tag(self.tag_rhel)

Expand Down
11 changes: 1 addition & 10 deletions tcms/testplans/tests.py
Expand Up @@ -22,7 +22,7 @@
class PlanTests(test.TestCase):

@classmethod
def setUpClass(cls):
def setUpTestData(cls):
cls.user = UserFactory(username='admin', email='admin@example.com')
cls.user.set_password('admin')
cls.user.is_superuser = True
Expand All @@ -44,15 +44,6 @@ def setUpClass(cls):
type=cls.plan_type)
cls.plan_id = cls.test_plan.pk

@classmethod
def tearDownClass(cls):
cls.c.logout()
cls.test_plan.delete()
cls.product_version.delete()
cls.product.delete()
cls.classification.delete()
cls.user.delete()

def test_open_plans_search(self):
location = reverse('tcms.testplans.views.all')
response = self.c.get(location)
Expand Down
17 changes: 3 additions & 14 deletions tcms/testruns/tests.py
Expand Up @@ -20,8 +20,8 @@ class TestOrderCases(BasePlanCase):
"""Test view method order_case"""

@classmethod
def setUpClass(cls):
super(TestOrderCases, cls).setUpClass()
def setUpTestData(cls):
super(TestOrderCases, cls).setUpTestData()
cls.build = TestBuildFactory(product=cls.product)
cls.test_run = TestRunFactory(product_version=cls.version, plan=cls.plan,
manager=cls.tester, default_tester=cls.tester)
Expand All @@ -34,18 +34,7 @@ def setUpClass(cls):
cls.case_run_3 = TestCaseRunFactory(assignee=cls.tester, tested_by=cls.tester,
run=cls.test_run, case=cls.case_3, build=cls.build,
sortkey=300)

@classmethod
def tearDownClass(cls):
cls.case_run_1.delete()
cls.case_run_2.delete()
cls.case_run_3.delete()
cls.test_run.delete()
cls.build.delete()
super(TestOrderCases, cls).tearDownClass()

def setUp(self):
self.client = test.Client()
cls.client = test.Client()

def test_404_if_run_does_not_exist(self):
nonexisting_run_pk = TestRun.objects.count() + 1
Expand Down
19 changes: 1 addition & 18 deletions tcms/tests/__init__.py
Expand Up @@ -13,9 +13,7 @@ class BasePlanCase(test.TestCase):
"""Base test case by providing essential Plan and Case objects used in tests"""

@classmethod
def setUpClass(cls):
super(BasePlanCase, cls).setUpClass()

def setUpTestData(cls):
cls.product = ProductFactory(name='Nitrate')
cls.version = VersionFactory(value='0.1', product=cls.product)
cls.tester = UserFactory()
Expand All @@ -29,18 +27,3 @@ def setUpClass(cls):
plan=[cls.plan])
cls.case_3 = TestCaseFactory(author=cls.tester, default_tester=None, reviewer=cls.tester,
plan=[cls.plan])

@classmethod
def tearDownClass(cls):
cls.case.plan.clear()
cls.case.delete()
cls.case_1.delete()
cls.case_2.delete()
cls.case_3.delete()
cls.plan.delete()
cls.plan.type.delete()
cls.tester.delete()
cls.version.delete()
cls.product.delete()
cls.product.classification.delete()
super(BasePlanCase, cls).tearDownClass()

0 comments on commit ddbcf2e

Please sign in to comment.