unittest an under appreciated gem
play

unittest: An under-appreciated gem Andrew Bennetts 3rd December, - PowerPoint PPT Presentation

Introduction Why unittest is good Extending unittest Wrapping up unittest: An under-appreciated gem Andrew Bennetts 3rd December, 2008 Andrew Bennetts unittest: An under-appreciated gem Introduction What I hope you get out of this talk Why


  1. Introduction Why unittest is good Extending unittest Wrapping up unittest: An under-appreciated gem Andrew Bennetts 3rd December, 2008 Andrew Bennetts unittest: An under-appreciated gem

  2. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up What I hope you get out of this talk In no particular order. . . Andrew Bennetts unittest: An under-appreciated gem

  3. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up What I hope you get out of this talk In no particular order. . . ◮ Why I think Python’s unittest module is good Andrew Bennetts unittest: An under-appreciated gem

  4. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up What I hope you get out of this talk In no particular order. . . ◮ Why I think Python’s unittest module is good ◮ A new appreciation of what’s possible with Python’s unittest module Andrew Bennetts unittest: An under-appreciated gem

  5. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up What I hope you get out of this talk In no particular order. . . ◮ Why I think Python’s unittest module is good ◮ A new appreciation of what’s possible with Python’s unittest module ◮ Some general insights into good unit tests Andrew Bennetts unittest: An under-appreciated gem

  6. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up What I hope you get out of this talk In no particular order. . . ◮ Why I think Python’s unittest module is good ◮ A new appreciation of what’s possible with Python’s unittest module ◮ Some general insights into good unit tests Andrew Bennetts unittest: An under-appreciated gem

  7. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up Disclaimer I’m not doing a comparison with other test frameworks/tools (Nose, py.test, doctest, insert your favorite here , . . . ) Andrew Bennetts unittest: An under-appreciated gem

  8. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up What makes a good unit test suite? Andrew Bennetts unittest: An under-appreciated gem

  9. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up What makes a good unit test suite? Readable: intent of each test is clear, implementation is clear Reliable: only fails when it should, only passes when it should Usable: easy to run, fast to run, easy to debug (if necessary!) Andrew Bennetts unittest: An under-appreciated gem

  10. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up unittest basics Here’s a very quick overview of unittest. Andrew Bennetts unittest: An under-appreciated gem

  11. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up unittest basics — toy example class TestFrobnicator(unittest.TestCase): def setUp(self): self.frobnicator = Frobnicator() self.frobnicator.initialise() def test frob one word(self): input = ”word” output = self.frobnicator.frob(input) self.assertEqual(”frob”, output) def test frob two words(self): input = ”two words” output = self.frobnicator.frob(input) self.assertEqual(”frob frob”, output) Andrew Bennetts unittest: An under-appreciated gem

  12. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up unittest basics — toy example class TestFrobnicator(unittest.TestCase): def setUp(self): self.frobnicator = Frobnicator() self.frobnicator.initialise() def test frob one word(self): input = ”word” output = self.frobnicator.frob(input) self.assertEqual(”frob”, output) def test frob two words(self): input = ”two words” output = self.frobnicator.frob(input) self.assertEqual(”frob frob”, output) Andrew Bennetts unittest: An under-appreciated gem

  13. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up unittest basics — toy example class TestFrobnicator(unittest.TestCase): def setUp(self): self.frobnicator = Frobnicator() self.frobnicator.initialise() def test frob one word(self): input = ”word” output = self.frobnicator.frob(input) self.assertEqual(”frob”, output) def test frob two words(self): input = ”two words” output = self.frobnicator.frob(input) self.assertEqual(”frob frob”, output) Andrew Bennetts unittest: An under-appreciated gem

  14. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up unittest basics — toy example class TestFrobnicator(unittest.TestCase): def setUp(self): self.frobnicator = Frobnicator() self.frobnicator.initialise() def test frob one word(self): input = ”word” output = self.frobnicator.frob(input) self.assertEqual(”frob”, output) def test frob two words(self): input = ”two words” output = self.frobnicator.frob(input) self.assertEqual(”frob frob”, output) Andrew Bennetts unittest: An under-appreciated gem

  15. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up unittest basics — TestCase Key fact: A single unit test is represented by a TestCase instance. Andrew Bennetts unittest: An under-appreciated gem

  16. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up unittest basics — TestCase Key fact: A single unit test is represented by a TestCase instance. TestCase instances have a run method that will: ◮ run setUp ◮ run the test ◮ run tearDown ◮ report the outcome to a TestResult object They also provide assertion methods like assertEquals . Andrew Bennetts unittest: An under-appreciated gem

  17. Introduction What I hope you get out of this talk Why unittest is good What makes a good unit test suite? Extending unittest unittest basics Wrapping up unittest basics — runners, loaders, results Other major components: TestResult : object that can record details of a success or failure. TestLoader : turns test methods in TestCase subclasses into TestCase instances. TestRunner : glues everything together. Andrew Bennetts unittest: An under-appreciated gem

  18. Introduction It’s the Standard Why unittest is good Structure Extending unittest Extensibility Wrapping up Why unittest is good Andrew Bennetts unittest: An under-appreciated gem

  19. Introduction It’s the Standard Why unittest is good Structure Extending unittest Extensibility Wrapping up It’s the Standard Andrew Bennetts unittest: An under-appreciated gem

  20. Introduction It’s the Standard Why unittest is good Structure Extending unittest Extensibility Wrapping up It’s the Standard It’s in the standard library. ◮ It’s always available. ◮ Most other test frameworks interoperate with it. ◮ Practically every Python programmer is at least minimally familiar with it. Andrew Bennetts unittest: An under-appreciated gem

  21. Introduction It’s the Standard Why unittest is good Structure Extending unittest Extensibility Wrapping up It’s the Standard It’s an implementation of xUnit . ◮ Proven design. ◮ Practically every non-Python programmer is at least minimally familiar with it. Andrew Bennetts unittest: An under-appreciated gem

  22. Introduction It’s the Standard Why unittest is good Structure Extending unittest Extensibility Wrapping up Structure Andrew Bennetts unittest: An under-appreciated gem

  23. Introduction It’s the Standard Why unittest is good Structure Extending unittest Extensibility Wrapping up Structure — Isolation When run, each test has its own TestCase instance . So by default, tests are isolated from each other. Andrew Bennetts unittest: An under-appreciated gem

  24. Introduction It’s the Standard Why unittest is good Structure Extending unittest Extensibility Wrapping up Structure — Reusable fixture definition Each TestCase has a setUp and tearDown method. This makes it easy to reuse a test fixture definition between multiple tests. Andrew Bennetts unittest: An under-appreciated gem

  25. Introduction It’s the Standard Why unittest is good Structure Extending unittest Extensibility Wrapping up Structure — More code reuse TestCases naturally group tests with common needs. Andrew Bennetts unittest: An under-appreciated gem

Recommend


More recommend