launchpad-dev team mailing list archive
-
launchpad-dev team
-
Mailing list archive
-
Message #07663
Reminder about a great Testtools matcher
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 MatchesStructure
matcher because I used it a while ago, forgot about it, and re-discovered it
recently.
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!
Cheers
J.
[1]
http://readthedocs.org/docs/testtools/en/latest/for-test-authors.html#matchers
Follow ups