You are previewing Python Testing Beginner's Guide.
O'Reilly logo
Python Testing Beginner's Guide

Book Description

An easy and convenient approach to testing your powerful Python projects

  • Covers everything you need to test your code in Python

  • Easiest and enjoyable approach to learn Python testing

  • Write, execute, and understand the result of tests in the unit test framework

  • Packed with step-by-step examples and clear explanations

  • In Detail

    Automated testing moves much of the labor of testing off the developer and makes it easier as well as quicker to find bugs and fix them. Automated tests run faster, increase test coverage, and lower costs. However, testing is neither an easy process nor remotely exciting for most developers. But with the right techniques and tools, testing can become a simple and gratifying part of the development process.

    With this helpful guide – from an expert – in your hand, testing will not be a tiresome topic for you anymore. You will learn how to test your Python projects in the easiest way, making other parts of the development process easier and more enjoyable.

    This book covers the tools and techniques of automated testing and test-driven development. Starting from the very basics, the chapters introduce new tools and techniques in simple, accessible language with step-by-step examples. You will explore how to make testing easier and more accurate with Python's doctest module and learn test-driven development using the unittest framework. You will also learn how to keep your units separate from each other and discover a simple and easy way to integrate Mocker and unittest. Next, we cover integration testing and web application testing.

    Automated testing gives developers better feedback, faster and more often. Bugs get found sooner and fixed better, with less effort. By the end of this book, you will have all of the skills needed to benefit from automated testing.

    Test-driven development of Python software—make development easier using some of the best testing tools out there

    Table of Contents

    1. Python Testing
      1. Python Testing
      2. Credits
      3. About the Author
      4. About the Reviewers
      5. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Time for action - heading
          1. What just happened?
          2. Pop quiz - heading
          3. Have a go hero - heading
        6. Reader feedback
        7. Customer support
          1. Errata
          2. Piracy
          3. Questions
      6. 1. Testing for Fun and Profit
        1. How can testing help?
        2. Types of testing
          1. Unit testing
          2. Integration testing
          3. System testing
        3. You've got Python, right?
        4. Summary
      7. 2. Doctest: The Easiest Testing Tool
        1. Basic doctest
        2. Time for action - creating and running your first doctest
          1. What just happened?
          2. The syntax of doctests
        3. Time for action - writing a more complex test
          1. What just happened?
          2. Expecting exceptions
        4. Time for action - expecting an exception
          1. What just happened?
          2. Expecting blank lines in the output
          3. Using directives to control doctest
            1. Ignoring part of the result
        5. Time for action - using ellipsis in tests
          1. What just happened?
            1. Ignoring whitespace
        6. Time for action - normalizing whitespace
            1. Skipping an example entirely
        7. Time for action skipping tests
          1. What just happened?
            1. Other doctest directives
          2. Execution scope
          3. Pop quiz - doctest syntax
          4. Have a go hero - from English to doctest
        8. Embedding doctests in Python docstrings
        9. Time for action - embedding a doctest in a docstring
          1. What just happened?
          2. Doctest directives
          3. Execution scope
        10. Putting it in practice: an AVL tree
          1. English specification
          2. Node data
          3. Constructor
          4. Recalculate height
          5. Make deletable
          6. Rotation
          7. Locating a node
          8. Testing the rest of the specification
        11. Summary
      8. 3. Unit Testing with Doctest
        1. What is Unit testing and what it is not?
        2. Time for action - identifying units
          1. What just happened?
          2. Pop quiz - understanding units
        3. Unit testing throughout the development process
          1. Design phase
        4. Time for action - unit testing during design
          1. What just happened?
          2. Pop quiz - unit testing during design
          3. Have a go hero
          4. Development phase
        5. Time for action - unit testing during development
          1. What just happened?
          2. Feedback phase
        6. Time for action - unit testing during feedback
          1. What just happened?
          2. Back to the development phase
        7. Time for action - unit testing during development... again
          1. What just happened?
          2. Maintenance phase
        8. Time for action - unit testing during maintenance
          1. What just happened?
          2. Reuse phase
        9. Time for action - unit testing during reuse
          1. What just happened?
          2. Pop quiz - unit testing
          3. Have a go hero - test-driven development
        10. Summary
      9. 4. Breaking Tight Coupling by using Mock Objects
        1. Installing Python Mocker
        2. Time for action - installing Python Mocker
        3. The idea of a mock object
        4. Python Mocker
        5. Time for action - exploring the basics of Mocker
          1. What just happened?
          2. Mocking functions
          3. Mocking containers
          4. Parameter matching
            1. ANY
            2. ARGS
            3. KWARGS
            4. IS
            5. IN
            6. CONTAINS
            7. MATCH
          5. Mocking complex expressions
          6. Have a go hero
          7. Returning iterators
          8. Raising exceptions
          9. Calling functions via a mock
          10. Specifying that an expectation should occur multiple times
          11. Replacing library objects with mocks
          12. Pop quiz - Mocker usage
          13. Have a go hero - mocking datetime
        6. Mocking self
        7. Time for action - passing a mock object as self
          1. What just happened?
        8. Summary
      10. 5. When Doctest isn't Enough: Unittest to the Rescue
        1. Basic unittest
        2. Time for action - testing PID with unittest
          1. What just happened?
          2. Assertions
            1. assertTrue
            2. assertFalse
            3. assertEqual
            4. assertNotEqual
            5. assertAlmostEqual
            6. assertNotAlmostEqual
            7. assertRaises
            8. fail
          3. Pop quiz - basic unittest knowledge
          4. Have a go hero - translating into unittest
        3. Test fixtures
        4. Time for action - testing database-backed units
          1. What just happened?
          2. Pop quiz - test fixtures
          3. Have a go hero - file path abstraction
        5. Integrating with Python Mocker
        6. Summary
      11. 6. Running Your Tests: Follow Your Nose
        1. What is Nose?
        2. Installing Nose
        3. Organizing tests
        4. Time for action - organizing tests from previous chapters
          1. What just happened?
          2. Finding doctests
          3. Customizing Nose's search
          4. Pop quiz - testing with Nose
          5. Have a go hero - nosing around
        5. Nose and doctest
        6. Time for action - creating a fixture for a doctest
        7. Nose and unittest
        8. Time for action - creating a module fixture
          1. What just happened?
        9. Time for action - creating a package fixture
          1. What just happened?
        10. Nose's own testing framework
        11. Time for action - using Nose-specific tests
          1. What just happened?
        12. Summary
      12. 7. Developing a Test-Driven Project
        1. Writing the specification
        2. Time for action - what are you going to do?
          1. What just happened?
        3. Writing initial unit tests
        4. Time for action - nailing down the specification with unit tests
          1. What just happened?
        5. Coding planner.data
        6. Using the tests to get the code right
        7. Fixing the code
        8. Time for action - writing and debugging code
          1. What just happened?
        9. Writing persistence tests
        10. Writing persistence code
        11. Finishing up
          1. Pop quiz - test-driven development
          2. Have a go hero
        12. Summary
      13. 8. Testing Web Application Frontends using Twill
        1. Installing Twill
        2. Exploring the Twill language
        3. Time for action - browsing the web with Twill
          1. What just happened?
        4. Time for action - Twill scripting
          1. What just happened?
          2. Twill commands
            1. help
            2. setglobal
            3. setlocal
            4. add_auth
            5. add_extra_header
            6. clear_extra_headers
            7. show_extra_headers
            8. agent
            9. back
            10. clear_cookies
            11. code
            12. config
            13. debug
            14. echo
            15. exit
            16. extend_with
            17. find
            18. notfind
            19. follow
            20. formaction
            21. formclear
            22. formfile
            23. formvalue
            24. getinput
            25. getpassword
            26. go
            27. info
            28. save_cookies
            29. load_cookies
            30. show_cookies
            31. redirect_error
            32. redirect_output
            33. reset_error
            34. reset_output
            35. reload
            36. reset_browser
            37. run
            38. runfile
            39. save_html
            40. show
            41. showforms
            42. showhistory
            43. showlinks
            44. sleep
            45. submit
            46. tidy_ok
            47. title
            48. url
          3. Pop quiz - the Twill language
          4. Have a go hero - browsing the web with Twill
        5. Calling Twill scripts from tests
        6. Time for action - running Twill script files
          1. What just happened?
        7. Time for action - running Twill script strings
          1. What just happened?
          2. A nifty trick
        8. Integrating Twill operations into unittest tests
        9. Time for action - using Twill's browser object
          1. What just happened?
          2. Browser methods
            1. get_code
            2. get_html
            3. get_title
            4. get_url
            5. find_link
            6. follow_link
            7. get_all_forms
            8. get_form
            9. get_form_field
            10. clicked
            11. submit
          3. Pop quiz - browser methods
        10. Summary
      14. 9. Integration Testing and System Testing
        1. Integration tests and system tests
        2. Time for action - figuring out the order of integration
          1. What just happened?
          2. Pop quiz - diagramming integration
          3. Have a go hero - diagram your own program
        3. Automation with doctest, unittest, and Nose
        4. Time for action - writing integration tests for the time planner
          1. What just happened?
          2. Pop quiz - writing integration tests
          3. Have a go hero - integrating your own program
        5. Summary
      15. 10. Other Testing Tools and Techniques
        1. Code coverage
          1. coverage.py
        2. Time for action - using coverage.py
          1. What just happened?
          2. Pop quiz - code coverage
          3. Have a go hero - checking coverage in earlier chapters
        3. Version control hooks
          1. Bazaar
        4. Time for action - installing Nose as a Bazaar post-commit hook
          1. What just happened?
          2. Mercurial
        5. Time for action - installing Nose as a Mercurial post-commit hook
          1. What just happened?
          2. Git
        6. Time for action - installing Nose as a Git post-commit hook
          1. What just happened?
          2. Darcs
        7. Time for action - installing Nose as a Darcs post-record hook
          1. What just happened?
          2. Subversion
        8. Time for action - installing Nose as a Subversion post-commit hook
          1. What just happened?
          2. Pop quiz - version control hooks
        9. Automated continuous integration
          1. Buildbot
        10. Time for action - using Buildbot with Bazaar
          1. What just happened?
          2. Pop quiz Buildbot
          3. Have a go hero
        11. Summary
      16. A. Answers to Pop Quizes
        1. Chapter 2
          1. Pop quiz - doctest syntax
        2. Chapter 3
          1. Pop quiz - understanding units
          2. Pop quiz - unit testing during design
          3. Pop quiz - unit testing
        3. Chapter 4
          1. Pop quiz - Mocker usage
        4. Chapter 5
          1. Pop quiz - basic unittest knowledge
          2. Pop quiz - text fixtures
        5. Chapter 6
          1. Pop quiz - testing with Nose
        6. Chapter 7
          1. Pop quiz - test-driven development
        7. Chapter 8
          1. Pop quiz - the Twill language
          2. Pop quiz - browser methods
        8. Chapter 9
          1. Pop quiz - diagramming integration
          2. Pop quiz - writing integration tests
        9. Chapter 10
          1. Pop quiz - code coverage
          2. Pop quiz - version control hooks