标签: python-responses

How to get JSON.loads() output in dictionary type but not string type using PYTHON

When I make use of JSON.dump() I am getting below JSON format

Dumps data"b'{\"AutomaticReadabilityIndex\":2.7999999999999994,\"AgeLevel\":[\" 11 to 12\"],\"Statement\":[\"Nice. Your grade is about six\"],\"SpacheScore\":1.877,\"GunningFogScore\":9.099999999999998,\"SmogIndex\":5.999999999999999}'"
Run Code Online (Sandbox Code Playgroud)

When I make use of JSON.loads() I am getting below JSON format with bytes

loads data b'{"AutomaticReadabilityIndex":2.7999999999999994,"AgeLevel":[" 11 to 12"],"Statement":["Nice. Your grade is about six"],"SpacheScore":1.877,"GunningFogScore":9.099999999999998,"SmogIndex":5.999999999999999}'
Run Code Online (Sandbox Code Playgroud)

My question is when I am using loads format the output has to be in dictionary type but I don't know why I am getting string type as my output. How to convert …

json python-3.x python-requests python-responses

5
推荐指数
1
解决办法
8658
查看次数

python 响应 - 并非所有请求都已执行

我正在尝试测试用例来模拟 api 调用,并使用 python 响应来模拟 api 调用。

下面是我的模拟,

with responses.RequestsMock() as rsps:
    url_re = re.compile(r'.*udemy.com/api-2.0/courses.*')           
    url_re = re.compile(r'https://www.udemy.com/api-2.0/courses')
    rsps.add(
        responses.GET, url_re,
        body=mocked_good_json, 
        status=200,
        content_type='application/json',
        match_querystring=True
    )
    courses = self.app.courses.get_all(page=1, page_size=2)         
    for course in courses:              
        self.assertTrue(isinstance(course, Course))
        self.assertTrue(hasattr(course, 'id'))
        self.assertTrue(hasattr(course, 'title'))           
        self.assertIsNotNone(course.id)        
Run Code Online (Sandbox Code Playgroud)

当我执行这个模拟时,我收到此错误 -

AssertionError: Not all requests have been executed [(u'GET', 'https://www.udemy.com/api-2.0/courses/')]
Run Code Online (Sandbox Code Playgroud)

当我删除mock并直接调用api时,它工作正常。

关于我的模拟失败的原因有什么意见吗?

错误信息 -

======================================================================
FAIL: test_get_all_courses (tests.test_courses.TestApiCourses)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/rem/git/udemy/tests/test_courses.py", line 136, in test_get_all_courses
    courses = self.app.courses.get_all(page=1, page_size=2)
  File "/Users/rem/.virtualenvs/udemyapp/lib/python2.7/site-packages/responses.py", line 536, …
Run Code Online (Sandbox Code Playgroud)

python mocking python-responses

5
推荐指数
1
解决办法
3530
查看次数

如何删除 pytest 的重复响应?

响应库提供请求的模拟。就我而言,它通常看起来像这样:

import responses

@responses.activate
def test_foo():
    # Add mocks for service A
    responses.add(responses.POST, 'http://service-A/foo', json={'bar': 'baz'}, status=200)
    responses.add(responses.POST, 'http://service-A/abc', json={'de': 'fg'}, status=200)


@responses.activate
def test_another_foo():
    # Add mocks for service A
    responses.add(responses.POST, 'http://service-A/foo', json={'bar': 'baz'}, status=200)
    responses.add(responses.POST, 'http://service-A/abc', json={'de': 'fg'}, status=200)


Run Code Online (Sandbox Code Playgroud)

如何避免这种代码重复?

我很想有一个mock_service_a固定装置或类似的东西。

pytest python-responses

3
推荐指数
1
解决办法
400
查看次数