Software development practices

This exercise and demo are hands-on and done in your own system.

However as all other exercises are notebooks, the instructions are distributed as a notebook as well.

Set-up

In an appropriate directory, run the following commands to clone the example git repository.

$ git clone https://github.com/csc-training/python-quality-exercise.git
$ cd python-quality-exercise

Now you are in the repository. You can view the README file for an explanation.

Create a virtual environment (with Python3 with the following

$ virtualenv -p python3 python-quality
  [...]
$ source python-quality/bin/activate
(python-quality) $

Install the required package versions with

(python-quality) $ pip install -r requirements.txt

Now, whenever you want to activate the virtual environment you run:

$ source python-quality/bin/activate
(python-quality) $
```

Tests

You can now run tests using pytest with

(python-quality) $ pytest

Go ahead and find out the functions you can test and write unit tests for some of them.

You might want to use mock for some tests, for instance to avoid the initialization of DemoClass, but at least the find_median function should be simple to test with a few examples.

Test coverage

You can check the coverage of your tests with pytest-cov, a coverage plugin for pytest by running.

pytest --cov demolib

Observe that initially demomodule.py isn't even imported so it's not in the report.

Style

You can test style with pep8 by running

(python-quality) $ pep8 .

Or with pylint by running

(python-quality) $ pylint demolib

Go ahead and fix coverage mistakes.

Setup script

To make the package installable, you should put a setup.py at the top level.

A minimal one like in the Python documentation will suffice. The name of the package you wish to expose is, of course demolib.

Go ahead and make a setup script. You can test that the package is installable with

(python-quality) $ pip install .

Tox

To facilitate running test for more than one version of python, use tox.

Create a file called tox.ini, with the following contents

[tox]
envlist = py27,py35
[testenv]
deps= -rrequirements.txt
commands=py.test --cov demolib  # run pytest
            pep8  # run pep8

If you still have any errors go ahead and fix them.

You should now be able to run tox with

(python-quality) $ tox

That's it. If you had an open source project you could e.g. enable Travis to automatically run your tests and style checks against every branch your team makes.