Python Testing with unittest, nose, pytest



Similar documents
Survey of Unit-Testing Frameworks. by John Szakmeister and Tim Woods

Python as a Testing Tool. Chris Withers

CIS 192: Lecture 13 Scientific Computing and Unit Testing

N Ways To Be A Better Developer

Test Driven Development in Python

This book is for sale at This version was published on

The Jumplead Manual. Setting Up and Using Marketing Automation. Matt Fenn. This book is for sale at

Advanced Topics: Biopython

The Little Real-time Web Development Book

SuiteCRM for Developers

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Building Phone Applications

Self-review 9.3 What is PyUnit? PyUnit is the unit testing framework that comes as standard issue with the Python system.

Kotlin for Android Developers

Python programming Testing

Write a Web Application for Free Edition 2

Simple Search Engine Optimization Tips

Beginning Web Development with Node.js

Build it with Drupal 8

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

CME 193: Introduction to Scientific Python Lecture 8: Unit testing, more modules, wrap up

Automated Testing with Python

Testing Python. Applying Unit Testing, TDD, BDD and Acceptance Testing

Using New Relic to Monitor Your Servers

Python for Test Automation i. Python for Test Automation

The Social Accelerator Setup Guide

WRS CLIENT CASE STUDIES

Writing robust scientific code with testing (and Python) Pietro Berkes, Enthought UK


EXPRESSING LIKES, DISLIKES AND PREFERENCES DIALOGUE SCRIPT AND GLOSSARY

Fifty Enterprise Architect Tricks

Seven Reasons Why You Should Switch to Cloud Computing

The 10 Keys to Simple, Strategic Compensation Planning How To Get the Right Compensation Management Solution by asking the Right 10 Questions

Wrestling with Python Unit testing. Warren Viant

Lifebushido/Best Agent Business

Usability Test Results

Rapid Game Development Using Cocos2D-JS

Professional Plone 4 Development

CREATIVE CODING FOR KIDS

Colleen s Interview With Ivan Kolev

Entrepreneur Systems: Business Systems Development Tool

Python and Google App Engine

Python Loops and String Manipulation

Book 3 Cost Estimating in an Agile Development Environment. (early release)

GETTING STARTED CREATIVE ENTREPRENEUR VERSION A PINES UP NORTH WORKBOOK

Lay Betting Selection System. Strike Rate!!

To the point. a recipe for creating lean products. Paulo Caroli. This book is for sale at

Testing Mobile Apps CS 4720 Web & Mobile Systems

MEAP Edition Manning Early Access Program Hello! ios Development version 14

Club Accounts Question 6.

Break It Before You Buy It!

Unit testing with mock code EuroPython 2004 Stefan Schwarzer p.1/25

About the Free Report: DSI Media Three basic selling website styles: Direct Sales Educational or Informational + Sales: (Point of Presence (POP

Software Testing with Python

Your Complete Social Intranet Buyer s Guide & Handbook

React+d3.js. Build data visualizations with React and d3.js. Swizec Teller. This book is for sale at

5544 = = = Now we have to find a divisor of 693. We can try 3, and 693 = 3 231,and we keep dividing by 3 to get: 1

Profiling, debugging and testing with Python. Jonathan Bollback, Georg Rieckh and Jose Guzman

How the Internet has Impacted Marketing?

Scale Factors and Volume. Discovering the effect on the volume of a prism when its dimensions are multiplied by a scale factor

Getting Started with Dynamic Web Sites

How to get MOSS 2007 dev. environment set up in Vista with sample project.

9 Principles of Killer Dashboards SELL. SERVICE. MARKET. SUCCEED.

LIBERTY NATIONAL LIFE LAPTOP PRESENTATION SCRIPTS

Marketing casebook: Start spreading the news

Copyright 2012 DMT Solutions, Inc.

Agile Planet. A Travel Guide to the Agile Universe Fabian Schiller. This book is for sale at

Hello, my name is Jessica and I work in Human Resources for Target Corporation.

Title: SharePoint Advanced Training

SpiraTest / SpiraTeam Automated Unit Testing Integration & User Guide Inflectra Corporation

30- Day List Building Plan for a Software as a Service Business

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Exercise 1: Python Language Basics

Brought to you by:

Hydraulic Steering Install

Finding Rates and the Geometric Mean

5IMPROVE OUTBOUND WAYS TO SALES PERFORMANCE: Best practices to increase your pipeline

INinbox Start-up Pack

How to Charge for Access to your Membership Site.

Sending Blasts in NationBuilder

How to Make a Working Contact Form for your Website in Dreamweaver CS3

T he complete guide to SaaS metrics

New Tools for Testing Web Applications with Python

What do I need to complete a college application? (And why should I care about this if I m not planning on going to college?

Early Stage Funding. Dragon Law. This book is for sale at This version was published on

The Smart Business Owners Guide To Mobile Responsive Design

What does student success mean to you?

Using SQL Monitor at Interactive Intelligence


Using Logistics to Grow Your Business

How to Build an Enterprise App in 5 Days 1

A Simple Guide to Churn Analysis

Agile.NET Development Test-driven Development using NUnit

5 Websites Where You Can Make Money Right Now

Hands-On Lab. Embracing Continuous Delivery with Release Management for Visual Studio Lab version: Last updated: 12/11/2013

Software Life Cycle. Management of what to do in what order

Troubleshoot Using Event Log Mining

PhoneGap Build Starter

Transcription:

Python Testing with unittest, nose, pytest Efficient and effective testing using the 3 top python testing frameworks Brian Okken This book is for sale at http://leanpub.com/pythontesting This version was published on 2014-09-19 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. 2014 Brian Okken

Contents Introduction............................................ i unittest.............................................. 1 unittest introduction....................................... 2 Full Table Of Contents...................................... 7

Introduction PythonTesting.net¹ is a blog where I write about software testing. The most popular posts so far are the tutorials on how to get started using the 3 most popular testing frameworks: unittest nose pytest I ve organized a bunch of the most poplar posts into a sequence that I think makes sense, and pulled them into this book format. I did this becuase I personally would have liked this stuff in an ebook format when I started learning about testing with Python. So, I hope it s helpful for you. How I got started writing about this stuff. I ve been a professional software developer since 1996. I ve been using Python for about the last 10 years. Mostly for testing. One day I needed a test framework to run a bunch of pre-existing test scripts that weren t set up to use any franework. Because of Pythons introspection facilities, it seemed like it would be pretty easy to implement. I knew somewhat about unittest. But I really didn t want to derive classes just to get test functions running. The first incantation was pretty simple, and was implemented quickly. However, the more we used it, the more new features we needed. So it grew. Finally, one day, I looked on the web to see what test frameworks were available. I found out more about unittest. And I found nose. And then pytest. But the documentation (for all of them) was confusing. At least to me. I decided to find out for myself, and explore all three. ¹http://pythontesting.net

Introduction ii At first I thought I would discover that one was far superior to all the rest. But I don t think that s the case. I do think pytest is by far the coolest and most advanced. However, unittest keeps improving, and is no slouch. And although nose isn t really in development as far as I can tell, lots of people still use it successfully. Along the way I ve learned lots about all three, and also some on doctest. That s where pythontesting.net² comes in. I am trying to share all that I know (and can clearly speak about) regarding these frameworks. This book is a fund raiser The proceeds from this book do a few things. 1. Encourage me to keep writing content on pythontesting.net³. 2. Help offset the cost of putting this content and more into a professionally published book on the subject. See next section. 3. Help pay off the laptop I just bought so I can write more. 4. Let my family know that this is a good thing for me to be spending my time on. ²http://pythontesting.net ³http://pythontesting.net

unittest

unittest introduction The unittest test framework is python s xunit style framework. It is a standard module that you already have if you ve got python version 2.1 or greater. In this post, I ll cover the basics of how to create and run a simple test using unittest. Then I ll show how I m using it to test markdown.py. Overview of unittest The unittest module used to be called PyUnit, due to it s legacy as a xunit style framework. It works much the same as the other styles of xunit, and if you re familiar with unit testing in other languages, this framework (or derived versions), may be the most comfortable for you. The standard work flow is: 1. You define your own class derived from unittest.testcase. 2. Then you fill it with functions that start with test_. 3. You run the tests by placing unittest.main() in your file, usually at the bottom. One of the many benefits of unittest, that you ll use when your tests get bigger than the toy examples I m showing on this blog, is the use of setup and teardown functions to get your system ready for the tests. I ll run through a simple example first, then show how I m using unittest for testing markdown.py. unittest example Here is a simple module called unnecessary_math.py. unnecessary_math.py: 1 def multiply(a, b): 2 return a * b Here s some example test code to test my multiply function. test_um_unittest.py:

unittest introduction 3 1 import unittest 2 from unnecessary_math import multiply 3 4 class TestUM(unittest.TestCase): 5 6 def setup(self): 7 pass 8 9 def test_numbers_3_4(self): 10 self.assertequal( multiply(3,4), 12) 11 12 def test_strings_a_3(self): 13 self.assertequal( multiply('a',3), 'aaa') 14 15 if name == ' main ': 16 unittest.main() In this example, I ve used assertequal(). The unittest framework has a whole bunch of assertblah() style functions like assertequal(). Once you have a reasonable reference⁴ for all of the assert functions bookmarked, working with unnittest is pretty powerful and easy. Aside from the tests you write, most of what you need to do can be accomplished with the test fixture methods such as setup, teardown, setupclass, teardownclass, etc. Running unittests At the bottom of the test file, we have this code: 1 if name == ' main ': 2 unittest.main() This allows us to run all of the test code just by running the file. Running it with no options is the most terse, and running with a -v is more verbose, showing which tests ran. ⁴http://docs.python.org/2/library/unittest.html#unittest.TestCase

unittest introduction 4 1 > python test_um_unittest.py 2.. 3 ------------------------------------------ 4 Ran 2 tests in 0.000s 5 6 OK 7 > python test_um_unittest.py -v 8 test_numbers_3_4 ( main.testum)... ok 9 test_strings_a_3 ( main.testum)... ok 10 11 ------------------------------------------ 12 Ran 2 tests in 0.000s 13 14 OK Test discovery Let s say that you ve got a bunch of test files. It would be annoying to have to run each test file separately. That s where test discovery comes in handy. In our case, all of my test code (one file for now) is in simple_example. To run all of the unittests in there, use python -m unittest discover simple_example, with or without the -v, like this: 1 > python -m unittest discover simple_example 2.. 3 -------------------------------------------------- 4 Ran 2 tests in 0.000s 5 6 OK 7 > python -m unittest discover -v simple_example 8 test_numbers_3_4 (test_um_unittest.testum)... ok 9 test_strings_a_3 (test_um_unittest.testum)... ok 10 11 --------------------------------------------------- 12 Ran 2 tests in 0.000s 13 14 OK unittest example with markdown.py Now, I ll throw unittest at my markdown.py project. This is going to be pretty straightforward, as the tests are quite similar to the doctest versions, just

unittest introduction 5 formatted with all of the unittest boilerplate stuff, especially since I don t need to make use of startup or teardown fixtures. test_markdown_unittest.py: 1 import unittest 2 from markdown_adapter import run_markdown 3 4 class TestMarkdownPy(unittest.TestCase): 5 6 def setup(self): 7 pass 8 9 def test_non_marked_lines(self): 10 ''' 11 Non-marked lines should only get 'p' tags around all input 12 ''' 13 self.assertequal( 14 run_markdown('this line has no special handling'), 15 '<p>this line has no special handling</p>') 16 17 def test_em(self): 18 ''' 19 Lines surrounded by asterisks should be wrapped in 'em' tags 20 ''' 21 self.assertequal( 22 run_markdown('*this should be wrapped in em tags*'), 23 '<p><em>this should be wrapped in em tags</em></p>') 24 25 def test_strong(self): 26 ''' 27 Lines surrounded by double asterisks should be wrapped in 'strong' tags 28 ''' 29 self.assertequal( 30 run_markdown('**this should be wrapped in strong tags**'), 31 '<p><strong>this should be wrapped in strong tags</strong></p>') 32 33 if name == ' main ': 34 unittest.main() Testing markdown.py And now we can see that everything is failing (as expected).

unittest introduction 6 1 > python test_markdown_unittest.py 2 FFF 3 ====================================================================== 4 FAIL: test_em ( main.testmarkdownpy) 5 ---------------------------------------------------------------------- 6 Traceback (most recent call last): 7 File "test_markdown_unittest.py", line 29, in test_em 8 '<p><em>this should be wrapped in em tags</em></p>') 9 AssertionError: '*this should be wrapped in em tags*'!= '<p><em>this should be \ 10 wrapped in em tags</em></p>' 11 12 ====================================================================== 13 FAIL: test_non_marked_lines ( main.testmarkdownpy) 14 ---------------------------------------------------------------------- 15 Traceback (most recent call last): 16 File "test_markdown_unittest.py", line 21, in test_non_marked_lines 17 '<p>this line has no special handling</p>') 18 AssertionError: 'this line has no special handling'!= '<p>this line has no spec\ 19 ial handling</p>' 20 21 ====================================================================== 22 FAIL: test_strong ( main.testmarkdownpy) 23 ---------------------------------------------------------------------- 24 Traceback (most recent call last): 25 File "test_markdown_unittest.py", line 37, in test_strong 26 '<p><strong>this should be wrapped in strong tags</strong></p>') 27 AssertionError: '**this should be wrapped in strong tags**'!= '<p><strong>this \ 28 should be wrapped in strong tags</strong></p>' 29 30 ---------------------------------------------------------------------- 31 Ran 3 tests in 0.142s 32 33 FAILED (failures=3) More unittest info The python.org page on unittest⁵ is a great source for information on unittest. ⁵http://docs.python.org/2/library/unittest.html

Full Table Of Contents You are reading a sample, which just has the introduction and an intrudoction to unittest. If this is useful to you, please consider purchasing the full book. The full book contains: unittest unittest introduction * Overview of unittest * Running unittests * Test discovery * unittest examples unittest fixture syntax and flow reference * Software Test Fixtures * Common Case Example * Full Test Fixture Example * Full Test Fixture Flow * Adding Cleanup Calls * Skipping tests within setup * Exceptions in Fixtures nose nose introduction * Nose example * Running nose * Nose fixtures * Nose assert_equals * Test discovery * Running unittests from nose * Running doctests from nose nose support for unittest style fixtures nose fixture reference * Method, Function, Package, Module, and Class fixtures * Full example * Control flow * Alternative names for fixtures pytest pytest introduction * pytest example

Full Table Of Contents 8 * Running pytest * pytest fixtures * Test discovery * Running unittests from pytest * Running doctests from pytest * Running nose tests from pytest pytest fixtures * pytest unittest style fixtures * pytest xunit style fixtures * pytest fixtures easy example * pytest fixtures nuts and bolts * pytest session scoped fixtures