0%

Book Description

A straightforward and easy approach to testing your Python projects

In Detail

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.

What You Will Learn

  • Make testing easy and convenient with powerful tools such as doctest, unittest, and Nose
  • Walk through the test-driven development process step by step
  • Translate specification documents into actionable tests
  • Write tests that highlight exactly where a bug resides
  • Build on the basis of unit testing to be able to test a complete software system
  • Use unit testing to drive development and discover how this can make the process faster

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Learning Python Testing
    1. Table of Contents
    2. Learning Python Testing
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Python and Testing
      1. Testing for fun and profit
      2. Levels of testing
        1. Unit testing
        2. Integration testing
        3. System testing
      3. Acceptance testing
      4. Regression testing
      5. Test-driven development
      6. You'll need Python
      7. Summary
    9. 2. Working with doctest
      1. Where doctest performs best
      2. The doctest language
        1. Example – creating and running a simple doctest
        2. Result – three times three does not equal ten
        3. The syntax of doctests
        4. Example – a more complex test
        5. Result – five tests run
      3. Expecting exceptions
        1. Example – checking for an exception
        2. Result – success at failing
      4. Expecting blank lines
      5. Controlling doctest behavior with directives
      6. Ignoring part of the result
        1. Example – ellipsis test drive
        2. Result – ellipsis elides
      7. Ignoring white space
        1. Example – invoking normality
        2. Result – white space matches any other white space
      8. Skipping an example
        1. Example – humans only
        2. Result – it looks like a test, but it's not
      9. The other directives
      10. The execution scope of doctest tests
      11. Check your understanding
      12. Exercise – English to doctest
      13. Embedding doctests into docstrings
        1. Example – a doctest in a docstring
        2. Result – the code is now self-documenting and self-testable
      14. Putting it into practice – an AVL tree
        1. English specification
        2. Node data
        3. Testing the constructor
        4. Recalculating height
        5. Making a node deletable
        6. Rotation
        7. Locating a node
        8. The rest of the specification
      15. Summary
    10. 3. Unit Testing with doctest
      1. What is unit testing?
      2. The limitations of unit testing
        1. Example – identifying units
        2. Choosing units
      3. Check your understanding
      4. Unit testing during the development process
        1. Design
        2. Development
        3. Feedback
        4. Development, again
        5. Later stages of the process
      5. Summary
    11. 4. Decoupling Units with unittest.mock
      1. Mock objects in general
      2. Mock objects according to unittest.mock
        1. Standard mock objects
          1. Non-mock attributes
          2. Non-mock return values and raising exceptions
          3. Mocking class or function details
          4. Mocking function or method side effects
        2. Mocking containers and objects with a special behavior
        3. Mock objects for properties and descriptors
        4. Mocking file objects
        5. Replacing real code with mock objects
      3. Mock objects in action
        1. Better PID tests
          1. Patching time.time
          2. Decoupling from the constructor
      4. Summary
    12. 5. Structured Testing with unittest
      1. The basics
      2. Assertions
        1. The assertTrue method
        2. The assertFalse method
        3. The assertEqual method
        4. The assertNotEqual method
        5. The assertAlmostEqual method
        6. The assertNotAlmostEqual method
        7. The assertIs and assertIsNot methods
        8. The assertIsNone and assertIsNotNone methods
        9. The assertIn and assertNotIn methods
        10. The assertIsInstance and assertNotIsInstance methods
        11. The assertRaises method
        12. The fail method
      3. Make sure you get it
      4. Test fixtures
        1. Example – testing database-backed units
      5. Summary
    13. 6. Running Your Tests with Nose
      1. Installing Nose
      2. Organizing tests
        1. An example of organizing tests
      3. Simplifying the Nose command line
      4. Customizing Nose's test search
        1. Check your understanding
        2. Practicing Nose
      5. Nose and doctest tests
      6. Nose and unittest tests
        1. Module fixture practice
        2. Package fixture practice
      7. Nose and ad hoc tests
      8. Summary
    14. 7. Test-driven Development Walk-through
      1. Writing the specification
        1. Try it for yourself – what are you going to do?
        2. Wrapping up the specification
      2. Writing initial unit tests
        1. Try it for yourself – write your early unit tests
        2. Wrapping up the initial unit tests
      3. Coding planner.data
      4. Using tests to get the code right
        1. Try it for yourself – writing and debugging code
      5. Writing the persistence tests
      6. Finishing up the personal planner
      7. Summary
    15. 8. Integration and System Testing
      1. Introduction to integration testing and system testing
        1. Deciding on an integration order
      2. Automating integration tests and system tests
        1. Writing integration tests for the time planner
      3. Check yourself – writing integration tests
      4. Summary
    16. 9. Other Tools and Techniques
      1. Code coverage
        1. Installing coverage.py
        2. Using coverage.py with Nose
      2. Version control integration
        1. Git
          1. Example test-runner hook
        2. Subversion
        3. Mercurial
        4. Bazaar
      3. Automated continuous integration
        1. Buildbot
          1. Setup
          2. Using Buildbot
      4. Summary
    17. Index
3.135.198.174