README.md 2.05 KB
Newer Older
Gabriel Couture's avatar
Gabriel Couture committed
1
# HowToTest
Gabriel Couture's avatar
Gabriel Couture committed
2

Gabriel Couture's avatar
Gabriel Couture committed
3 4
Example of a python project with tests. Presentation in french can be found here:
https://gitlab.physmed.chudequebec.ca/gacou54/howtotest/blob/master/docs/pdf/HowToTest.pdf
Gabriel Couture's avatar
Gabriel Couture committed
5 6

__Note__: Please note that this project does not really respect
Gabriel Couture's avatar
Gabriel Couture committed
7
"good practices" of software engineering. Some functions in the `conversion` 
Gabriel Couture's avatar
Gabriel Couture committed
8 9
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
Gabriel Couture's avatar
Gabriel Couture committed
10
use a custom object (`DVH` here). This way, we can introduce `mocking`, which is
Gabriel Couture's avatar
Gabriel Couture committed
11
an important concept of unit testing.
Gabriel Couture's avatar
Gabriel Couture committed
12 13 14 15

## Setup.py

The `setup.py` file is where you setup your commands for the tests.
Gabriel Couture's avatar
Gabriel Couture committed
16
Here, in this project example, the command to run all tests is:
Gabriel Couture's avatar
Gabriel Couture committed
17 18 19
```bash
$ python setup.py test
```
Gabriel Couture's avatar
Gabriel Couture committed
20

Gabriel Couture's avatar
Gabriel Couture committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
You can run linters (checking code format and typing) with
```bash
$ python setup.py lint
```

Unit tests with
```bash
$ python setup.py unit
```

And integration tests with
```bash
$ python setup.py integration
```


There is others commands, you can look the `setup.py` to see them.
A simplier `setup.py` file can look like the `basic_setup.py` file.

Gabriel Couture's avatar
Gabriel Couture committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

## 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
```