Skip to content

pranaypareek/cc

Repository files navigation

Lab: Python Flask Behavior Driven Development

Build Status Codecov

This repository is part of lab for the NYU DevOps class for Spring 2017, CSCI-GA.3033-013 on Behavior Driven Development with Flask and Behave

The sample code is using Flask microframework and is intented to test the Python support on IBM's Bluemix environment which is based on Cloud Foundry. It also uses Redis as a database for storing JSON objects.

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 noseOfYeti and expects as plug-ins that 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!

Setup

For easy setup, you need to have Vagrant and VirtualBox installed. Then all you have to do is clone this repo and invoke vagrant:

git clone https://github.com/nyu-devops/lab-flask-bdd.git
cd lab-flask-bdd
vagrant up && vagrant ssh
cd /vagrant

You can now run behave and nosetests to run the BDD and TDD tests respectively.

Manually running the Tests

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

Run the tests using behave

    $ python run.py &
    $ behave

Note that the & runs the server in the background. To stop the server, you must bring it to the foreground and then press Ctrl+C

Stop the server with

    $ fg
    $ <ctrl+c>

Alternately you can run the server in another shell by opening another terminal window and using vagrant ssh to establish a second connection to the VM.

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.

What's featured in the project?

* ./app/server.py -- the main Service using Python Flask
* ./tests/test_server.py -- unit test cases for the server
* ./tests/test_items.py -- unit test cases for the model
* ./features/items.feature -- Behave feature file
* ./features/steps/steps.py -- Behave step definitions

Running these tests using Docker containers

If you want to deploy this example in a Docker container, you can run the tests from the container.

This service requires Redis so first start a Redis container

docker run -d --name redis-service -p 6379:6379 redis:alpine

Next build this repo as a container

docker build -t flask-bdd .

To run nosetests just run it in a container while linking it to the redis-service container that we have running.

docker run --rm --link redis-service flask-bdd nosetests

To run behave tests we need an instance of our service running so it takes two docker commands, one to run our service and another to run the behave tests

docker run -d --name flask-service --link redis-service -p 5000:5000 flask-bdd
docker run --rm --link flask-service -e BASE_URL="http://flask-service:5000/" flask-bdd behave

Notice how we injected the URL of the running service into our container running the behave tests using an environment variable in keeping with 12-factor configuration recommendations.

To bring down these services use:

docker stop flask-bdd
docker stop redis-service

...and to remove them with:

docker rm flask-bdd
docker rm redis-service

John Rofrano, Adjunct Professor, NYU

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published