Cypress搭配cucumber插件运行时报No tests found错误怎么解决
Cypress+cucumber No tests found 报错解决方案
核心问题根因
你遇到的报错由配置错误、步骤匹配不一致、预处理器缺失三类问题共同导致,按以下步骤逐一修正即可解决:
1. 修正cypress.json配置
你当前配置的testFiles仅匹配.js文件,但cucumber的测试入口是.feature文件,需要修改配置项:
{ "testFiles": "**/*.feature", "baseUrl": "https://localhost:17843/GfmAdminConsole/", "env":{ "url":"https://rahulshettyacademy.com" }, "reporter": "mochawesome" }
2. 修正步骤定义字符串完全匹配
cucumber要求步骤定义的字符串和feature文件中的步骤完全一致(大小写、内容、标点均无差异),你当前的匹配错误如下:
- feature中
Given I open ecommerce page对应步骤定义错误写为I open ECommerce Page,需修正为完全和feature一致的字符串 - feature中
When I add items to cart对应步骤定义错误写为I add items to Cart,需将Cart改为小写cart - feature中
And Validate the total prices对应步骤定义错误写为Validate the total price of the items selected,需修正为完全和feature一致的字符串 - feature中
Then select the country submit and verify Thankyou对应步骤定义错误写为Select the country submit and verify Thank you,需将首字母改为小写,Thank you合并为Thankyou
3. 补全cucumber预处理器配置
你缺少cucumber预处理器的必要配置,按以下要求补充:
- 首先确认
package.json中已安装cypress-cucumber-preprocessor依赖,新增如下配置项:
"cypress-cucumber-preprocessor": { "nonGlobalStepDefinitions": true }
- 打开
cypress/plugins/index.js文件,添加预处理器注册逻辑:
const cucumber = require('cypress-cucumber-preprocessor').default module.exports = (on, config) => { on('file:preprocessor', cucumber()) }
4. 修正代码与文件结构问题
- 将步骤定义文件顶部的
/// <reference types="Cypress" />移动到所有import语句的最上方,否则类型声明不生效 - 你步骤定义中使用的
testData变量未导入,需要补充对应测试数据文件的导入逻辑,调试阶段可先注释testData.productName相关代码验证测试是否能正常识别 - 检查
ecommerce.feature文件名是否存在前置空格,你提供的文件结构中该文件名前有多余空格,需删除保证和同级的ecommerce文件夹名称完全一致
内容的提问来源于stack exchange,提问作者Stitch
相关产品推荐
相关产品推荐

