dimanche 21 juin 2020

sys.path.insert before all tests in pytest

I have Python project with following structure, where src is project root:

- src
  - __init__.py
  - calc.py
- test
  - __init__.py
  - test_calc.py

Where calc.py looks like this:

def add(a, b):
    return a + b

And test_calc.py (tests are run by terminal command py.test):

import pytest
import sys
import os

sys.path.insert(0, os.path.abspath("../src"))

import calc

def test_add():
    assert calc.add(1, 2) == 3

However if the project will be bigger and I will have more test files, there must be this line in each test file:

sys.path.insert(0, os.path.abspath("../src"))

else error appears:

E ModuleNotFoundError: No module named 'calc'

Is there any way to add dir to sys path before all tests are loaded? I would not like to write this line in every test file.

Aucun commentaire:

Enregistrer un commentaire