PytestSetting Project Test System: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 27: Line 27:


<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
PYTHONPATH="${PWD}/src/main/\python"
PYTHONPATH="${PWD}/src/main/python"
export PYTHONPATH
export PYTHONPATH
</syntaxhighlight>
</syntaxhighlight>

Revision as of 03:14, 25 June 2022

Internal

Install pytest

Install pytest in the virtual environment associated with your project:

Pytest Installation

Layout

...
 └─ test
     └─ python
         └─ my_module
             ├─ data
             └─ test_my_function.py

Environment

Add the path to the directory that contains the module to test (in this case ./src/main/python contains my_module_to_test.py):

PYTHONPATH="$(dirname $0)/src/main/python"
export PYTHONPATH

This can be added to .envrc as follows:

PYTHONPATH="${PWD}/src/main/python"
export PYTHONPATH

Structure of a Test File

import pytest
from pathlib import Path
from my_module_to_test import some_function

def get_test_file(file_name):
    return Path(Path(__file__).parent, f'./data/{file_name}')