python - 使用行为进行 API 端点测试

标签 python rest web-api-testing python-behave

我有一个 django Rest API 端点登录,它采用 json 对象形式的用户名和密码,如下所示。

   {
      username: email,
      password: password,
   }

并返回一个包含 token 的 json 对象

{
   token : 0234jh324234j2hiy342
}

现在我想编写一个行为测试。我有以下功能文件。

Feature: Login User
  By providing different credentials we check if our login API end point is working as expected or not

  Scenario: Login User by Providing Authentication Credentials
    Given I provide user authentication credentials
    Then I must get a reponse with status code 200 and a jSon object with token

下面是我的 auth.py 文件

from behave import *
import requests
import json


@given('I have user authentication credentials')
def set_impl(context):
    url = 'https://example.com/v1/login'
    headers = {'content-type': 'application/json'}
    body = {
        "username": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ce4e5e6dcf9f1fdf5f0b2fff3f1" rel="noreferrer noopener nofollow">[email protected]</a>",
        "password": "abcdef123",
    }


@when('I make an http post call')
def step_impl(context):
    context.res = requests.post(url, data=json.dumps(body), headers=headers)


@then('I must get a reponse with status code 200 and a jSon object with token')
def step_impl(context):
    assert context.res.status == 200

我无法从 @when 装饰器中的 @given 装饰器访问 url、 header 和正文。我如何根据我预期的 json 检查响应中的 json。

最佳答案

根据 @KlausD. 的建议,您应该将变量添加到行为的 context 对象中。我编辑了您的代码,将变量添加为 context 对象的属性。

from behave import *
import requests
import json


@given('I have user authentication credentials')
def set_impl(context):
    context.url = 'https://example.com/v1/login'
    context.headers = {'content-type': 'application/json'}
    context.body = {
        "username": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="08707172486d65696164266b6765" rel="noreferrer noopener nofollow">[email protected]</a>",
        "password": "abcdef123",
    }


@when('I make an http post call')
def step_impl(context):
    context.res = requests.post(context.url, data=json.dumps(context.body), headers=context.headers)


@then('I must get a reponse with status code 200 and a jSon object with token')
def step_impl(context):
    assert context.res.status == 200

至于根据预期的 JSON 检查响应中的 JSON...

  1. 查看 requests 包的 response 对象 here了解如何获取response对象的属性。

  2. 通过 open() 打开您自己的预期 JSON 文件,获取与 token 键对应的值,然后执行 assert ExpectedToken = =responseToken,或类似的东西。

关于python - 使用行为进行 API 端点测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50819491/

相关文章:

python - 如何在 python 中检查命令行参数是否为文件?

Python os.walk topdown true 与正则表达式

python - 在 Google App Engine 中创建表单自动完成

python - Elasticsearch - 如果单个术语出现在字段中,则提升该术语

java - 尝试使用 ZipInputStream 验证 com.sun.xml.internal.org.jvnet.mimepull.DataHead.ReadMultiStream

java - 无法使用 org.json.simple.JSONObject 访问其余 CXF

node.js - 努力使用 gherkin apickli 为 API 获取简单的 BDD 测试套件

json - 通过 JSON-RPC 的 NodeJS POST 请求

rest - Jmeter:- 如何在 "Jmeter"中并发/顺序发送多个请求,每个请求的值设置不同?

testing - clojure 中用于端到端测试或 api 测试的库?