Merge lp:~jtv/launchpad/importqueue-test-cleanup into lp:launchpad

Proposed by Jeroen T. Vermeulen on 2010-11-03
Status: Merged
Approved by: Robert Collins on 2010-11-03
Approved revision: no longer in the source branch.
Merged at revision: 11839
Proposed branch: lp:~jtv/launchpad/importqueue-test-cleanup
Merge into: lp:launchpad
Diff against target: 56 lines (+3/-18)
1 file modified
lib/lp/translations/tests/test_translationimportqueue.py (+3/-18)
To merge this branch: bzr merge lp:~jtv/launchpad/importqueue-test-cleanup
Reviewer Review Type Date Requested Status
Robert Collins (community) Approve on 2010-11-03
Steve Kowalik (community) code* Approve on 2010-11-03
Launchpad code reviewers code 2010-11-03 Pending
Review via email: mp+39931@code.launchpad.net

Commit Message

Translations test cleanup.

Description of the Change

= Translations Import Queue Test Cleanup =

This cleans up something I just found in a Translations test.

The test uses a popular and valid technique for building multiple variant of very similar test scenarios: it creates a base class containing the re-usable tests, and derives from that multiple test cases that define the different circumstances under which the same tests from the base class are to be run.

However it does this in an awkward way. The base class is derived from TestCaseWithFactory, so the test runner treats it as a test case and tries to run it. This is undesirable, and probably won't even work because the base class lacks some of the specifics needed to set up and run a useful test.

To work around that, the test defines a test_suite function (which we have otherwise more or less abolished) to define exactly which tests are to be run. As far as I can see, every test case that is added to the test will have to be added to test_suite in order for it to become part of the test suite.

There is a better pattern to deal with this particular problem. Because of Python's dynamic nature, the base class does not need to be derived from TestCase (or TestCaseWithFactory). In this branch I remove that derivation, treating the base test class as a mix-in instead and giving its children a TestCaseWithFactory as a second base class.

I verified manually (well, using diff) that this runs the exact same tests as before. Only the order is different. Running "wc -l" on the test output confirms this.

To test, try this before and after:
{{{
./bin/test -vv lp.translations.tests.test_translationimportqueue
}}}

The output shows the same tests passing, though not necessarily in the same order.

Jeroen

To post a comment you must log in.
Steve Kowalik (stevenk) :
review: Approve (code*)
Robert Collins (lifeless) wrote :

Hardly worth the review :)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/translations/tests/test_translationimportqueue.py'
2--- lib/lp/translations/tests/test_translationimportqueue.py 2010-10-29 05:58:51 +0000
3+++ lib/lp/translations/tests/test_translationimportqueue.py 2010-11-03 06:27:51 +0000
4@@ -3,8 +3,6 @@
5
6 __metaclass__ = type
7
8-import unittest
9-
10 import transaction
11 from zope.component import getUtility
12
13@@ -25,7 +23,7 @@
14 )
15
16
17-class TestCanSetStatusBase(TestCaseWithFactory):
18+class TestCanSetStatusBase:
19 """Base for tests that check that canSetStatus works ."""
20
21 layer = LaunchpadZopelessLayer
22@@ -166,7 +164,7 @@
23 [False, False, False, False, False, False, False])
24
25
26-class TestCanSetStatusPOTemplate(TestCanSetStatusBase):
27+class TestCanSetStatusPOTemplate(TestCanSetStatusBase, TestCaseWithFactory):
28 """Test canStatus applied to an entry with a POTemplate."""
29
30 def setUp(self):
31@@ -180,7 +178,7 @@
32 productseries=self.productseries, potemplate=self.potemplate)
33
34
35-class TestCanSetStatusPOFile(TestCanSetStatusBase):
36+class TestCanSetStatusPOFile(TestCanSetStatusBase, TestCaseWithFactory):
37 """Test canStatus applied to an entry with a POFile."""
38
39 def setUp(self):
40@@ -332,16 +330,3 @@
41 productseries=self.product.series[0])
42 self.product.owner = self.new_owner
43 self.assertEqual(self.old_owner, old_entry.importer)
44-
45-
46-def test_suite():
47- """Add only specific test cases and leave out the base case."""
48- suite = unittest.TestSuite()
49- suite.addTest(unittest.makeSuite(TestCanSetStatusPOTemplate))
50- suite.addTest(unittest.makeSuite(TestCanSetStatusPOFile))
51- suite.addTest(
52- unittest.makeSuite(TestCanSetStatusPOTemplateWithQueuedUser))
53- suite.addTest(unittest.makeSuite(TestCanSetStatusPOFileWithQueuedUser))
54- suite.addTest(unittest.makeSuite(TestGetGuessedPOFile))
55- suite.addTest(unittest.makeSuite(TestProductOwnerEntryImporter))
56- return suite