0% found this document useful (0 votes)
4 views

Python Unit Test Automation

The document is a comprehensive guide on Python unit test automation, authored by Ashwin Pajankar. It covers various frameworks and tools for automating unit tests in Python, including unittest, nose, and pytest, and is aimed at Python programmers with some prior knowledge. The book also discusses logging frameworks, coding standards, and test-driven development practices, making it a valuable resource for developers, test automators, and learners in the field.

Uploaded by

Kamal Kannan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Unit Test Automation

The document is a comprehensive guide on Python unit test automation, authored by Ashwin Pajankar. It covers various frameworks and tools for automating unit tests in Python, including unittest, nose, and pytest, and is aimed at Python programmers with some prior knowledge. The book also discusses logging frameworks, coding standards, and test-driven development practices, making it a valuable resource for developers, test automators, and learners in the field.

Uploaded by

Kamal Kannan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Python Unit Test

Automation
Automate, Organize, and
Execute Unit Tests in Python
Second Edition

Ashwin Pajankar
Python Unit Test Automation: Automate, Organize, and Execute
Unit Tests in Python
Ashwin Pajankar
Nashik, Maharashtra, India

ISBN-13 (pbk): 978-1-4842-7853-6 ISBN-13 (electronic): 978-1-4842-7854-3


https://doi.org/10.1007/978-1-4842-7854-3

Copyright © 2022 by Ashwin Pajankar


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or
part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Celestin Suresh John
Development Editor: James Markham
Coordinating Editor: Mark Powers
Copyeditor: Kezia Endsley
Cover designed by eStudioCalamar
Cover image by Jason Leung on Unsplash (www.unsplash.com)
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY
10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected],
or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member
(owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance
Inc is a Delaware corporation.
For information on translations, please e-mail [email protected]; for
reprint, paperback, or audio rights, please e-mail [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available
to readers on GitHub. For more detailed information, please visit http://www.apress.com/
source-code.
Printed on acid-free paper
I dedicate this book to Alan Mathison Turing, an
English mathematician, computer scientist, logician,
cryptanalyst, and philosopher who was influential in the
development of theoretical computer science.
Table of Contents
About the Author .................................................................................xi

About the Technical Reviewers .........................................................xiii


Acknowledgments ..............................................................................xv
Introduction ......................................................................................xvii

Chapter 1: Introduction to Python ........................................................1


The History of Python ............................................................................................1
Features of Python ................................................................................................3
Simple .............................................................................................................3
Easy to Learn ...................................................................................................3
Easy to Read ....................................................................................................4
Easy to Maintain ..............................................................................................4
Open Source ....................................................................................................4
High-Level Language .......................................................................................4
Portable ...........................................................................................................5
Interpreted .......................................................................................................5
Object-Oriented ...............................................................................................5
Extensible ........................................................................................................6
Extensive Libraries ..........................................................................................6
Robust .............................................................................................................6
Rapid Prototyping ............................................................................................7
Memory Management .....................................................................................7

v
TABLE OF CONTENTS

Powerful ..........................................................................................................7
Community Support .........................................................................................8
Python 3 ................................................................................................................8
Differences Between Python 2 and Python 3 ..................................................8
Why Use Python 3 ..........................................................................................10
Installing Python 3 ..............................................................................................11
Installation on Linux ......................................................................................11
Installation on Debian, Ubuntu, and Derivatives ............................................11
Installation on Fedora and CentOS ................................................................11
Installation on macOS X ................................................................................12
Installation on Windows.................................................................................12
Running a Python Program and Python Modes ...................................................15
Interactive Mode ............................................................................................16
Script Mode ...................................................................................................17
IDEs for Python....................................................................................................18
IDLE ...............................................................................................................18
The PyDev Plugin for Eclipse .........................................................................19
Geany.............................................................................................................20
PyCharm ........................................................................................................21
Conclusion ..........................................................................................................23

Chapter 2: Getting Started ..................................................................25


A Brief Introduction to Software Testing Concepts .............................................25
Unit Testing ....................................................................................................26
Test Automation .............................................................................................26
The Benefits of Automated Unit Testing.........................................................26
Using Docstrings .................................................................................................27
Example of a Docstring in Python..................................................................28

vi
TABLE OF CONTENTS

A Brief Introduction to doctest ............................................................................32


Failing Tests ...................................................................................................35
Conclusion ..........................................................................................................41

Chapter 3: unittest ..............................................................................43


Introduction to xUnit............................................................................................44
Using unittest ......................................................................................................45
Order of Execution of the Test Methods.........................................................47
Test Discovery .....................................................................................................73
Coding Conventions for unittest ..........................................................................75
Assertions in unittest ..........................................................................................76
Other Useful Methods ....................................................................................78
Failing a Test .......................................................................................................79
Exceptions in Test Cases ...............................................................................82
Creating Test Suites .......................................................................................86
Creating Test Suites .......................................................................................88
Conclusion ..........................................................................................................89

Chapter 4: nose and nose2 .................................................................91


Introduction to nose ............................................................................................91
Installing nose on Linux Distributions............................................................91
Installing nose on macOS and Windows........................................................92
Verifying the Installation ................................................................................92
Getting Started with nose ..............................................................................93
A Simple nose Test Case ...............................................................................93
Running the Test Module with nosetests .......................................................94
Getting Help ...................................................................................................95
Organizing the Test Code ...............................................................................96
Test Discovery ...............................................................................................98

vii
TABLE OF CONTENTS

Fixtures for Classes, Modules, and Methods ......................................................98


Fixtures for Functions ..................................................................................101
Testing Tools .....................................................................................................107
ok_ and eq_.................................................................................................108
The @raises() Decorator ..............................................................................109
The @timed() decorator...............................................................................111
Report Generation .............................................................................................112
Creating an XML Report...............................................................................112
Creating an HTML Report ............................................................................113
Creating Color Output in the Console ..........................................................114
Running unittest Tests from nose .....................................................................115
Running doctest Tests from nose ......................................................................116
Advantages of nose over unittest......................................................................116
Disadvantages of nose ......................................................................................117
Using nose2 ......................................................................................................118
Conclusion ........................................................................................................121

Chapter 5: pytest ..............................................................................123


Introduction to pytest ........................................................................................124
Simple Test ..................................................................................................124
Running Tests with the py.test Command ...................................................126
Test Class and Test Package in pytest .........................................................127
Test Discovery in pytest...............................................................................129
xUnit-Style Fixtures .....................................................................................129
pytest Support for unittest and nose ...........................................................131
Introduction to pytest Fixtures ..........................................................................131
Scope of pytest Fixtures ..............................................................................137

viii
TABLE OF CONTENTS

Important pytest Command-Line Options .........................................................139


Help .............................................................................................................139
Stopping After the First (or N) Failures ........................................................140
Profiling Test Execution Duration .................................................................140
JUnit-Style Logs ..........................................................................................140
Conclusion ........................................................................................................140

Chapter 6: Testing with Selenium .....................................................143


Introduction to Selenium ...................................................................................143
Selenium IDE .....................................................................................................144
Selenium Webdriver ..........................................................................................155
Selenium with Unittest ......................................................................................158
Conclusion ........................................................................................................159

Chapter 7: Logging in Python ...........................................................161


Logging Basics ..................................................................................................161
Logging with an OS ...........................................................................................162
Manually Logging with File Operations .............................................................163
Logging in Python .............................................................................................165
Logging to a File ..........................................................................................167
Customizing the Log Message.....................................................................168
Customizing Logging Operations .................................................................169
Rotating a Log File .......................................................................................172
Using Multiple Loggers ................................................................................173
Logging with Threads ..................................................................................176
Multiple Loggers Writing to the Same Target ..............................................180
Logging with loguru ..........................................................................................183
Using loguru and the Available Logging Levels ...........................................183
Customizing File Retention ..........................................................................186

ix
TABLE OF CONTENTS

Customizing Tracing ....................................................................................187


Customizing the Log Message Format and Display.....................................189
Configuring with a Dictionary ......................................................................190
Conclusion ........................................................................................................191

Chapter 8: Tips and Tricks ................................................................193


Coding and Filenaming Conventions for Easier Test Discovery ........................193
Test-Driven Development with pytest ...............................................................195
Conclusion ........................................................................................................203

Index .................................................................................................205

x
About the Author
Ashwin Pajankar is a programmer, a maker, an author, a YouTuber, and a
science popularizer. He graduated from IIIT Hyderabad with an MTech in
Computer Science and Engineering. He has a keen interest in promoting
science, technology, engineering, and mathematics (STEM) education.
He has written many books with Packt, Leanpub, BPB, and Apress, and
has also reviewed many books for Packt and Apress. He’s also working on
many more books with Apress.
His YouTube channel has more than 10,000 subscribers and he also
teaches more than 75,000 students on Udemy.
His personal website is www.AshwinPajankar.com.
His LinkedIn profile is
https://in.linkedin.com/in/ashwinpajankar

xi
About the Technical Reviewers
Shraddha Joshi is currently working as an engineer at PwC with experience
in testing with Python and Java in major production environments. She has
worked with various global clients across multiple domains and helped
them build products and solutions with full-fledged testing frameworks.
She has expertise in all phases of the development process and leads the
design, development, execution, and automation stages of test plans for
a diverse set of system components. Previously, she was a Senior Quality
Engineer at Incture Technologies, where she was involved in designing
functional integration and regression test plans, building and executing
manual and automated tests, and performing highly complex analysis for
multiple products. She also helped set cross-functional product testing
standards involving the application of advanced technical/business skills in
the area of specialization.
Shraddha’s great knack for simplifying concepts and explaining them
in an easy-to-understand manner makes her stand apart. She is passionate
about guiding and mentoring people in their technology journey. She is
also actively involved in conducting workshops, webinars, and sessions.
She lives in Bangalore with her family.

Sujay Raghavendra is a distinguished IT professional with a master’s


degree in Information Technology. His research interests include
computer vision, NLP, machine learning, deep learning, and artificial
intelligence. He has served as an advisor for various universities and
startups. He has been active in the research community. He has also
authored a book with Apress Media. He has published research papers at
various international journals and conferences and is a leading industry
expert and mentor for professionals.

xiii
Acknowledgments
I am grateful to the student and teacher community which, with their
continual bombardment of queries, impelled me to learn more, simplify
my findings, and organize them into a book. This book is for them.
I wish to thank my friends and colleagues—the practitioners from the
field—for their good counsel and for filling me in on the latest in the field
of test automation.
A special thanks to the technical reviewers—Shraddha and Sujay—for
their vigilant review and for providing their expert opinions.
I consider myself very fortunate for the editorial assistance provided
by Apress; the collaboration with them has been fabulous. I am thankful
to Celestin Suresh John, Senior Manager, Editorial Acquisitions, Apress
and Springer Science and Business Media Company, for giving me
this and many other opportunities to collaborate with Apress. I wish to
acknowledge and appreciate James Markham, Mark Powers, and the
team of associates from Apress who adeptly guided me through the entire
process of preparation and publication.

xv
Introduction
Why This Book?
I have been using Python for more than ten years on a wide variety of
projects. Initially, I used it for GUI applications. Then I quickly moved to
scientific usage, as my academic projects demanded it. When I entered
professional life, I used it for automation first and then for implementation
of alert mechanisms. I have been using Python for the last six years in the
fields of scientific computing, Internet of Things (IoT), and single board
computers. I have written plenty of Python code over these years.
I always prefer it to bash scripting, which offers limited capabilities to
users like me. At different points over the last ten years, I’ve worked as a
developer, an R&D engineer, a maker, an author, and a QA specialist. I used
Python in every single role.
Whenever I write code, I unit test it thoroughly. I used to unit test all
my Python modules in the good old manual way. I used to run all the
scripts once and compare the outcome with what was expected. However,
I learned that when your codebase grows larger, it’s pretty difficult to test
the scripts manually. Also, all the scripts have to be tested, re-tested, and
tested for regression whenever a small part of the codebase changes. I was
looking for a way to run all the tests automatically, which led me to reading
about test automation. It immediately piqued my curiosity and, after a
couple of days, I was running my own automated Python tests.
After acquainting myself with the philosophy of test automation,
I applied my newfound knowledge to automate unit and integration testing
to web, mobile, GUI, API, and a variety of other types of applications using
programming languages like C++, Python, Java, and PHP.

xvii
INTRODUCTION

I wrote this book to share my knowledge and experiences while


automating unit tests in Python 3. I explore different frameworks and
plugins in this book. I learned about the tools and techniques explained
in this book by spending numerous hours learning, coding, discussing,
and actively participating in diverse Internet forums. I have condensed the
knowledge to the basics of the unit test automation frameworks. I hope you
will enjoy reading and following the book as much as I enjoyed writing it.
This book includes the following:

• An introduction to Python and various IDEs

• Various test automation frameworks for Python 3,


including doctest, unittest, nose, nose2, and pytest

• Logging frameworks and web driver automation

• Coding standards for Python 3 test automation and


implementation of test-driven development with
pytest in Python 3

Who This Book Is For


The main audience of this book is Python 3 programmers who want to
automate their unit tests. This includes a large and diverse set of people,
including developers, test automators, students, researchers, and novice
learners. The book is for those who have some knowledge of the Python
programming language. The test automation engineers who have already
worked with other programming frameworks, such as Java and C++, will
find this book immensely useful to learn how test automation is done in
Python 3. If you are just beginning with Python 3 programming and want
to quickly get into automating the unit tests of your modules and packages,
you will find this book helpful.

xviii
INTRODUCTION

This book is not a book for learning Python 3 programming and syntax
from scratch. It is also not a DIY cookbook for development projects. If
your understanding of coding is limited, you will find it difficult to follow
this book.

How This Book Is Organized


This book has eight chapters. Here is a sneak peek into the topics covered
in each chapter:

• Chapter 1: This chapter introduces you to the history


and philosophy of Python. It teaches you how to install
Python and how to set up the environment for Python 3
programming. It also briefly explores the new features
of Python 3 and introduces you to a few popular
Python 3 IDEs.

• Chapter 2: The aim of this chapter is to quickly get


you started with unit test automation in Python 3. The
chapter revises the understanding of testing concepts
and quickly moves into implementing those concepts
with docstring and doctest.

• Chapter 3: This chapter serves to introduce xUnit and


its philosophy to you. Then it proceeds to teach you
how to implement concepts of xUnit with unittest, a
xUnit port for Python.

• Chapter 4: This chapter explores the inadequacies


of unittest. Then it explores a better unit-testing
framework, called nose. It explains the installation of
plugins for nose to generate reports. It also discusses
nose2, which is nose's next-generation version that’s
under active development.

xix
INTRODUCTION

• Chapter 5: This chapter introduces you to a modular,


easy-to-use, unit test framework for Python, called
pytest. It discusses the drawbacks of nose and
compares nose, unittest, and pytest.

• Chapter 6: This chapter introduces you to a web driver


automation framework known as selenium. You will
learn how to use the Selenium IDE and Selenium
Python library.

• Chapter 7: This chapter introduces you to various


logging frameworks in Python. First, you will explore
the built-in framework, logging. Then you will explore
a third-party logging library, called loguru.

• Chapter 8: This chapter helps you understand the


coding and filenaming conventions for facilitating
easier test discovery across various unit test
frameworks in Python. The chapter concludes the book
by implementing a test-driven development in Python
3 using pytest.

How to Get the Most Out of This Book


To get the most out of this book, it’s best to abide by the following:

• Read the chapters thoroughly. Use the chapters hands-


on by following the step-by-step instructions stated
in the code examples. Do not skip any of the code
examples.

• If need be, repeat them a second time or until the


concept is firmly etched in your mind.

• Join a Python community or discussion forum.

xx
INTRODUCTION

• Explore and practice with various Python IDEs

• Read the online documentation available for various


test automation frameworks for Python 3.

• Read the blogs related to test automation, Python 3,


migration to Python 3 from Python 2, logging, and test-
driven development.

Where Next?
I have endeavored to unleash the power of the unit test automation
libraries for Python 3 as an aid to the community of professional
developers and testers. I recommend you read the book from cover to
cover, without skipping any of the chapters, text, or code examples.
I wish you well in exploring Python!

A Quick Word About the Instructors’ Fraternity


Attention has been paid to the sequence of chapters and to the flow of
topics in each chapter. This is done particularly with an objective to assist
my fellow instructors and academicians in carving out a syllabus from the
Table of Contents (ToC) of the book. The complete ToC complements the
syllabus of “Introduction to Software Testing,” if students were introduced
to programming during their freshman year with the help of Python.
I have ensured that each concept discussed in this book includes
adequate hands-on content to enable you to teach better and to provide
ample hands-on practice to your students.
Happy learning and exploring!
—Ashwin Pajankar

xxi

You might also like