Python: How to mock a value at import time?

Viewed 34

I would like to test a class (A) that extends another class (B). For my test I would like to mock class B to modify the behaviour of A but in order to do that I need to be able to apply the mock at import time. How would I do that? Please see below.

database.py

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

models.py

from .database import db

class Meta(db.Model):
    ...

In my unit test, I would like Meta to extend the standard python declarative Base so essentially I would like to mock database.py to instead act as if I'd done something like this:

db.Model = declarative_base()

My test currently looks like test_model.py

from pkg.models import Meta

test_model()
    assert(...)

Thanks for the help.

0 Answers
Related