# Tests Documentation

Install pytest & pytest-qt:

```bash
cd hev
. .hev_env/bin/activate
pip3 install pytest
pip3 install pytest-qt
```

⚠️ For unit and integration testing, remember️ to export your PYTHONPATH environment variable as shown below.

## Unit Tests

To run all unit tests in the `NativeUI/tests/unittests` dir on a Raspberry Pi or VM, run the following 
(adjust the full path to your NativeUI directory accordingly):

```bash
cd hev
. .hev_env/bin/activate
export PYTHONPATH=/home/pi/hev/NativeUI
pytest NativeUI/tests/unittests
```

To run a single unit test file, set the environment as above and specify the test file:

```bash
pytest NativeUI/tests/unittests/test_hevclient.py
```

## Integration Tests

* ⚠️ Integration tests automatically start / stop the ArduinoEmulator & hevserver, there is no longer any need to run
them manually from the CLI.
* ⚠️ Some integration tests spin up the GUI and simulate button clicks / and interactions with the GUI. 
These tests then assert that the state of the GUI has changed as expected. 
Therefore, a DISPLAY is (currently) needed.

To run all integration tests in the `NativeUI/tests/integration` dir:

```bash
cd hev
. .hev_env/bin/activate
export PYTHONPATH=/home/pi/hev/NativeUI
pytest NativeUI/tests/integration
```

* To run a single integration test file, set the environment as above and specify the test file:

```bash
pytest NativeUI/tests/integration/test_hevclient_integration.py
```


### Integration tests automatically start / stop the ArduinoEmulator & hevserver 

`integration_utils.py` has functions to start/manage/kill the ArduinoEmulator and hevserver
from within pytest code INSTEAD of running these background processes separately on the CLI.
Nothing wrong with using the CLI, but managing these processes from within test-code itself
can be useful - tests can be more atomic and easier to manage: e.g. you can choose to
start/kill these background processes for each/every test individually
and/or once for a particular test-suite.

The `sample_test_hevclient_integration.py` shows sample usage (note, don't run
this as a pytest if hevserver/ArduinoEmulator is already running, it will clash).
This sample integration test is purposefully not pre/appended with 'test' so that
it is excluded from pytest by default, but you can run it explicitly as below:

```bash
cd hev
. .hev_env/bin/activate
export PYTHONPATH=/home/pi/hev/NativeUI
pytest NativeUI/tests/integration/sample_test_hevclient_integration.py
```

## Coverage

To get pytest coverage run from the root of the repo:

```bash
pip install pytest-cov
pytest --cov=NativeUI NativeUI
```

## SOUP CVEs Testing

To test for Common Vulnerabilities and Exposures (CVEs) in the Software of Unknown Provence (SOUP) use the Dockerfile to build a docker image:

```bash
docker build -t hplv .
```

The docker image can then be scanned using `docker scan` which utilizes the [synk](https://snyk.io/) engine:

```bash
docker scan --file Dockerfile --exclude-base hplv
```
