behave结果转化为cucumber结果,主要用于将behave.json转化为cucumber.json

前言

该python库主要用于将behave框架生成的behave.json转化为cucumber.json,使用cucumber的报告插件可以对behave生成结果进行解析,展示。为什么我会单独进行该库的维护,主要是原作者已经很久没有对于issue进行bug修复,在使用过程中,有局限性,所以我将该库重新拉下来,进行bug修复,重新打包,上传pypi,非常感谢原作者:andreybehalf

behave_to_cucumber

由于原作者对于behave_to_cucumber没有进行维护了,有一些bug需要进行修复,所以单开一个仓库用于维护,如果有问题,可以提issue给成都-阿木木

  • 感谢原作者:andreybehalf
  • 原项目地址:https://github.com/behalf-oss/behave_to_cucumber

介绍

该项目主要用于将behave框架生成的behave.json转换为cucumber.json

使用示例

import json
import behave_to_cucumber
with open('behave_json.json') as behave_json:
    cucumber_json = behave_to_cucumber.convert(json.load(behave_json),remove_background=True)

covert内置三个参数用于控制生成的cucumber.json报告

  • remove_background:删除前置条件,默认False
  • duration_format:持续时间格式化,默认False
  • deduplicate:重复数据消除,默认False

从bash运行

感谢 @lawnmowerlatte 添加了 Main ,现在您可以运行:

python -m behave2cucumber

changelog

2022/0706

  • 修改错误堆栈信息截取修改为5000
  • 修复堆栈信息只在一行中显示,格式化信息优化显示

测试社区

欢迎加入测试交流群:夜行者自动化测试(816489363)进行交流学习QAQ

成都-阿木木

### Cucumber API 自动化测试框架使用教程 #### 背景介绍 Cucumber 是一种行为驱动开发 (BDD) 工具,主要用于功能测试和验收测试。它允许开发者和技术人员通过自然语言描述业务需求,并将其转化为可执行的测试脚本。虽然 Cucumber用于 Web 应用程序的功能测试[^1],但它也可以扩展到接口自动化测试领域。 以下是基于 Java 和 Python 的两种常见方式来构建 Cucumber 驱动的 API 测试框架: --- #### 方法一:Java + Cucumber + RestAssured 构建 API 测试框架 RestAssured 是一个强大的库,专门用于简化 HTTP 请求和响应验证的过程。它可以轻松集成到 Cucumber 中以完成 API 测试任务。 ##### 依赖配置 在 `pom.xml` 文件中添加以下 Maven 依赖项: ```xml <dependencies> <!-- Cucumber --> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>7.8.0</version> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-junit</artifactId> <version>7.8.0</version> </dependency> <!-- Rest Assured --> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>5.3.0</version> </dependency> </dependencies> ``` ##### Feature File 定义 创建 `.feature` 文件,定义测试场景。例如,在 `src/test/resources/apiTest.feature` 下定义如下内容: ```gherkin Feature: Test User API Endpoints Scenario: Verify GET request to fetch user details Given the base URL is set to "https://jsonplaceholder.typicode.com" When a GET request is sent to "/users/1" Then the response status code should be 200 And the response body contains key-value pair {"name": "Leanne Graham"} ``` ##### Step Definitions 实现 在 `src/test/java/steps` 目录下创建对应的步骤定义类: ```java package steps; import io.cucumber.java.en.Given; import io.cucumber.java.en.When; import io.cucumber.java.en.Then; import static org.hamcrest.Matchers.equalTo; import io.restassured.RestAssured; import io.restassured.response.Response; public class ApiSteps { private Response response; @Given("the base URL is set to {string}") public void setBaseUrl(String baseUrl) { RestAssured.baseURI = baseUrl; } @When("a GET request is sent to {string}") public void sendGetRequest(String endpoint) { this.response = RestAssured.given().when().get(endpoint); } @Then("the response status code should be {int}") public void verifyStatusCode(int statusCode) { response.then().assertThat().statusCode(statusCode); } @Then("the response body contains key-value pair {string}") public void verifyResponseBodyContainsKeyValue(String keyValueString) { String[] keyValueArray = keyValueString.substring(1, keyValueString.length() - 1).split(":"); String key = keyValueArray[0].trim(); String value = keyValueArray[1].trim(); response.then().body(key, equalTo(value)); } } ``` ##### 执行测试 运行上述测试可以通过 JUnit 或其他测试工具触发。确保已正确设置 glue 参数指向 step definitions 类路径[^4]。 --- #### 方法二:Python + Cucumber + Requests 构建 API 测试框架 对于 Python 用户,可以利用 Behave(类似于 Cucumber 的 BDD 工具)以及 Requests 库来实现类似的 API 测试流程。 ##### 安装依赖 安装必要的包: ```bash pip install behave requests ``` ##### Feature File 定义 创建 `features/user_api_test.feature` 文件: ```gherkin Feature: Test User API Endpoints with Python and Behave Scenario: Verify POST request to create a new user Given the base URL is "https://reqres.in" When I send a POST request to "/api/users" with payload: """ { "name": "morpheus", "job": "leader" } """ Then the response status code should be 201 And the response body should contain "id" ``` ##### Steps Implementation 在 `features/steps/api_steps.py` 文件中实现对应逻辑: ```python from behave import given, when, then import requests import json @given('the base URL is "{base_url}"') def set_base_url(context, base_url): context.base_url = base_url @when('I send a POST request to "{endpoint}" with payload:') def post_request_with_payload(context, endpoint): url = f"{context.base_url}{endpoint}" headers = {'Content-Type': 'application/json'} payload = json.loads(context.text) context.response = requests.post(url, headers=headers, json=payload) @then('the response status code should be {status_code}') def check_status_code(context, status_code): assert context.response.status_code == int(status_code), \ f"Expected {status_code}, but got {context.response.status_code}" @then('the response body should contain "{key}"') def check_response_body_contains_key(context, key): data = context.response.json() assert key in data, f'Key "{key}" not found in response' ``` ##### 运行测试 切换至项目根目录并运行命令: ```bash behave features/ ``` --- ### 总结 无论是采用 Java 技术栈还是 Python 生态环境,都可以借助 Cucumber/Behave 来实施 API 接口的自动化测试。这种方式不仅能够提升团队协作效率,还能显著降低维护成本[^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值