shiningflash / dockerized-pytest

Python Unit Testing with Pytest, Docker and TDD (Test-Driven Development) principles

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pytest_project

Prerequisites

Install Docker

  • These tests have been packaged to run with all dependencies installed within a Docker container. Due to the use of f-strings, this must be run with python 3.6+. The Docker image is based on python 3.7

Usage

To run open a shell

$ docker-compose build
$ docker-compose run test sh

This will open the docker shell and you can run one of the following commands:

Run the entire test suite

$ pytest

Run the tests for a certain file matching a keyword

$ pytest -k <test_file_name>

Run tests while printing all variables and verbose output

$ pytest -vvl

To exit the shell

$ exit

Debugging

  1. If you page up (ctrl + fn) within the debug output when running pytest -vvl or when encountering test errors, your cursor may stick and be unable to continue writing in the docker shell. You can get past this by typing q to return to entry mode in the docker container.

  2. If you'd like to debug a piece of code, you can add either of the following built-in functions to a section of the code to enter into the pdb debugger while running pytest.

    • breakpoint() (python 3)
    • import pdb; pdb.set_trace() (python 2)

Additional Practice

  • Try to create a Map class that assembles the points you've created into a data structure. Create one test to make sure the map has the appropriate points and create another test to raise an exception if you assert the Map has a point that it has not stored.

  • Write a simple xml or json data processor to our data_processor.py file. After doing so, update the factory fixture to also allow a user to pass in the data for testing. There's a json_processor.py file in the scripts file if you get stuck and want to compare what you've done to a sample json reader.

  • Create a User class. Each User should have a height and favorite color. Based on their height, create a function that awards them different types of candy. Try working from the test first as you add this functionality and think of ways to parametrize your test.

  • Find open source Python projects that use pytest and read their tests. Then go to the source code to validate your assumptions. Try doing this repeatedly over a few projects that use the pytest framework. Here are a few to get you started: - Pytest retains a list of projects. - sentry, cross-platform application monitoring, with a focus on error reporting - pdb++, a drop-in replacement for pdb - Astropy,a single core package for Astronomy in Python

About

Python Unit Testing with Pytest, Docker and TDD (Test-Driven Development) principles


Languages

Language:Python 98.9%Language:Dockerfile 1.1%