【问题标题】:Using mock.patch + parametrize in a Pytest Class Function在 Pytest 类函数中使用 mock.patch + parametrize
【发布时间】:2022-11-17 10:03:50
【问题描述】:

我一直在研究 fastAPI 并有一些异步方法来生成授权令牌

编写单元测试时出现以下错误:

TypeError: test_get_auth_token() missing 2 required positional arguments: 'test_input' and 'expected_result'

我的单元测试看起来像:

class TestGenerateAuthToken(IsolatedAsyncioTestCase):
    """
    """
    
    @pytest.mark.parametrize(
        "test_input,expected_result",
        [("user", "user_token"), ("admin", "admin_token")],
    )
    @mock.patch("myaauth.get_token", new_callable=AsyncMock)
    async def test_get_auth_token(self, get_token_mock, test_input, expected_result):
        """
        Test get_auth_header
        """
        def mock_generate_user_token(_type):
            return f"{_type}_token"

        get_token_mock.side_effect = mock_generate_user_token
        assert await myaauth.get_token(test_input) == expected_result

我知道就像删除参数一样简单,但我想知道是否可以这样做

【问题讨论】:

    标签: python python-3.x unit-testing pytest


    【解决方案1】:

    它与模拟无关。

    原因是pytest与unittest.IsolatedAsyncioTestCase不兼容。

    相反,您可以尝试使用pytest的插件,例如pytest-asyncio,让pytest使用协程测试功能。

    from unittest import mock
    from unittest.mock import AsyncMock
    
    import pytest as pytest
    
    import myaauth
    
    
    class TestGenerateAuthToken:
        @pytest.mark.parametrize(
            "test_input,expected_result",
            [("user", "user_token"), ("admin", "admin_token")],
        )
        @pytest.mark.asyncio
        @mock.patch("myaauth.get_token", new_callable=AsyncMock)
        async def test_get_auth_token(self, get_token_mock, test_input, expected_result):
            """
            Test get_auth_header
            """
    
            def mock_generate_user_token(_type):
                return f"{_type}_token"
    
            get_token_mock.side_effect = mock_generate_user_token
            assert await myaauth.get_token(test_input) == expected_result
    
    -> % python -m pytest pytest_coroutine.py
    =================================================================================================== test session starts ===================================================================================================
    platform darwin -- Python 3.10.8, pytest-7.2.0, pluggy-1.0.0
    rootdir: /Users/james/PycharmProjects/stackoverflow
    plugins: asyncio-0.20.2
    asyncio: mode=strict
    collected 2 items                                                                                                                                                                                                         
    
    pytest_coroutine.py ..                                                                                                                                                                                                  [100%]
    
    ==================================================================================================== 2 passed in 0.03s ====================================================================================================
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 2020-05-18
    相关资源
    最近更新 更多