I'm writing a framework for RESTful API test automation, I already decided to go with REST Assured, I'm not 100% sure about add a layer to allow define tests using a domain specific language like Gherkin, therefore adding a BDD framework like Cucumber.
What is your opinion?
Is a good approach to use BDD in API automation testing?
我正在使用Robot打HTTP服务。但这向我展示了以下问题
找不到名称为“创建会话”的关键字。
导入测试库'RequestsLibrary'失败:ImportError:没有名为RequestsLibrary Traceback的模块(最近一次调用):
我已经安装了RequestsLibrary。我的TC是:
*** Settings ***
Library Collections
Library String
#Library RequestsLibrary
Library OperatingSystem
Library ExtendedRequestsLibrary
Suite Teardown Delete All Sessions
*** Test Cases ***
Get Requests
[Tags] get
Create Session google http://www.google.com
# Create Session github https://api.github.com
${resp}= Get google /
Should Be Equal As Strings ${resp.status_code} 200
${resp}= Get github /users/bulkan
Should Be Equal As Strings ${resp.status_code} 200
Dictionary Should Contain Value ${resp.json()} Bulkan Evcimen
Run Code Online (Sandbox Code Playgroud) 如何从另一个 GET API 的预请求脚本选项卡调用 POST API 请求(具有用户名和密码字段的请求正文的登录 API),它在其请求 url 中使用来自上述 API 正文的令牌。
登录API:POST方法;请求体:用户名和密码;响应主体:令牌。获取客户记录 API : GET 方法;请求 URI : /token/
只想在 Postman 的一个测试中涵盖这个端到端场景。任何人都可以帮我预请求脚本吗?我应该如何调用登录 API?
语境
我正在尝试为查询外部 API 的函数编写测试。这些函数向 API 发送请求、获取响应并处理它们。在我的测试中,我想使用本地运行的模拟服务器来模拟外部 API。到目前为止,模拟服务器已成功运行并响应自定义 GET 查询。
问题
外部 API 使用 type 的对象进行响应<class 'dict'>,而显然我可以从模拟服务器获得的只是 type 的响应<class 'bytes'>。模拟服务器从磁盘获取预定义的数据并通过流返回它们。由于我无法模拟外部 API,因此我的测试会因响应类型错误而抛出错误消息。
以下是我的代码片段和一些解释。
1.setUp ()函数:
setUp 函数在测试套件的开头运行。它负责在运行测试之前配置和运行服务器:
def setUp(self):
self.factory = APIRequestFactory()
# Configuring the mock server
self.mock_server_port = get_free_port()
self.mock_server = HTTPServer(('localhost', self.mock_server_port), MockServerRequestHandler)
# Run the mock server in a separate thread
self.mock_server_thread = Thread(target=self.mock_server.serve_forever)
self.mock_server_thread.setDaemon(True)
self.mock_server_thread.start()
Run Code Online (Sandbox Code Playgroud)
2. MockServerClassHandler:
class MockServerRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
if re.search(config.SYSTEM_STATUS_PATTERN, self.path):
# Response status code
self.send_response(requests.codes.ok)
# Response headers
self.send_header("Content-Type", "application/json; …Run Code Online (Sandbox Code Playgroud) 我使用 Karate 和 RestAssured 一段时间了。当然,这两种工具都有优点和缺点。现在我有一个 RestAssured 项目,其中有请求和响应对象以及 POJO。我的请求包装我的端点并将我的 POJO 发送到这些端点。我在抽象层中完成所有标题等配置。如果我需要覆盖它们,我会在测试期间覆盖它们。如果没有,我需要两行代码来触发端点。
我处理 edpoint 的快乐路径和消极路径的方法是,在每次测试之前使用构造函数中的新值初始化 POJO。然后我在测试范围中覆盖我想要的值。例如,如果我想测试密码字段的负大小写,我在测试过程中将此字段设置为空字符串。但其他字段在测试之前已经设置为一些随机的东西。
但我不知道如何用空手道实现这一点。
Karate 允许我创建请求正文的 JSON 表示形式并定义参数,如下例所示。
{
"firstName": "<name>",
"lastName": "<lastName>",
"email": "<email>",
"role": <role>
}
Run Code Online (Sandbox Code Playgroud)
然后在每次测试中我都必须用一些数据填充所有字段。
|token |value|
|name |'canberk'|
|lastName |''|
|email |'canberk@blbabla.com'|
|role |'1'|
Run Code Online (Sandbox Code Playgroud)
和
|token |value|
|name |''|
|lastName |'akduygu'|
|email |'canberk@blbabla.com'|
|role |'1'|
Run Code Online (Sandbox Code Playgroud)
就这样继续下去。
4 个字段的 JSON 主体没问题,但是当主体开始拥有超过 20 个字段时,为每个测试初始化每个字段就变得很痛苦。
空手道是否有办法通过我需要提出解决方案的预定义步骤来解决这个问题?
我需要验证像下面这样的响应是否包含一些字段。我对字段值不感兴趣 - 只是对键存在感兴趣。例如,我想检查这种类型的响应中是否存在键“id”。我将如何实现这一目标?
[
{
"id":"1",
"title":"Title",
"details":"details",
"benefit":"Welcome",
"expirationTimestamp":1549995900,
"notice":"some text",
}
]
Run Code Online (Sandbox Code Playgroud)
如果我做
given()
.spec(reqSpec).
when()
.get().
then()
.body("$", hasKey("id"));
Run Code Online (Sandbox Code Playgroud)
我收到这样的错误:
java.lang.AssertionError: 1 expectation failed.
JSON path $ doesn't match.
Expected: map containing ["id"->ANYTHING]
Actual: [{blabla=something, id=1, details=details, etc=etc}]
Run Code Online (Sandbox Code Playgroud)
请问,有人可以向我解释一下这应该如何工作吗?
json jsonpath rest-assured web-api-testing rest-assured-jsonpath
我正在使用 Cypress 进行一些 API 测试,但我很难访问 JSON 响应正文中的值;但是我可以对身体进行断言,这表明它正确地接收了它。
下面我试图分配 JSON 正文(response.body),然后从中获取 'id' 的值:
describe('Creating a board', () => {
it('should create a board', () => {
cy.request({
method : 'POST',
url:`${requestUrl}/boards/`,
qs: {
name : "test-board",
token : token,
key : key
}
}).then((response) => {
expect(response).property('status').to.equal(200)
expect(response.body).property('id').to.not.be.oneOf([null, ""])
const body = (response.body)
boardId = body['id']
})
})
Run Code Online (Sandbox Code Playgroud)
我已经做了很多搜索,但找不到具体的方法来做到这一点。任何帮助,将不胜感激...
我按照这些指南开始为我们的API编写测试
不幸的是,文件夹tests/api中没有名为ApiTester.php的文件,所以正在运行
php codecept.phar run
Run Code Online (Sandbox Code Playgroud)
给我这样的错误:
[PHPUnit_Framework_Exception]
fopen(/Users/jj/Development/codeception/tests/api/ApiTester.php): failed to open stream: No such file or directory
Run Code Online (Sandbox Code Playgroud) 我正在为 post api 编写测试,它返回创建的资源。但是如何将这些数据传递给 python 中的夹具,以便在测试完成后进行清理
清理:
@pytest.fixture(scope='function')
def delete_after_post(request):
def cleanup():
// Get ID of resource to cleanup
// Call Delete api with ID to delete the resource
request.addfinalizer(cleanup)
Run Code Online (Sandbox Code Playgroud)
测试:
def test_post(delete_after_post):
Id = post(api)
assert Id
Run Code Online (Sandbox Code Playgroud)
将响应(ID)传递回夹具以进行清理的最佳方法是什么。不想将清理作为测试的一部分。
我在测试选项卡中使用 tv4 验证 Postman 中的架构时遇到问题 - 无论我提供什么,它总是返回一个真实的测试。我完全不知所措,真的需要帮忙 - 这是我的示例 JSON 响应和我的测试:
我已经尝试了我能找到的每个 Stack Overflow/教程的大量变体,但没有任何效果 - 它总是返回 true。
//Test Example
var jsonData = JSON.parse(responseBody);
const schema = {
"required" : ["categories"],
"properties": {
"categories": {
"required" : ["aStringOne", "aStringTwo", "aStringThree" ],
"type": "array",
"properties" : {
"aStringOne": {"type": "string" },
"aStringTwo": {"type": "null" },
"aStringThree": {"type": "boolean" }
}
}
}
};
pm.test('Schema is present and accurate', () => {
var result=tv4.validateMultiple(jsonData, schema);
console.log(result);
pm.expect(result.valid).to.be.true;
});
//Response Example
{
"categories": [ …Run Code Online (Sandbox Code Playgroud) web-api-testing ×10
json ×2
postman ×2
python ×2
rest-assured ×2
bdd ×1
codeception ×1
cucumber ×1
cypress ×1
httprequest ×1
httpserver ×1
javascript ×1
jsonpath ×1
karate ×1
php ×1
pytest ×1
python-2.7 ×1
rest ×1
testing ×1
unit-testing ×1