Coding kata - Python with pytest

Project layout

  • app contains dummy package to be tested
  • tests contains tests definitions

Installation

Create virtual environment...

python3 -m venv VENV

...activate it:

source VENV/bin/activate

...and install requirements listed in requirements.txt file:

pip install -r requirements.txt

Run

  • Write your test in a python file (test_thing.py in the example)
  • run : python -m pytest tests/test_thing.py
  • run tests with coverage: python3 -m pytest --cov=app tests/
  • or if using python >= 3 on a Mac : python3 -m pytest tests/test_thing.py
  • or if using VSCode + Python extension, right-click any test and select 'Run All Tests' (select 'pytest' when prompted to configure a test framework)
  • or if using PyCharm:
    • First you would need to set up test runner for project:
      • Open Project preferences (On Mac - ⌘,)
      • Navigate into Tools -> Python integrated tools -> Testing -> Test runner
      • Choose pytest
    • After that you can click on tests folder or individual test and either choose to
      • Run default runner using shortcut (On Mac - ⇧^R)
      • Manually choose test configuration under More Run/Debug menu - for example, in order to run tests with coverage

Other

An alternate version with before fixture is provided in the test_thing_fixture.py file.

To run same as before : python -m pytest tests/test_thing_fixture.py or python3 -m pytest tests/test_thing_fixture.py

Resources

run from docker

'./docker_test.sh'