witty-laptop-37343
03/23/2023, 3:18 AMunittest.mock.patch
to mock, but it works when testing with pytests but not pants, I am wondering if there’s extra setup required to mock external apis with pants
conftest.py
@pytest.fixture(scope="session")
def mock_api():
mock_get_response_patcher = patch("some-api")
mock_get_response = mock_get_response_patcher.start()
mock_get_response.return_value = {....}
Then in my test, I have
A.py
def test_A(..., mock_api):
.....
irrelevant code
.....
I have verified the mock_api function is called correctly, but when I test it with pants, the mock function is called, but the mock_get_response
value is not what I expected, and it will actually call the mocked function instead of returning the value I set it to be.
However, when testing it with pytest, it works perfectly(it actually mocks), so I wonder if I had missed anything?enough-analyst-54434
03/23/2023, 3:33 AMwitty-laptop-37343
03/23/2023, 3:37 AMpytest A.py
and
./pants test --force A.py
to run a single file though, why is there differenceenough-analyst-54434
03/23/2023, 3:39 AMpytest
you're running bare vs the version pants uses. Perhaps there is a relevant difference.witty-laptop-37343
03/26/2023, 3:53 PM