I am trying to implement bdd test cases for flask application api end points. i am getting following error.
**You can implement step definitions for undefined steps with these snippets:
@then(u’the response should contain JSON with “result” equal to 12′)
def step_impl(context):
raise NotImplementedError(u’STEP: Then the response should contain JSON with “result” equal to 12′)**
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route('/api/greet', methods=['GET'])
def greet():
name = request.args.get('name', 'World')
return jsonify(message=f"Hello, {name}!")
@app.route('/api/add', methods=['POST'])
def add():
data = request.get_json()
result = data['a'] + data['b']
return jsonify(result=result)
@app.route('/api/subtract', methods=['POST'])
def subtract():
data = request.get_json()
result = data['a'] - data['b']
return jsonify(result=result)
if __name__ == '__main__':
app.run(host='0.0.0.0')
Feature file:
Feature: Flask API Endpoints
Scenario: Greet endpoint with query parameter
Given the API is running
When I send a GET request to "/api/greet" with query parameter "name" set to "Alice"
Then the response status code should be 200
And the response should contain JSON with "message" equal to "Hello, Alice!"
Scenario: Add endpoint with JSON payload
Given the API is running
When I send a POST request to "/api/add" with JSON payload:
"""
{
"a": 5,
"b": 7
}
"""
Then the response status code should be 200
And the response should contain JSON with "result" equal to 12
Scenario: Subtract endpoint with JSON payload
Given the API is running
When I send a POST request to "/api/subtract" with JSON payload:
"""
{
"a": 10,
"b": 4
}
"""
Then the response status code should be 200
Then the response should contain JSON with "result" equal to 6
Step definition:
from behave import given, when, then
import requests
import json
from flask import Flask
import threading
import time
# Import the Flask app
from app import app
# Helper function to run the Flask app in a separate thread
def start_flask_app():
app.run(port=5000)
# Start the Flask app in a separate thread
thread = threading.Thread(target=start_flask_app)
thread.daemon = True
thread.start()
# Wait a bit for the server to start
time.sleep(1)
@given('the API is running')
def step_impl(context):
response = requests.get('http://localhost:5000/api/greet')
assert response.status_code == 200
@when('I send a GET request to "{endpoint}" with query parameter "name" set to "{name}"')
def step_impl(context, endpoint, name):
context.response = requests.get(f'http://localhost:5000{endpoint}', params={'name': name})
@when('I send a POST request to "{endpoint}" with JSON payload')
def step_impl(context, endpoint):
print("ENDPOINT:{}".format(endpoint))
payload = json.loads(context.text)
context.response = requests.post(f'http://localhost:5000{endpoint}', json=payload)
@then('the response status code should be {status_code:d}')
def step_impl(context, status_code):
assert context.response.status_code == status_code
# @then('the response should contain JSON with "{key}" equal to "{value}"')
# def step_impl(context, key, value):
# response_json = context.response.json()
# if key.isdigit():
# assert response_json[key] == int(value)
# else:
# assert response_json[key] == value
@then('the response should contain JSON with "{key}" equal to "{value}"')
def step_impl(context, key, value):
response_json = context.response.json()
# Convert value to the appropriate type (e.g., int or str)
if value.isdigit():
value = int(value)
else:
try:
value = float(value)
except ValueError:
pass
assert response_json[key] == value, f"Expected {key} to be {value}, but got {response_json[key]}"
for some reason it is not able to read.
@then(‘the response should contain JSON with “{key}” equal to “{value}”‘)
and I am getting undefined step error