Book Image

Mastering Flask

By : Jack Stouffer
Book Image

Mastering Flask

By: Jack Stouffer

Overview of this book

Starting from a simple Flask app, this book will walk through advanced topics while providing practical examples of the lessons learned. After building a simple Flask app, a proper app structure is demonstrated by transforming the app to use a Model-View-Controller (MVC) architecture. With a scalable structure in hand, the next chapters use Flask extensions to provide extra functionality to the app, including user login and registration, NoSQL querying, a REST API, an admin interface, and more. Next, you’ll discover how to use unit testing to take the guesswork away from making sure the code is performing as it should. The book closes with a discussion of the different platforms that are available to deploy a Flask app on, the pros and cons of each one, and how to deploy on each one.
Table of Contents (20 chapters)
Mastering Flask
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

How does testing work?


Let's start with a very simple Python function for us to test.

def square(x):
    return x * x

In order to verify the correctness of this code, we pass a value and we will test if the result of the function is what we expect. For example, we would give it an input of five and would expect the result to be 25.

To illustrate the concept, we can manually test this function in the command line using the assert statement. The assert statement in Python simply says that if the conditional statement after the assert keyword returns False, throw an exception as follows:

$ python
>>> def square(x): 
...     return x * x
>>> assert square(5) == 25
>>> assert square(7) == 49
>>> assert square(10) == 100
>>> assert square(10) == 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError

Using these assert statements, we verified that the square function was working as intended.