Is there a way to instruct pytest to skip the module definition?

Viewed 38

Is there a way to tell pytest to mock out any __init__.pys, or otherwise skip them?

I have a python module with a global dependency that gets introduced via a Docker container. Here's an example:

/mymodule
  /__init__.py
  mymodule.py
  mymodule_test.py

mymodule/__init__.py looks like:

from .mymodule import mymodule

And mymodule/mymodule.py looks like:

import globalpackage

def mymodule():
  pass

Finally, we have a Dockerfile that looks like:

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8
COPY /some/path/to/globalpackage /app/globalpackage
COPY mymodule /app

So that, at runtime, globalpackage is available as an import. I can't change this structure as it's used across our codebase.

The issue I'm running into is in trying to write a test for this module. It seems that pytest hits the module __init__.py before it hits any tests. This means I'm unable to mock or patch globalpackage (which doesn't exist when running pytest at the command line).

I can't for the life of me figure out a way of skipping over __init__.py in this scenario. Ideally I'd like to avoid modifying the src code, as that will lead to a massive refactor.

0 Answers
Related