I thought this was cool enough it might be of interest to t-i-p.<div><br clear="all">Martin<br><br>
<br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Julian Edwards</b> <span dir="ltr"><<a href="mailto:julian.edwards@canonical.com">julian.edwards@canonical.com</a>></span><br>
Date: 19 July 2011 22:11<br>Subject: [Launchpad-dev] Reminder about a great Testtools matcher<br>To: Launchpad Community Development Team <<a href="mailto:launchpad-dev@lists.launchpad.net">launchpad-dev@lists.launchpad.net</a>><br>
<br><br>I hope you all have the documents for Testtools[1] matchers on speed dial<br>
because they are awesome. I thought I'd remind you about the MatchesStructure<br>
matcher because I used it a while ago, forgot about it, and re-discovered it<br>
recently.<br>
<br>
Check out this diff snippet:<br>
<br>
- self.assertEqual(source_name, copy_job.package_name)<br>
- self.assertEqual(version, copy_job.package_version)<br>
- self.assertEqual(target_archive, copy_job.target_archive)<br>
- self.assertEqual(source_archive, copy_job.source_archive)<br>
- self.assertEqual(to_series, copy_job.target_distroseries)<br>
- self.assertEqual(to_pocket, copy_job.target_pocket)<br>
- self.assertFalse(copy_job.include_binaries)<br>
- self.assertEquals(PackageCopyPolicy.INSECURE, copy_job.copy_policy)<br>
+ self.assertThat(copy_job, MatchesStructure(<br>
+ package_name=Equals(source_name),<br>
+ package_version=Equals(version),<br>
+ target_archive=Equals(target_archive),<br>
+ source_archive=Equals(source_archive),<br>
+ target_distroseries=Equals(to_series),<br>
+ target_pocket=Equals(to_pocket),<br>
+ include_binaries=Equals(False),<br>
+ copy_policy=Equals(PackageCopyPolicy.INSECURE)))<br>
<br>
I replaced a load of serially-processed assertions with a single assertion.<br>
This is good because:<br>
<br>
* It checks all of the conditions, not just up until the first one fails.<br>
* It prints out all the values if any of them fail to match.<br>
<br>
I hope to see more use of this pattern!<br>
<br>
Cheers<br>
J.<br>
<br>
[1]<br>
<a href="http://readthedocs.org/docs/testtools/en/latest/for-test-authors.html#matchers" target="_blank">http://readthedocs.org/docs/testtools/en/latest/for-test-authors.html#matchers</a><br>
<br>
_______________________________________________<br>
Mailing list: <a href="https://launchpad.net/~launchpad-dev" target="_blank">https://launchpad.net/~launchpad-dev</a><br>
Post to : <a href="mailto:launchpad-dev@lists.launchpad.net">launchpad-dev@lists.launchpad.net</a><br>
Unsubscribe : <a href="https://launchpad.net/~launchpad-dev" target="_blank">https://launchpad.net/~launchpad-dev</a><br>
More help : <a href="https://help.launchpad.net/ListHelp" target="_blank">https://help.launchpad.net/ListHelp</a><br>
<br>
</div><br></div>