0

I have a feature file with the following scenarios

      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

The second scenario relies on the "id" of the first response in order to update the resource previously created. The steps are below:

    @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)

I get an error like this simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0) even though I already did many attempts but never managed to complete successfully the second request in the second scenario. For debugging purposes I can see that I am able to print the response body from the first call {"id": 5000, "title": "Bohemian Rhapsody", "year": 2019, "plot": "null", "duration": 120} and I am also able to grab the endpoint /resources/5000 when printing it. So I am doing the PUT request but then I get the above error. I do not understand why the issue is only with the second request and not with the first since they are similar.

6
  • When you're doing the PUT call, what does it print for json_response? Commented Nov 24, 2020 at 17:59
  • 1
    It doesn't look like you set the json_response anywhere Commented Nov 24, 2020 at 18:00
  • I see the error on this step Given the user makes a "PUT" call to the "/resources/" endpoint. I am not able to see the value of json_response after the call. Before the call the json_response looks like {"id": 3428, "title": "Bohemian Rhapsody", "year": 2019, "plot": "null", "duration": 120} Commented Nov 24, 2020 at 18:06
  • Hi @C_Z_ I tried your approach (below) but I am still unable to perform the request and still bumping into the same issue simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0). I really don't get it as I am being able to print both the id to append to the url and the request_body and they look ok to me. This is the request body {"title": "We will rock you", "year": 2024, "plot": "null", "duration": 120} Commented Nov 25, 2020 at 17:26
  • Which line is throwing the error? Commented Nov 25, 2020 at 18:23

1 Answer 1

0

These types of tests run in isolation from each other. So you can't base a test off of the response from a previous test. Instead, you can add an additional POST request inside your second test

  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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.