如何在IntelliJ IDEA中识别cypress-cucumber-preprocessor未使用步骤定义?
如何在IntelliJ IDEA Ultimate中找出Cucumber未使用的步骤定义?
我正在清理代码,现有login.feature与login.ts文件,用IntelliJ IDEA Ultimate开发,想找出未使用的步骤定义。试了「分析->按名称运行检查->划掉已弃用步骤」但没结果,而且Cucumber+标签页找不到feature文件,请问能不能用IDEA实现这个需求?
现有代码
login.feature
Feature: Login Test Scenario: Sign in with an invalid password and good email Given I open my page and I'm not registered When I sign in with an invalid password and a good email Then I get an error message displayed
login.ts
Given('I open my page and I\'m not registered', () => { cy.visit(url); }); When('I sign in with an invalid password and a good email', () => { cy.get('input[name=username]').type(`username`)); cy.get('input[name=password]').type(`password`); cy.contains('Sign In').click(); }); Then('I get an error message displayed', () => { cy.contains('Invalid username or password.'); }); And('Unused step definition', () => { //unused step definition });
解决方法
首先得让IDEA正确识别你的Cucumber项目配置,不然没法检测步骤和feature文件的关联:
- 配置Cucumber框架
- 打开「File -> Project Structure -> Modules」,选中你的模块,切到「Dependencies」标签,确认已添加Cucumber相关依赖(npm项目的话,确保
cucumber或@cucumber/cucumber在package.json里,IDEA会自动识别)。 - 进「Settings -> Languages & Frameworks -> Cucumber」,点「+」添加feature文件所在的目录,保证
.feature文件关联到Cucumber。
- 打开「File -> Project Structure -> Modules」,选中你的模块,切到「Dependencies」标签,确认已添加Cucumber相关依赖(npm项目的话,确保
- 用正确的检查功能找未使用步骤
- 配置好后,打开「Analyze -> Run Inspection by Name」,搜索Unused Step Definition(你之前选的「划掉已弃用步骤」不对),运行这个检查,IDEA会扫描所有步骤定义,标记出没被feature文件引用的代码。
- 另外也可以右键点击步骤定义函数,选「Find Usages」,如果没任何引用结果,就说明这个步骤没被用。
- 修复Cucumber+标签页不显示feature文件的问题
配置完成后,Cucumber+标签页应该能自动加载feature文件。如果还是没显示,试试重启IDEA或者重新导入项目,还要确认feature文件所在目录没被标记为「Excluded」(右键目录,看「Mark Directory as」里有没有选「Excluded」)。
内容的提问来源于stack exchange,提问作者Eloise
相关产品推荐
相关产品推荐

