AndreyErmilov / pytest-mock-server

Mock server plugin for pytest
MIT License
27 stars 5 forks source link

Waring `KeyError: 'WERKZEUG_SERVER_FD'` #11

Open CorrectRoadH opened 1 year ago

CorrectRoadH commented 1 year ago
=========================================================================================== warnings summary ===========================================================================================
test/video_download/test_download_video.py::TestVideoDownload::test_fetch_bilibili_video_info
  /Users/ctrdh/.pyenv/versions/3.8.16/lib/python3.8/site-packages/_pytest/threadexception.py:73: PytestUnhandledThreadExceptionWarning: Exception in thread Thread-1

  Traceback (most recent call last):
    File "/Users/ctrdh/.pyenv/versions/3.8.16/lib/python3.8/threading.py", line 932, in _bootstrap_inner
      self.run()
    File "/Users/ctrdh/.pyenv/versions/3.8.16/lib/python3.8/threading.py", line 870, in run
      self._target(*self._args, **self._kwargs)
    File "/Users/ctrdh/.pyenv/versions/3.8.16/lib/python3.8/site-packages/flask/app.py", line 889, in run
      run_simple(t.cast(str, host), port, self, **options)
    File "/Users/ctrdh/.pyenv/versions/3.8.16/lib/python3.8/site-packages/werkzeug/serving.py", line 1073, in run_simple
      fd = int(os.environ["WERKZEUG_SERVER_FD"])
    File "/Users/ctrdh/.pyenv/versions/3.8.16/lib/python3.8/os.py", line 675, in __getitem__
      raise KeyError(key) from None
  KeyError: 'WERKZEUG_SERVER_FD'

    warnings.warn(pytest.PytestUnhandledThreadExceptionWarning(msg))

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
CleanShot 2023-08-01 at 12 43 30@2x

my test code

import pytest
import os
from script.main import FetchVideoInfo, DownloadVideo, Rename

# fix http server mock Warning
os.environ["WERKZEUG_RUN_MAIN"] = "true"  <- I hope it can fix the warning, but it didn't working.

class TestVideoDownload:
    @pytest.mark.server(url='/api/update', response=[{'data': 'update video status'}], method='POST')
    def test_fetch_bilibili_video_info(self):
        # to mock a http service

        fixture_storage = ""
        # single video
        video_info_array = FetchVideoInfo("https://www.bilibili.com/video/BV1ZW411u7V3")
        assert len(video_info_array) == 1

        # # playlist
        # video_info_array = FetchVideoInfo("https://www.bilibili.com/video/BV1SW4y1v7WN")
        # assert len(video_info_array) == 3

        # # playlist with 3 episode
        # video_info_array = FetchVideoInfo("https://www.bilibili.com/video/BV15V411t7uW")
        # assert len(video_info_array) == 4

    # def test_fetch_youtube_video_info(self):
    #     assert 1

    # def test_download_video(self):
    #     print("test_download_video")
    #     assert 1
CorrectRoadH commented 1 year ago

I fix it by installing flask and werkzeug == 2.0.0. But I got a new problem😭

test/video_download/test_download_video.py:10
  /Users/ctrdh/Code/Media-Fetch-Pro/test/video_download/test_download_video.py:10: PytestUnknownMarkWarning: Unknown pytest.mark.server_settings - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.server_settings(port=7789)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html