pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.85k stars 2.64k forks source link

pytest runs forever #9466

Closed shakori999 closed 2 years ago

shakori999 commented 2 years ago

I'm working on a django channel project, i did more than 10 tests it works fine, but the last one is running forever. i wanna make a test for if a driver can update the trip or not(code of the test under the output) ,

the output of pip list is

Package                       Version
----------------------------- -------
aioredis                      1.3.1  
asgiref                       3.4.1  
async-timeout                 4.0.2  
atomicwrites                  1.4.0  
attrs                         21.2.0 
autobahn                      21.11.1
Automat                       20.2.0 
autopep8                      1.6.0  
Babel                         2.9.1  
cffi                          1.15.0 
channels                      3.0.4  
channels-redis                3.3.1
colorama                      0.4.4
constantly                    15.1.0
cryptography                  36.0.1
daphne                        3.0.2
Django                        3.2.9
django-dotenv                 1.4.2
django-environ                0.8.1
django-filter                 21.1
django-guardian               2.4.0
django-money                  2.1
djangorestframework           3.12.4
djangorestframework-jwt       1.11.0
djangorestframework-simplejwt 5.0.0
hiredis                       2.0.0
hyperlink                     21.0.0
idna                          3.3
incremental                   21.3.0
iniconfig                     1.1.1
msgpack                       1.0.3
packaging                     21.3
Pillow                        8.4.0
pip                           21.3.1
pluggy                        1.0.0
psycopg                       3.0.2
psycopg2-binary               2.9.2
py                            1.11.0
py-moneyed                    1.2
pyasn1                        0.4.8
pyasn1-modules                0.2.8
pycodestyle                   2.8.0
pycparser                     2.21
PyJWT                         2.3.0
pyOpenSSL                     21.0.0
pyparsing                     3.0.6
pytest                        6.2.5
pytest-asyncio                0.16.0
pytest-django                 4.5.2
pytest-timeout                2.0.2
pytz                          2021.3
service-identity              21.1.0
setuptools                    58.3.0
six                           1.16.0
sqlparse                      0.4.2
toml                          0.10.2
Twisted                       21.7.0
twisted-iocpsupport           1.0.2
txaio                         21.2.1
typing_extensions             4.0.1
wheel                         0.37.0
zope.interface                5.4.0

pytest and operation system -v

platform win32 -- Python 3.10.1, pytest-6.2.5, py-1.11.0, pluggy-1.0.0

this is my test in test_websocket.py

@database_sync_to_async
def create_user(username, password, group='rider'):
    user = get_user_model().objects.create_user(
        username=username,
        password=password
    )
    user_group, _ = Group.objects.get_or_create(name=group)
    user.groups.add(user_group)
    user.save()
    access = AccessToken.for_user(user)
    return user, access

class TestWebSocket:
        async def test_driver_can_update_trip(self, settings):
                settings.CHANNEL_LAYERS = TEST_CHANNEL_LAYERS

                # Create trip request.
                rider, _ = await create_user(
                    'test.rider@example.com', 'pAssw0rd', 'rider'
                )
                trip = await create_trip(rider=rider)
                trip_id = f'{trip.id}'

                # Listen for messages as rider.
                channel_layer = get_channel_layer()
                await channel_layer.group_add(
                    group=trip_id,
                    channel='test_channel'
                )

                # Update trip.
                driver, access = await create_user(
                    'test.driver@example.com', 'pAssw0rd', 'driver'
                )
                communicator = WebsocketCommunicator(
                    application=application,
                    path=f'/taxi/?token={access}'
                )
                connected, _ = await communicator.connect()
                message = {
                    'type': 'update.trip',
                    'data': {
                        'id': trip_id,
                        'pick_up_address': trip.pick_up_address,
                        'drop_off_address': trip.drop_off_address,
                        'status': Trip.IN_PROGRESS,
                        'driver': driver.id,
                    },
                }
                await communicator.send_json_to(message)

                # Rider receives message.
                response = await channel_layer.receive('test_channel')
                response_data = response.get('data')
                assert response_data['id'] == trip_id
                assert response_data['rider']['username'] == rider.username
                assert response_data['driver']['username'] == driver.username

                await communicator.disconnect()

and this is my consumer.py file

@database_sync_to_async
    def _update_trip(self, data):
        instance = Trip.objects.get(id=data.get('id'))
        serializer = TripSerializer(data=data)
        serializer.is_valid(raise_exception=True)
        return serializer.update(instance, serializer.validated_data)

class TaxiConsumer(AsyncJsonWebsocketConsumer):
        async def receive_json(self, content, **kwargs):
                message_type = content.get('type')
                if message_type == 'create.trip':
                    await self.create_trip(content)
                elif message_type == 'echo.message':
                    await self.echo_message(content)
                elif message_type == 'update.trip':
                    await self.echo_message(content)

        @database_sync_to_async
        def create_user(username, password, group='rider'):
            user = get_user_model().objects.create_user(
                username=username,
                password=password
            )
            user_group, _ = Group.objects.get_or_create(name=group)
            user.groups.add(user_group)
            user.save()
            access = AccessToken.for_user(user)
            return user, access

when i run pytest it stills like this

=========== test session starts ===========
platform win32 -- Python 3.10.1, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
django: settings: taxi.settings (from ini)
rootdir: C:\Users\shako\Desktop\web_devolper\driver_app, configfile: pytest.ini
plugins: asyncio-0.16.0, django-4.5.2, timeout-2.0.2
collected 1 item

trips\tests\test_websocket.py 
The-Compiler commented 2 years ago

If you launch pytest with -o faulthandler_timeout=5 (adjust seconds as needed), it should give you a trackeback of the hang. Where does that point to? FWIW I doubt this is a bug in pytest.

shakori999 commented 2 years ago

this is the output

(django-taxi-app) PS C:\Users\shako\Desktop\taxi-app\server\driver_app\taxi> pytest -o faulthandler_timeout=5
=================================================== test session starts ===================================================
platform win32 -- Python 3.7.3, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
django: settings: taxi.settings (from ini)
rootdir: C:\Users\shako\Desktop\taxi-app, configfile: pytest.ini     
plugins: asyncio-0.16.0, django-4.5.2
collected 14 items

trips\tests\test_http.py ....                                                                                        [ 28%]
trips\tests\test_websocket.py .........Timeout (0:00:05)!
Thread 0x00002ff0 (most recent call first):
  File "c:\users\shako\appdata\local\programs\python\python37\lib\concurrent\futures\thread.py", line 78 in _worker
  File "c:\users\shako\appdata\local\programs\python\python37\lib\threading.py", line 865 in run
  File "c:\users\shako\appdata\local\programs\python\python37\lib\threading.py", line 917 in _bootstrap_inner
  File "c:\users\shako\appdata\local\programs\python\python37\lib\threading.py", line 885 in _bootstrap

Thread 0x00001558 (most recent call first):
  File "c:\users\shako\appdata\local\programs\python\python37\lib\selectors.py", line 314 in _select
  File "c:\users\shako\appdata\local\programs\python\python37\lib\selectors.py", line 323 in select
  File "c:\users\shako\appdata\local\programs\python\python37\lib\asyncio\base_events.py", line 1739 in _run_once
  File "c:\users\shako\appdata\local\programs\python\python37\lib\asyncio\base_events.py", line 539 in run_forever
  File "c:\users\shako\appdata\local\programs\python\python37\lib\asyncio\base_events.py", line 571 in run_until_complete   
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pytest_asyncio\plugin.py", line 195 in inner
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\_pytest\python.py", line 183 in pytest_pyfunc_call
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pluggy\_callers.py", line 39 in _multicall 
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pluggy\_manager.py", line 80 in _hookexec  
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pluggy\_hooks.py", line 265 in __call__    
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\_pytest\python.py", line 1641 in runtest
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\_pytest\runner.py", line 162 in pytest_runtest_call
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pluggy\_callers.py", line 39 in _multicall 
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pluggy\_manager.py", line 80 in _hookexec  
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pluggy\_hooks.py", line 265 in __call__
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\_pytest\runner.py", line 255 in <lambda>   
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\_pytest\runner.py", line 311 in from_call  
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\_pytest\runner.py", line 255 in call_runtest_hook
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\_pytest\runner.py", line 215 in call_and_report
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\_pytest\runner.py", line 126 in runtestprotocol
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\_pytest\runner.py", line 109 in pytest_runtest_protocol
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pluggy\_callers.py", line 39 in _multicall 
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pluggy\_manager.py", line 80 in _hookexec  
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pluggy\_hooks.py", line 265 in __call__    
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\_pytest\main.py", line 348 in pytest_runtestloop
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pluggy\_callers.py", line 39 in _multicall
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pluggy\_manager.py", line 80 in _hookexec  
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pluggy\_hooks.py", line 265 in __call__    
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\_pytest\main.py", line 323 in _main        
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\_pytest\main.py", line 269 in wrap_session 
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\_pytest\main.py", line 316 in pytest_cmdline_main
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pluggy\_callers.py", line 39 in _multicall 
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pluggy\_manager.py", line 80 in _hookexec  
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\pluggy\_hooks.py", line 265 in __call__    
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\_pytest\config\__init__.py", line 163 in main
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\lib\site-packages\_pytest\config\__init__.py", line 185 in console_main
  File "C:\Users\shako\Desktop\taxi-app\server\django-taxi-app\Scripts\pytest.exe\__main__.py", line 7 in <module>
  File "c:\users\shako\appdata\local\programs\python\python37\lib\runpy.py", line 85 in _run_code
  File "c:\users\shako\appdata\local\programs\python\python37\lib\runpy.py", line 193 in _run_module_as_main
RonnyPfannschmidt commented 2 years ago

according to the traceback, it seems like you have a network hangup thats not running to completion and is not wrapped with a timeout/cancelation

The-Compiler commented 2 years ago

Taking the freedom to convert this to a discussion, as it indeed doesn't look like a bug in pytest.