Book Image

Crafting Test-Driven Software with Python

By : Alessandro Molina
Book Image

Crafting Test-Driven Software with Python

By: Alessandro Molina

Overview of this book

Test-driven development (TDD) is a set of best practices that helps developers to build more scalable software and is used to increase the robustness of software by using automatic tests. This book shows you how to apply TDD practices effectively in Python projects. You’ll begin by learning about built-in unit tests and Mocks before covering rich frameworks like PyTest and web-based libraries such as WebTest and Robot Framework, discovering how Python allows you to embrace all modern testing practices with ease. Moving on, you’ll find out how to design tests and balance them with new feature development and learn how to create a complete test suite with PyTest. The book helps you adopt a hands-on approach to implementing TDD and associated methodologies that will have you up and running and make you more productive in no time. With the help of step-by-step explanations of essential concepts and practical examples, you’ll explore automatic tests and TDD best practices and get to grips with the methodologies and tools available in Python for creating effective and robust applications. By the end of this Python book, you will be able to write reliable test suites in Python to ensure the long-term resilience of your application using the range of libraries offered by Python for testing and development.
Table of Contents (18 chapters)
1
Section 1: Software Testing and Test-Driven Development
6
Section 2: PyTest for Python Testing
13
Section 3: Testing for the Web
16
About Packt

To get the most out of this book

You will need a recent version of Python 3 and pip (the package installer for Python).

All code examples in this book have been tested using Python 3.7, 3.8, and 3.9 on Linux. However, they should work on other systems too. PyTest 6.0.2 was used by the examples that rely on PyTest.

Software/hardware covered in the book

OS requirements

Python 3.7, 3.8, or 3.9

Windows, MacOSX, or Linux (any)

pip 18+

PyTest 6.0.2+


Additional packages and libraries will be installed from the Python Package Index (
PyPI) using pip over the course of the chapters.

If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Crafting-Test-Driven-Software-with-Python. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "The test prepares a dbpath object for the sole purpose of checking that dbmanager is asked to load that specific path."

A block of code is set as follows:

    def test_load(self):
dbpath = Path(tempfile.gettempdir(), "something")
dbmanager = Mock(
load=Mock(return_value=["buy milk", "buy water"])
)
app = TODOApp(io=(Mock(return_value="quit"), Mock()),
dbpath=dbpath, dbmanager=dbmanager)

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    def run(self):
self._quit = False
while not self._quit:
self._out(self.prompt(self.items_list()))
command = self._in()
self._dispatch(command)
self._out("bye!\n")

def items_list(self):
enumerated_items = enumerate(self._entries, start=1)
return "\n".join(
"{}. {}".format(idx, entry) for idx, entry in enumerated_items
)

Any command-line input or output is written as follows:

$ pip install pytest pytest-bdd

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select System info from the Administration panel."

Warnings or important notes appear like this.
Tips and tricks appear like this.