Python Testing: Beginner's Guide


Python Testing: Beginner's Guide
eBook: $23.99
Formats: PDF, PacktLib, ePub and Mobi formats
$20.39
save 15%!
Print + free eBook + free PacktLib access to the book: $63.98    Print cover: $39.99
$39.99
save 37%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Overview
Table of Contents
Author
Reviews
Support
Sample Chapters
  • 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

Book Details

Language : English
Paperback : 256 pages [ 235mm x 191mm ]
Release Date : January 2010
ISBN : 1847198848
ISBN 13 : 9781847198846
Author(s) : Daniel Arbuckle
Topics and Technologies : All Books, App Testing, Application Development, Beginner's Guides, Open Source, Python


Table of Contents

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

Daniel Arbuckle

Daniel Arbuckle holds a Ph.D. in Computer Science from the University of Southern California. While at USC, he performed original research in the Interaction Lab (part of the Center for Robotics and Embedded Systems) and the Laboratory for Molecular Robotics (now part of the Nanotechnology Research Laboratory). His work has been published in peer-reviewed journals and in the proceedings of international conferences.

Code Downloads

Download the code and support files for this book.


Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


Errata

- 16 submitted: last submission 24 Jan 2014

Errata type: Language | Page number: 38

"..,we want that failure to tell where the problem is us as specifically as possible." should be "...,we want that failure to tell where the problem is as specifically as possible."

 

Errata type: Language | Page number: 9

"...something is a unit if it there's no..." should be "...something is a unit if there's no..."

 

Errata type: Typo | Page number: 21

"This is usefull if you..." should be "This is useful if you..."

 

Errata type: Code | Page number: 28

"...descendants, such that the the tree..." should be "...descendants, such that the tree..."

 

Errata type: Code | Page number: 45

"self.integrated_error +q= err * delta" should be "self.integrated_error += err * delta"

 

Errata type: Technical | Page number: 62

"...you should be ready to use Nose." should be "...you should be ready to use Python Mocker."

 

Errata type: Code | Page number: 103

test_module.1.py test_module.1.py module1.txt module1.txt should be test_module.1.py test_module.2.py module1.txt module2.txt

 

Errata type: Others | Page number: 126

TThe line ending with "2008-04-27T00:" contains crossed box characters instead of whitespaces.

 

Errata type: Others | Page number: 129

The line ending with "2008-04-27T00" contains crossed box characters instead of whitespaces.

 

Errata type: Others | Page number: 131

The line ending with "2008-04-27T00:00" contains crossed box characters instead of whitespaces.

 

Errata type: Others | Page number: 141

The line ending with "contained.exclude(task):" contains crossed box characters instead of whitespaces.

 

Errata type: Others | Page number: 142

The line ending with "contained.exclude(task)" contains crossed box characters instead of whitespaces.

 

Errata type: Typo | Page number: 114 | Errata date: 21 July 11

"That's two underbars, the word 'init, and two more". should be "That's two underbars, the word 'init', and two more" A quote (') is missing after init

 

Errata type: Typo | Page number: 32

"leaf descendants, such that the the tree"
Should be: "leaf descendants, such that the tree"

 

Errata type: Code | Page number: 18

In the section titled Time for action – using ellipsis in tests, the code:

>>> import datetime
>>> datetime.datetime.now().isoformat() # doctest: +ELLIPSIS
'...-...-...T...:...:...'

Should be:

>>> import datetime
>>> datetime.datetime.now().isoformat() # doctest: +ELLIPSIS
'...-...-...T...:...:...'

 

Errata type: Code

Page No. 17, Time for action, Step 1
Insert the following text into your doctest file: Next up, we're exploring the ellipsis. >>> sys.modules # doctest: +ELLIPSIS
It should be
Insert the following text into your doctest file: Next up, we're exploring the ellipsis. >>> import sys >>> sys.modules # doctest: +ELLIPSIS

Sample chapters

You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

Frequently bought together

Python Testing: Beginner's Guide +    Microsoft Exchange 2010 PowerShell Cookbook =
50% Off
the second eBook
Price for both: £27.05

Buy both these recommended eBooks together and get 50% off the cheapest eBook.

What you will learn from this book

  • Make testing easy and convenient with Python's doctest module, which lets you put your tests right into your doc strings
  • Drive development using unit testing and learn how it can make the process quicker
  • Explore the assorted features Mocker provides to help you keep units separate from each other and customize it to suit your needs
  • Embed your tests in a controlled and temporary environment using test fixtures
  • Simplify the integration of unittest and Mocker using Python Mocker's MockerTestCase
  • Automatically find and execute tests using Nose – a unittest-based testing framework for Python that makes writing and running tests easier
  • Write tests for web applications using Twill and integrate it's capabilities as a library into Python testing code
  • Create integration tests and run them to see if your programs work correctly in concert with each other
  • Run Nose automatically when your version control software detects changes in the source code
  • Set up the Buildbot automated continuous integration system to run your tests repeatedly over many different environments


Chapter 1
introduces Python test-driven development and various testing methods.

Chapter 2 covers the doctest tool and teaches you how to use it.

Chapter 3 introduces the ideas of unit testing and test-driven development, and applies doctest to create unit tests.

Chapter 4 covers mock objects and the Python Mocker tool.

Chapter 5 introduces the unittest framework and discusses when it is preferred over doctest.

Chapter 6 introduces the Nose test runner, and discusses project organization.

Chapter 7 walks through a complete test-driven development process.

Chapter 8 applies the knowledge gained from previous chapters to web applications, and introduces the Twill tool.

Chapter 9 teaches how to build up from unit tests to tests of a complete software system.

Chapter 10 introduces code coverage and continuous integration, and teaches how to tie automated testing into version control systems.

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

Approach

The book begins with the very foundations of automated testing, and expands on them until the best-practice tools and techniques are fully covered. New concepts are illustrated with step-by-step hands-on exercises. Testing will be easier and more enjoyable with this beginner's guide.

Who this book is for

If you are a Python developer and want to write tests for your applications, this book will get you started and show you the easiest way to learn testing.

You need to have sound Python programming knowledge to follow along. An awareness of software testing would be good, but no formal knowledge of testing is expected nor do you need to have any knowledge of the libraries discussed in the book.

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software