/LAB-flask-bdd

NYU DevOps lab on Behavior Driven Development with Flask and Behave

Primary LanguagePythonApache License 2.0Apache-2.0

Lab: Python Flask Behavior Driven Development

Build Status Build Status Open in Remote - Containers

This repository is a lab from the NYU DevOps and Agile Methodologies graduate course CSCI-GA.2820-001 on Behavior Driven Development with Flask and Behave

The sample code is using Flask micro-framework and is intended to be deployed to Kubernetes on IBM Cloud. It also uses PostgreSQL as a database.

Introduction

One of my favorite quotes is:

"If it's worth building, it's worth testing. If it's not worth testing, why are you wasting your time working on it?"

As Software Engineers we need to have the discipline to ensure that our code works as expected and continues to do so regardless of any changes, refactoring, or the introduction of new functionality.

This lab introduces Test Driven Development using PyUnit and nose. It also explores the use of using RSpec syntax with Python through the introduction of the compare library that introduces the expects statement to make test cases more readable.

It also introduces Behavior Driven Development using Behave as a way to define Acceptance Tests that customer can understand and developers can execute!

Prerequisite Software Installation

This lab uses Docker and Visual Studio Code with the Remote Containers extension to provide a consistent repeatable disposable development environment for all of the labs in this course.

You will need the following software installed:

All of these can be installed manually by clicking on the links above or you can use a package manager like Homebrew on Mac of Chocolatey on Windows.

Alternately, you can use Vagrant and VirtualBox to create a consistent development environment in a virtual machine (VM).

You can read more about creating these environments in my article: Creating Reproducible Development Environments

Bring up the development environment

To bring up the development environment you should clone this repo, change into the repo directory:

git clone https://github.com/nyu-devops/lab-flask-bdd.git
cd lab-flask-bdd

Depending on which development environment you created, pick from the following:

Start developing with Visual Studio Code and Docker

Open Visual Studio Code using the code . command. VS Code will prompt you to reopen in a container and you should say yes. This will take a while as it builds the Docker image and creates a container from it to develop in.

code .

Note that there is a period . after the code command. This tells Visual Studio Code to open the editor and load the current folder of files.

Once the environment is loaded you should be placed at a bash prompt in the /app folder inside of the development container. This folder is mounted to the current working directory of your repository on your computer. This means that any file you edit while inside of the /app folder in the container is actually being edited on your computer. You can then commit your changes to git from either inside or outside of the container.

Manually running the Tests

This repository has both unit tests and integration tests. You can now run nosetests and behave to run the TDD and BDD tests respectively.

Test Driven Development (TDD)

This repo also has unit tests that you can run nose

nosetests

Nose is configured to automatically include the flags --with-spec --spec-color so that red-green-refactor is meaningful. If you are in a command shell that supports colors, passing tests will be green while failing tests will be red.

Behavior Driven Development (BDD)

These tests require the service to be running because unlike the the TDD unit tests that test the code locally, these BDD integration tests are using Selenium to manipulate a web page on a running server.

Run the tests using behave

Start the server in a separate bash shell:

honcho start

Then start behave in your original bash shell:

behave

What's featured in the project?

./service/routes.py -- the main Service using Python Flask
./service/models.py -- the data models for persistence
./service/common -- a collection of status, error handlers and logging setup
./tests/test_routes.py -- unit test cases for the server
./tests/test_models.py -- unit test cases for the model
./features/pets.feature -- Behave feature file
./features/steps/web_steps.py -- Behave step definitions

License

Copyright (c) 2016, 2022, John Rofrano. All rights reserved.

Licensed under the Apache License. See LICENSE

This repository is part of the NYU graduate class CSCI-GA.2810-001: DevOps and Agile Methodologies taught by John Rofrano, Adjunct Instructor, NYU Courant Institute, Graduate Division, Computer Science.