[TIP] Fwd: [Launchpad-dev] Reminder about a great Testtools matcher

Martin Pool mbp at sourcefrog.net
Tue Jul 19 18:08:43 PDT 2011

I thought this was cool enough it might be of interest to t-i-p.


---------- Forwarded message ----------
From: Julian Edwards <julian.edwards at canonical.com>
Date: 19 July 2011 22:11
Subject: [Launchpad-dev] Reminder about a great Testtools matcher
To: Launchpad Community Development Team <launchpad-dev at lists.launchpad.net>

I hope you all have the documents for Testtools[1] matchers on speed dial
because they are awesome.  I thought I'd remind you about the
matcher because I used it a while ago, forgot about it, and re-discovered it

Check out this diff snippet:

-        self.assertEqual(source_name, copy_job.package_name)
-        self.assertEqual(version, copy_job.package_version)
-        self.assertEqual(target_archive, copy_job.target_archive)
-        self.assertEqual(source_archive, copy_job.source_archive)
-        self.assertEqual(to_series, copy_job.target_distroseries)
-        self.assertEqual(to_pocket, copy_job.target_pocket)
-        self.assertFalse(copy_job.include_binaries)
-        self.assertEquals(PackageCopyPolicy.INSECURE, copy_job.copy_policy)
+        self.assertThat(copy_job, MatchesStructure(
+            package_name=Equals(source_name),
+            package_version=Equals(version),
+            target_archive=Equals(target_archive),
+            source_archive=Equals(source_archive),
+            target_distroseries=Equals(to_series),
+            target_pocket=Equals(to_pocket),
+            include_binaries=Equals(False),
+            copy_policy=Equals(PackageCopyPolicy.INSECURE)))

I replaced a load of serially-processed assertions with a single assertion.
This is good because:

 * It checks all of the conditions, not just up until the first one fails.
 * It prints out all the values if any of them fail to match.

I hope to see more use of this pattern!



Mailing list: https://launchpad.net/~launchpad-dev
Post to     : launchpad-dev at lists.launchpad.net
Unsubscribe : https://launchpad.net/~launchpad-dev
More help   : https://help.launchpad.net/ListHelp
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20110720/4f76d147/attachment.htm>

More information about the testing-in-python mailing list