Martiusweb / asynctest

Enhance the standard unittest package with features for testing asyncio libraries
https://asynctest.readthedocs.org/
Apache License 2.0
309 stars 41 forks source link

Different tests using Same Magic Mock Instance #127

Open vinay0410 opened 5 years ago

vinay0410 commented 5 years ago

Hi @Martiusweb , First of all thanks for this library. This has really helped in unit testing with aiohttp. I faced an issue while running parameterized testing using ddt.

Let's say I have following tests:

# test.py
from ddt import ddt, data, unpack
import unittest
import os
import asynctest
import asyncio

@ddt
class Data(asynctest.TestCase):

    def setUp(self):
        pass

    @data(({'a': 'b'}, '54759eb3c090d83494e2d804'), ({'a': 1}, '54759eb3c090d83494e2d804'))
    @unpack
    @unittest.mock.patch('os.getcwd')
    async def test_func_one(self, val1, val2, mock_os):
        asyncio.sleep(1)
        print(mock_os)
        print(val1, val2)

    @data((1, 1), (2, 2))
    @unpack
    @unittest.mock.patch('os.getcwd')
    async def test_func_two(self, val1, val2, mock_os):
        print(mock_os)
        print(val1, val2)

I get the following output:

================================================================ test session starts =================================================================
platform linux -- Python 3.6.7, pytest-4.2.0, py-1.8.0, pluggy-0.11.0 -- /home/vinaysharma/work/new/survey/venv/bin/python3
rootdir: /home/vinaysharma/work/new/survey, inifile: pytest.ini
plugins: cov-2.6.1, asyncio-0.10.0, aiohttp-0.3.0
collected 4 items                                                                                                                                    

check_ddt.py::Data::test_func_one_1 
------------------------------------------------------------------- live log call --------------------------------------------------------------------
selector_events.py          65 DEBUG    Using selector: EpollSelector
<MagicMock name='getcwd' id='140009124050536'>
{'a': 'b'} 54759eb3c090d83494e2d804
PASSED
check_ddt.py::Data::test_func_one_2 
------------------------------------------------------------------- live log call --------------------------------------------------------------------
selector_events.py          65 DEBUG    Using selector: EpollSelector
<MagicMock name='getcwd' id='140009124050536'>
{'a': 1} 54759eb3c090d83494e2d804
PASSED
check_ddt.py::Data::test_func_two_1 
------------------------------------------------------------------- live log call --------------------------------------------------------------------
selector_events.py          65 DEBUG    Using selector: EpollSelector
<MagicMock name='getcwd' id='140009121942720'>
{'a': 'b'} 54759eb3c090d83494e2d804
PASSED
check_ddt.py::Data::test_func_two_2 
------------------------------------------------------------------- live log call --------------------------------------------------------------------
selector_events.py          65 DEBUG    Using selector: EpollSelector
<MagicMock name='getcwd' id='140009121998384'>
{'a': 1} 54759eb3c090d83494e2d804
PASSED

============================================================== 4 passed in 0.05 seconds ==============================================================

Run Using following command: pytest -p no:cacheprovider -s test.py

As you can see ddt adds more functions to the class, causing the total number of functions to be 4. But the first and second function both have same MagicMock Instance, which causes problems with self.assert_called_once().

I have validated that this is not an issue with ddt, because all they do is add new functions to the class.

Also, this error only occurs if using the async keyword before def.

Replicated on asynctest 0.13.0 and 0.12.4.

I would be very grateful if you could look into it.