Is it possible to create anonymous objects in Python?

前端 未结 11 585
余生分开走
余生分开走 2020-12-14 13:58

I\'m debugging some Python that takes, as input, a list of objects, each with some attributes.

I\'d like to hard-code some test values -- let\'s say, a list of four

11条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 14:35

    This is how I did it:

    from mock import patch
    import requests
    
    class MockResponse:
    
        def __init__(self, text, status_code):
            self.text = text
            self.status_code = status_code
    
    
    class TestSomething(unittest.TestCase):
    
        @patch('requests.get',return_value=MockResponse('the result',200))
        def test_some_request(self, *args, **kwargs):
            response = requests.get('some.url.com')
            assert response.text=='the result'
            assert response.status_code=='200'
    

提交回复
热议问题