0

Based on the following example:

app = Flask(__name__)

@app.route('/users')
def get_users():
    return UsersAPI().get_users()

And the following tests (using pytest and pytest-mock):

@pytest.fixture
def users():
    return UsersAPI(how_many=1)


def test_simple(users, mocker):
    mocker.patch("???", return_value=users)

I simply want to call UsersAPI(how_many=1) instead of UsersAPI(). Is this possible to do?

(if you know how to get done with unittest.mock that is also fine since pytest-mock is simply some pytest wrapper)

renatodamas
  • 16,555
  • 8
  • 30
  • 51

1 Answers1

1

Turns out it is as easy as:

@pytest.fixture
def users():
    return UsersAPI(how_many=1)


def test_simple(users, mocker):
    mocker.patch("path.to.module.UsersAPI", return_value=users)

And this also works:

mocker.patch.object(path.to.module, 'UsersAPI', return_value=users)
renatodamas
  • 16,555
  • 8
  • 30
  • 51
  • Yeah, I realized that this is the correct answer after a night of sleep... shouldn't write answers while sleep-depreviated. – MrBean Bremen Feb 19 '22 at 09:48