PytestSetting Project Test System: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(12 intermediate revisions by the same user not shown)
Line 9: Line 9:
<font size=-1>
<font size=-1>
  ...
  ...
   └─ test
   └─ tests
       └─ python
       ├─ data
          └─ my_module
      ├─ util.py <font color=teal># get_file()</font>
              ├─ data
      └─ my_module
              └─ test_my_function.py
          └─ test_my_function.py
<font size=-1>
<font size=-1>
<code>util.py</code>:
<syntaxhighlight lang='py'>
from pathlib import Path
def get_file(path_rel_to_data):
    """
    Return the resolved path corresponding to path_rel_to_data, which is
    relative to the test 'data' file. The function does not attempt to check
    whether the file or directory exists.
    try to resolve
    :param path_rel_to_data:
    :return: the resolved path
    """
    return str(Path(Path(__file__).parent, './data/' + path_rel_to_data))
</syntaxhighlight>
=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'>
PYTHONPATH="$(dirname $0)/src:${PWD}/tests"
export PYTHONPATH
</syntaxhighlight>
 
This can be added to <code>.envrc</code> as follows:


<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
PYTHONPATH="$(dirname $0)/src/main/python"
PYTHONPATH="${PWD}/src:${PWD}/tests"
export PYTHONPATH
export PYTHONPATH
</syntaxhighlight>
</syntaxhighlight>
Line 31: Line 55:
from my_module_to_test import some_function
from my_module_to_test import some_function


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




</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 04:21, 25 June 2022

Internal

Install pytest

Install pytest in the virtual environment associated with your project:

Pytest Installation

Layout

...
 └─ tests
     ├─ data
     ├─ util.py # get_file()
     └─ my_module
         └─ test_my_function.py

util.py:

from pathlib import Path

def get_file(path_rel_to_data):
    """
    Return the resolved path corresponding to path_rel_to_data, which is
    relative to the test 'data' file. The function does not attempt to check
    whether the file or directory exists.
    try to resolve
    :param path_rel_to_data:
    :return: the resolved path
    """
    return str(Path(Path(__file__).parent, './data/' + path_rel_to_data))

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}')