PytestSetting Project Test System: Difference between revisions
Jump to navigation
Jump to search
(→Layout) |
|||
Line 23: | Line 23: | ||
=Environment= | =Environment= | ||
Add the path to the directory that contains the module to test (in this case <code>./src/main/python</code> contains <code>my_module_to_test.py</code>) | Add the path to the directory that contains the module to test (in this case <code>./src/main/python</code> contains <code>my_module_to_test.py</code>). In case there is auxiliary code in the <code>tests</code> directory, like <code>util.py</code>, also add "./tests" to the path. | ||
<syntaxhighlight lang='bash'> | <syntaxhighlight lang='bash'> | ||
PYTHONPATH="$(dirname $0)/src/ | PYTHONPATH="$(dirname $0)/src:${PWD}/tests" | ||
export PYTHONPATH | export PYTHONPATH | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 33: | Line 33: | ||
<syntaxhighlight lang='bash'> | <syntaxhighlight lang='bash'> | ||
PYTHONPATH="${PWD}/src/ | PYTHONPATH="${PWD}/src:${PWD}/tests" | ||
export PYTHONPATH | export PYTHONPATH | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 04:05, 25 June 2022
Internal
Install pytest
Install pytest
in the virtual environment associated with your project:
Layout
... └─ tests ├─ data ├─ util.py # get_data_file() └─ my_module └─ test_my_function.py
util.py
def
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
). In case there is auxiliary code in the tests
directory, like util.py
, also add "./tests" to the path.
PYTHONPATH="$(dirname $0)/src:${PWD}/tests"
export PYTHONPATH
This can be added to .envrc
as follows:
PYTHONPATH="${PWD}/src:${PWD}/tests"
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}')