首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Behave JSONDecodeError

Python Behave JSONDecodeError
EN

Stack Overflow用户
提问于 2020-11-25 01:54:44
回答 1查看 107关注 0票数 0

我有一个包含以下场景的功能文件

代码语言:javascript
复制
      Scenario: POST Request
    And the title is "Bohemian Rhapsody"
    And the year is "2019"
    And the plot is "null"
    And the duration is "120"
    And the genres are " "
    Given the user makes a "POST" call to the "/resources/" endpoint
    Then you should receive status code "201"
    And title "Bohemian Rhapsody" should be in response body
    And year "2019" should be in response body

  Scenario: PUT Request /resources/{id}, where "id" represents the resource resource previously created
    And the title is "We will rock you"
    And the year is "2024"
    And the plot is "null"
    And the duration is "120"
    And the audio qualities are " "
    And the video qualities are " "
    And the genres are " "
    Given the user makes a "PUT" call to the "/resources/" endpoint
    Then you should receive status code "204"
    And title "We will rock you" should be in response body
    And year "2024" should be in response body

第二个场景依赖于第一个响应的"id“来更新先前创建的资源。具体步骤如下:

代码语言:javascript
复制
    @given('the user makes a "{method}" call to the "{endpoint}" endpoint')
def execute_request_to_endpoint(context, method, endpoint):
    global json_response
    if method == 'GET':
        response = requests.get(endpoint)
    elif method == 'POST':
        response = requests.post(endpoint, json=context.request_body)
    elif method == 'PUT':
        print(json_response)
        new_json_response = json.loads(json_response)
        movie_id = new_json_response['id']
        print(endpoint + str(movie_id))
        context.request_body = str(context.request_body).replace("\'", "\"")
        response = requests.put(endpoint + str(movie_id), json=context.request_body)
    context.response_body = response.json()
    json_response = json.dumps(context.response_body)
    context.status_code = response.status_code


@then('you should receive status code "{status_code}"')
def check_status_code(context, status_code):
    assert context.status_code == int(status_code)


@step('the title is "{title}"')
def set_title(context, title):
    context.request_body = {"title": title}


@step('the year is "{year}"')
def set_year(context, year):
    context.request_body['year'] = int(year)


@step('the plot is "{plot}"')
def set_plot(context, plot):
    context.request_body['plot'] = plot


@step('the duration is "{duration}"')
def set_duration(context, duration):
    context.request_body['duration'] = int(duration)


@step('the genres are "{genres}"')
def set_genres(context, genres):
    context.request_body['genres'] = genres


@step('title "{title}" should be in response body')
def check_title(context, title):
    assert context.response_body['title'] == title


@step('year "{year}" should be in response body')
def check_year(context, year):
    assert context.response_body['year'] == int(year)

我得到了一个类似这样的错误,simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0),尽管我已经进行了多次尝试,但从未成功完成第二个场景中的第二个请求。出于调试的目的,我可以看到我能够打印第一次调用{"id": 5000, "title": "Bohemian Rhapsody", "year": 2019, "plot": "null", "duration": 120}的响应正文,并且在打印它时我还能够获取端点/resources/5000。所以我正在做PUT请求,但是我得到了上面的错误。我不明白为何只有第二项要求才有问题,而第一项要求却没有,因为两者是相似的。

EN

回答 1

Stack Overflow用户

发布于 2020-11-25 02:13:50

这些类型的测试相互隔离地运行。因此,您不能基于上一次测试的响应来进行测试。相反,您可以在第二个测试中添加额外的POST请求

代码语言:javascript
复制
  Scenario: PUT Request /resources/{id}, where "id" represents the resource resource previously created
    And the title is "We will rock you"
    And the year is "2024"
    And the plot is "null"
    And the duration is "120"
    And the audio qualities are " "
    And the video qualities are " "
    And the genres are " "
    Given the user makes a "POST" call to the "/resources/" endpoint
    Then you should receive status code "201"
    Given the user makes a "PUT" call to the "/resources/" endpoint
    Then you should receive status code "204"
    And title "We will rock you" should be in response body
    And year "2024" should be in response body
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64992179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档