This book covers testing and test-driven development practices, introducing you to the most widespread tools and concepts that are common in the software testing community, using both the Python native unittest module and the pytest framework.
Who this book is for
This book is aimed at any Python developers that want to learn how they can test their applications and integrate testing into their development model, as well as for developers who know how to test software in other languages but are just turning to Python and thus don't yet know which tools are available to them.
What this book covers
Chapter 1, Getting Started with Software Testing, provides an introduction to the core concepts of automated testing and to the unittest Python module.
Chapter 2, Test Doubles with a Chat Application, presents the most common kinds of test doubles while building a real-time chat application.
Chapter 3, Test-Driven Development while Creating a TODO List, covers writing a todo list application adhering to the test-driven development best practices.
Chapter 4, Scaling the Test Suite, explores the complexities of maintaining a test suite as the software and the suite grow in size and complexity.
Chapter 5, Introduction to PyTest, presents the pytest framework and explores how it differs from the unittest module.
Chapter 6, Dynamic and Parametric Tests and Fixtures, dives into more advanced features of pytest, such as parametric tests and dynamic fixtures.
Chapter 7, Fitness Function with a Contact Book Application, dives into more advanced concepts related to acceptance tests and Acceptance Test Driven Development (ATDD) building a real application.
Chapter 8, PyTest Essential Plugins, showcases the most widespread pytest plugins that can be helpful in most projects.
Chapter 9, Managing Test Environments with Tox, presents how to manage test suites across different Python environments.
Chapter 10, Test Documentation and Property-Based Testing, introduces the concept of testing documentation and auto-generating tests based on the properties of the system under test.
Chapter 11, Testing for the Web: WSGI versus HTTP, covers how to test client-server applications based on the HTTP and WSGI protocols.
Chapter 12, End-to-End Testing with the Robot Framework, covers how to write tests that drive a real browser acting on a web application.
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."
Get in touch
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at customercare@packtpub.com.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Reviews
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packt.com.