Python Unit Test Automation
Python Unit Test Automation
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
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
vi
TABLE OF CONTENTS
vii
TABLE OF CONTENTS
viii
TABLE OF CONTENTS
ix
TABLE OF CONTENTS
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.
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
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.
xix
INTRODUCTION
xx
INTRODUCTION
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!
xxi