Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running pytest with separate test files

Tags:

python

pytest

I have a directory structure as recommended by the pytest good integration practices guide, which looks like this:

.
src/
    mypkg/
          __init__.py
          mypkg.py
tests/
      __init__.py
      test_mypkg.py

The __init__.py files are both empty, and the other files are as follows:

mypkg.py

def foo(x):
    """
    Show x.
    >>> foo(5)
    x is 5
    >>> foo("hello")
    x is hello
    """
    return "x is {0}".format(x)

test.py

from mypkg import *
def test_foo():
    assert foo(5) == "x is 5"

When I run pytest from the root directory I get:

tests\test_foo.py:1: in <module>
    from mypkg import *
E   ModuleNotFoundError: No module named 'mypkg'

What is the recommended way of setting this up?

like image 606
LondonRob Avatar asked Jul 23 '26 14:07

LondonRob


1 Answers

I am no pytest specialist but this looks like a mission for conftest.py, conftest allow one to load tests from module / package / shared fixtures and more...

See this page for more info.

like image 60
Pier Avatar answered Jul 25 '26 04:07

Pier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!