Automated testing is the best way to increase efficiency and decrease the defects of software testing. It takes away much of the effort on your part so that you can find bugs early and easily. The Python unit testing framework makes it easy to write efficient automated test cases in Python. Applications involving many paradigms and styles can be very complicated to test, but with the right tools, testing becomes the simplest part of the development process. This book starts with a short introduction to testing, and then introduces the doctest tool, both in terms of practicalities and how it fits into the testing environment. From there, the discussion proceeds to unittest.mock and mock objects, and to unittest. Next, Nose is introduced and discussed. Later on, focus turns from the tools themselves toward best practices and disciplines of testing. Finally, the integration of testing with existing build processes and toolchains is covered. By the end of this book, you will know how to use automated testing quickly and easily and in a way that helps catch bugs early and fix them. Cover Copyright Credits About the Author About the Reviewers www.PacktPub.com Table of Contents Preface Chapter 1: Python and Testing Testing for fun and profit Levels of testing Unit testing Integration testing System testing Acceptance testing Regression testing Test-driven development You'll need Python Summary Chapter 2: Working with doctest Where doctest performs best The doctest language Example – creating and running a simple doctest Result – three times three does not equal ten The syntax of doctests Example – a more complex test Result – five tests run? Expecting exceptions Example – checking for an exception Result – success at failing Expecting blank lines Controlling doctest behavior with directives Ignoring part of the result Example – ellipsis test drive Result – ellipsis elides Ignoring white space Example – invoking normality Result – white space matches any other white space Skipping an example Example – humans only Result – it looks like a test, but it's not The other directives The execution scope of doctest tests Check your understanding Exercise – English to doctest Embedding doctests into docstrings Example – a doctest in a docstring Result – the code is now self-documenting and self-testable Putting it into practice – an AVL tree English specification Node data Testing the constructor Recalculating height Making a node deletable Rotation Locating a node The rest of the specification Summary Chapter 3: Unit Testing with doctest What is unit testing? The limitations of unit testing Example – identifying units Choosing units Check your understanding Unit testing during the development process Design Development Feedback Development, again Later stages of the process Summary Chapter 4: Decoupling Units with unittest.mock Mock objects in general Mock objects according to unittest.mock Standard mock objects Non-mock attributes Non-mock return values and raising exceptions Mocking class or function details Mocking function or method side effects Mocking containers and objects with a special behavior Mock objects for properties and descriptors Mocking file objects Replacing real code with mock objects Mock objects in action Better PID tests Patching time.time Decoupling from the constructor Summary Chapter 5: Structured Testing with unittest The basics Assertions The assertTrue method The assertFalse method The assertEqual method The assertNotEqual method The assertAlmostEqual method The assertNotAlmostEqual method The assertIs and assertIsNot methods The assertIsNone and assertIsNotNone methods The assertIn and assertNotIn methods The assertIsInstance and assertNotIsInstance methods The assertRaises method The fail method Make sure you get it Test fixtures Example – testing database-backed units Summary Chapter 6: Running Your Tests with Nose Installing Nose Organizing tests An example of organizing tests Simplifying the Nose command line Customizing Nose's test search Check your understanding Practicing Nose Nose and doctest tests Nose and unittest tests Module fixture practice Package fixture practice Nose and ad hoc tests Summary Chapter 7: Test-driven Development Walk-through Writing the specification Try it for yourself – what are you going to do? Wrapping up the specification Writing initial unit tests Try it for yourself – write your early unit tests Wrapping up the initial unit tests Coding planner.data Using tests to get the code right Try it for yourself – writing and debugging code Writing the persistence tests Finishing up the personal planner Summary Chapter 8: Integration and System Testing Introduction to integration testing and system testing Deciding on an integration order Automating integration tests and system tests Writing integration tests for the time planner Check yourself – writing integration tests Summary Chapter 9: Other Tools and Techniques Code coverage Installing coverage.py Using coverage.py with Nose Version control integration Git Example test-runner hook Subversion Mercurial Bazaar Automated continuous integration Buildbot Setup Using Buildbot Summary Index
Automated testing is the best way to increase efficiency and decrease the defects of software testing. It takes away much of the effort on your part so that you can find bugs early and easily. The Python unit testing framework makes it easy to write efficient automated test cases in Python. Applications involving many paradigms and styles can be very complicated to test, but with the right tools, testing becomes the simplest part of the development process.
This book starts with a short introduction to testing, and then introduces the doctest tool, both in terms of practicalities and how it fits into the testing environment. From there, the discussion proceeds to unittest.mock and mock objects, and to unittest. Next, Nose is introduced and discussed. Later on, focus turns from the tools themselves toward best practices and disciplines of testing. Finally, the integration of testing with existing build processes and toolchains is covered. By the end of this book, you will know how to use automated testing quickly and easily and in a way that helps catch bugs early and fix them.
This book is ideal if you want to learn about the testing disciplines and automated testing tools from a hands-on, conversational guide. You should already know Python and be comfortable with Python 3.