0

I'm trying to mock fetchone/fetchall to return different data set for different cases within a suite but it seems the first mock is global and can't be overwritten. All I get for the subsequent tests is the same value.

The first test case which sets up the mock:

def test_rejected_scenario():
    with mock.patch('psycopg2.connect') as mock_connect:
        mock_con = mock_connect.return_value
        mock_cursor = mock_con.cursor.return_value
        mock_cursor.fetchall.return_value = REQUESTS
        mock_cursor.fetchone.return_value = REQUESTS[0]
        response = handler()
        assert response["statusCode"] == 200

All the subsequent tests will get REQUESTS for fetchall() and REQUESTS[0] for fetchone() no matter how the mock is set up.

This question is about setting different return data set between test cases. Setting mock for individual case works for me.

Lys
  • 591
  • 2
  • 9
  • 19
  • Try with `temp_cursor.fetchone.return_value = None` in the test `test_request_does_not_exist_scenario()`. I don't think that **first mock** can be global! – frankfalse Dec 13 '22 at 13:41
  • just tried and same thing happened. It seems test_request_does_not_exist_scenario is using the mock previous set by test_rejected_scenario – Lys Dec 13 '22 at 19:43
  • Please give me an other possibility. Modify `test_request_does_not_exist_scenario()` defining `temp_cursor = mock_con.cursor.return_value` and `temp_cursor.fetchone.return_value = None`. – frankfalse Dec 14 '22 at 07:41
  • the same wether it's seperate variables or one liner.:( – Lys Dec 16 '22 at 20:24

0 Answers0