1

In my project I have the following folder structure:

mymodule/
   main.py
tests/
   test_main.py

where test_main.py looks like this:

from .mymodule.main import myfunc

def test_myfunc():
    res = myfunc()
    assert res > 1

I want to test it with pytest, but when I call pytest in the parent folder (above mymodule and test), I get `ImportError: cannot import

I tried a trick with

import sys
sys.path.insert(0, '/path/to/application/app/folder')

but it doesn't work either.

I'm using Python 3.9.4.

A. Newski
  • 156
  • 1
  • 8

1 Answers1

0

First, create an empty __init__.py in mymodule and tests directories.

In your test_main.py file, try replacing:

  • from .mymodule.main import myfunc
  • with from mymodule.main import myfunc

and run pytest with the parent directory of mymodule and tests as the current working directory.

Laurent
  • 12,287
  • 7
  • 21
  • 37