Hi
I mock connect to Postgresql(psycopg2) in two tests
In first test set data in side_effect, in second using return_value:
One by one works fine.
If I run
'python -m unittest discover -s test'
First test(side_effect) works fine, second test failed, StopIteration. In second test mock-object trying get data from side_effect from first test. I'm trying set side_effect in both test, the same result, StopIteration exception in second test. I'm trying ti give different names(name=) to patch and MagicMock, the same result.
How can I mock one object(psycopg2.pool.ThreadedConnectionPool) and one function(fetchall) mock in several test using 'python -m unittest discover'? It's possible with unittest.mock?
I mock connect to Postgresql(psycopg2) in two tests
1 2 3 4 |
from unittest.mock import patch, MagicMock with patch( 'psycopg2.pool.ThreadedConnectionPool' ) as mock_connect: mock_connect().getconn.return_value.cursor.return_value = mock_cursor |
1 2 3 4 5 6 7 |
# first test mock_cursor = MagicMock() mock_cursor.fetchall.side_effect = mock_data # second test mock_cursor = MagicMock() mock_cursor.fetchall.return_value = data |
If I run
'python -m unittest discover -s test'
First test(side_effect) works fine, second test failed, StopIteration. In second test mock-object trying get data from side_effect from first test. I'm trying set side_effect in both test, the same result, StopIteration exception in second test. I'm trying ti give different names(name=) to patch and MagicMock, the same result.
How can I mock one object(psycopg2.pool.ThreadedConnectionPool) and one function(fetchall) mock in several test using 'python -m unittest discover'? It's possible with unittest.mock?