README.md 1.58 KB
Newer Older
Gabriel Couture's avatar
Gabriel Couture committed
1 2
# HowToTestBasic

Gabriel Couture's avatar
Gabriel Couture committed
3
Example of a python project with tests.
Gabriel Couture's avatar
Gabriel Couture committed
4 5 6 7 8 9 10

__Note__: Please note that this project does not really respect
"good practices" of software engineering. Functions in the `conversion` 
module should have been in the `DVH` class. The reason why it is not the case 
in this example is because I want to show simple tests of functions (see `tests/test_conversion`) that
use a custom object (`DVH`). This way, we can introduce `mocking`, which is
an important concept of unit testing.
Gabriel Couture's avatar
Gabriel Couture committed
11 12 13 14

## Setup.py

The `setup.py` file is where you setup your commands for the tests.
Gabriel Couture's avatar
Gabriel Couture committed
15 16 17 18
Here, in this basic project example, the only command to run tests is:
```bash
$ python setup.py test
```
Gabriel Couture's avatar
Gabriel Couture committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50


## Virtual environment

Having a virtual environment per projects is a great idea: 
https://realpython.com/python-virtual-environments-a-primer/

To create one, you first need `virtualenv`.
```bash
$ pip install virtualenv
```

After, you can create the virtual environnement in your project path.
Here, my virtual environment will be named `venv`, which is commun for local virtual environment.
```bash
$ virtualenv venv
```

Now, you should have a `venv/` directory in your project root path.
This directory contains the Python interpreter (with pip and other tools) at `./venv/bin/`.
You should use these executable for your project.

## requirements.txt

The `requirements.txt` file contains the needed libraries for the projects and the tests.
You need to have you virtual environment setup before generating the `requirements.txt`.

To generate the `requirements.txt`:
```bash
$ ./venv/bin/pip freeze > requirements.txt
```