Python Testing Cookbook

Python Testing Cookbook

Now pay Easier and Secure using Paypal

Read more

Over 70 simple but incredibly effective recipes for taking control of automated testing using powerful Python testing tools

Greg L. Turnquist

e-books shop
e-books shop
Purchase Now !
Just with Paypal

Book Details
 362 p
 File Size 
 8,659 KB
 File Type
 PDF format
 2011 Packt Publishing 

About the Author
Greg L. Turnquist has worked in the software industry since 1997. He is an active
participant in the open source community, and has contributed patches to several projects
including MythTV, Spring Security, MediaWiki, and the TestNG Eclipse plugin. As a test-bitten
script junky, he has always sought the right tool for the job. He is a firm believer in agile
practices and automated testing. He has developed distributed systems, LAMP-based setups,
and supported mission-critical systems hosted on various platforms.
After graduating from Auburn University with a Master's in Computer Engineering, Greg
started working with Harris Corporation. He worked on many contracts utilizing many types
of technology. In 2006, he created the Spring Python project and went on to write Spring
Python 1.1 in 2010. He joined SpringSource, a division of VMware in 2010, as part of their
international software development team.

About the Reviewers
Matthew Closson is a creative technologist and entrepreneur at heart. He is currently
employed as a software engineer by Philips Healthcare. He is passionate about software
testing, systems integration, and web technologies. When not obsessing over Ruby and C#
code, this elusive developer is likely to be found reading at the local bookstore or relaxing on the beach.

Chetan Giridhar has more than five years experience of working in the software services
industry, product companies, and research organizations. He has a string background of
C/C++, Java (certified Java professional) and has a good command of Perl, Python scripting
languages, using which he has developed useful tools and automation frameworks. His
articles on Code Reviews, Software Automation, and Agile methodologies have been
published in international magazines including TestingExperience and AgileRecord for which
he has received appreciation from other industry experts on his website—TechnoBeans.
Chetan has also co-authored a book on Design Patterns in Python that is listed at Python's
Official Website. He has given lectures on Python Programming to software professionals
and at educational institutes including the Indian Institute of Astrophysics, Bangalore. Chetan
holds a B.E. in Electrical Engineering from the University of Mumbai and feels that the world is
full of knowledge.

Sylvain Hellegouarch is a senior software engineer with several years experience in
development and performance testing in various companies, both in France and in the United
Kingdom. Passionate about open-source software, he has written several Python projects
around communication protocols such as HTTP, XMPP, and the Atom Publishing Protocol. He
has been part of the CherryPy team since 2004 and has also authored the CherryPy Essentials
book, published by Packt Publishing in 2007. Sylvain also reviewed Spring Python, published
by Packt Publishing in 2010. His current interests are set on the open-data movement and the
wave of innovation it brings to public services. When away from his computer, Sylvain plays the
guitar and the drums or spends his time with friends and family.

Maurice HT Ling completed his Ph.D. in Bioinformatics and B.Sc(Hons) in Molecular and
Cell Biology from The University of Melbourne where he worked on microarray analysis and
text mining for protein-protein interactions. He is currently a Senior Scientist (Bioinformatics)
in Life Technologies and an Honorary Fellow in The University of Melbourne, Australia.
Maurice holds several Chief Editorships including The Python Papers, Computational and
Mathematical Biology, and Methods and Cases in Computational, Mathematical, and
Statistical Biology. In Singapore, he co-founded the Python User Group (Singapore) and has
been the co-chair of PyCon Asia-Pacific since 2010. In his free time, Maurice likes to train in
the gym, read, and enjoy a good cup of coffee. He is also a Senior Fellow of the International
Fitness Association, USA. His personal website is: http://maurice.vodien.com.

Testing has always been a part of software development. For decades, comprehensive testing
was defined by complex manual test procedures backed by big budgets; but something
revolutionary happened in 1998. In his Guide to Better Smalltalk, Smalltalk guru Kent Beck
introduced an automated test framework called SUnit. This triggered an avalanche of test
frameworks including JUnit, PyUnit, and many others for different languages and various
platforms, dubbed the xUnit movement. Automated testing was made a cornerstone
of the agile movement when 17 top software experts signed the Agile Manifesto in 2001.
Testing includes many different styles including unit testing, integration testing, acceptance
testing, smoke testing, load testing, and countless others. This book digs in and explores
testing at all the important levels while using the nimble power of Python. It also shows many tools.

This book is meant to expand your knowledge of testing from something you either heard
about or have practiced a little into something you can apply at any level to meet your needs
in improving software quality. I hope to give you the tools to reap huge rewards in better
software development and customer satisfaction.

Table of Contents
Preface 1
Chapter 1: Using Unittest To Develop Basic Tests 5
Introduction 5
Asserting the basics 7
Setting up and tearing down a test harness 11
Running test cases from the command line with increased verbosity 14
Running a subset of test case methods 16
Chaining together a suite of tests 18
Defining test suites inside the test module 21
Retooling old test code to run inside unittest 25
Breaking down obscure tests into simple ones 29
Testing the edges 35
Testing corner cases by iteration 39
Chapter 2: Running Automated Test Suites with Nose 45
Introduction 45
Getting nosy with testing 46
Embedding nose inside Python 49
Writing a nose extension to pick tests based on regular expressions 52
Writing a nose extension to generate a CSV report 59
Writing a project-level script that lets you run different test suites 66
Chapter 3: Creating Testable Documentation with doctest 77
Introduction 77
Documenting the basics 78
Catching stack traces 82
Running doctests from the command line 85
Coding a test harness for doctest 88
Filtering out test noise 92
Printing out all your documentation including a status report 96
Testing the edges 101
Testing corner cases by iteration 104
Getting nosy with doctest 107
Updating the project-level script to run this chapter's doctests 110
Chapter 4: Testing Customer Stories with Behavior
Driven Development 117
Introduction 117
Naming tests that sound like sentences and stories 120
Testing separate doctest documents 126
Writing a testable story with doctest 130
Writing a testable novel with doctest 136
Writing a testable story with Voidspace 142
Mock and nose 142
Writing a testable story with mockito and nose 147
Writing a testable story with Lettuce 150
Using Should DSL to write succinct assertions with Lettuce 158
Updating the project-level script to run this chapter's BDD tests 163
Chapter 5: High Level Customer Scenarios with Acceptance Testing 169
Introduction 170
Installing Pyccuracy 172
Testing the basics with Pyccuracy 176
Using Pyccuracy to verify web app security 179
Installing the Robot Framework 183
Creating a data-driven test suite with Robot 186
Writing a testable story with Robot 191
Tagging Robot tests and running a subset 197
Testing web basics with Robot 204
Using Robot to verify web app security 208
Creating a project-level script to verify this chapter's acceptance tests 212
Chapter 6: Integrating Automated Tests with Continuous Integration 217
Introduction 217
Generating a continuous integration report for Jenkins using NoseXUnit 220
Configuring Jenkins to run Python tests upon commit 222
Configuring Jenkins to run Python tests when scheduled 227
Generating a CI report for TeamCity using teamcity-nose 231
Configuring TeamCity to run Python tests upon commit 234
Configuring TeamCity to run Python tests when scheduled 237
Chapter 7: Measuring your Success with Test Coverage 241
Introduction 241
Building a network management application 243
Installing and running coverage on your test suite 251
Generating an HTML report using coverage 255
Generating an XML report using coverage 257
Getting nosy with coverage 259
Filtering out test noise from coverage 261
Letting Jenkins get nosy with coverage 264
Updating the project-level script to provide coverage reports 269
Chapter 8: Smoke/Load Testing—Testing Major Parts 275
Introduction 275
Defining a subset of test cases using import statements 277
Leaving out integration tests 281
Targeting end-to-end scenarios 285
Targeting the test server 290
Coding a data simulator 298
Recording and playing back live data in real time 303
Recording and playing back live data as fast as possible 311
Automating your management demo 319
Chapter 9: Good Test Habits for New and Legacy Systems 323
Introduction 324
Something is better than nothing 324
Coverage isn't everything 326
Be willing to invest in test fixtures 328
If you aren't convinced on the value of testing, your team
won't be either 330
Harvesting metrics 331
Capturing a bug in an automated test 332
Separating algorithms from concurrency 333
Pause to refactor when test suite takes too long to run 334
Cash in on your confidence 336
Be willing to throw away an entire day of changes 337
Instead of shooting for 100 percent coverage, try to have a steady growth 339
Randomly breaking your app can lead to better code 340
Index 343

e-books shop

What you need for this book
You will need Python 2.6 or above. The recipes in this book have NOT been tested against
Python 3+. This book uses many other Python test tools, but includes detailed steps to show
how to install and use them.
Who this book is for
This book is for Python developers who want to take testing to the next level. It covers different
styles of testing, giving any developer an expanded set of testing skills to help write better
systems. It also captures lessons learned from the author, explaining not only how to write
better tests but why.