Ashwin Pajankar

Python Unit Test Automation

Automate, Organize, and Execute Unit Tests in Python

2nd ed.
Ashwin Pajankar
Nashik, Maharashtra, India
ISBN 978-1-4842-7853-6e-ISBN 978-1-4842-7854-3
© Ashwin Pajankar 2017, 2022
Apress Standard
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, express or implied, with respect to the material contained herein or for any errors or omissions that may have been made.

This Apress imprint is published by the registered company APress Media, LLC part of Springer Nature.

The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A.

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.

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.

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.

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.

  • 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.

  • 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

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.

Table of Contents
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

 
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.

 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.209.57