cucumber, 重新执行失败的用例
做自动化测试的时候,我们经常会出现因为环境或者数据问题导至用例失败,如何能简便快速地重新跑这些case呢?这时候我们就可以加一个很重用的config [rerun]到runer里面了.
如下所示,我们需要在runner里加入["rerun:target/failed_rerun.txt"],这样每次结束之后我们可以在target里面找到failed_rerun.txt, 里面就记录了哪些test cases是执行失败的。
package com.example.demoForCucumber; import com.example.demoForCucumber.config.PrettyCucumberReport; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; import org.junit.AfterClass; import org.junit.Test; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions( tags = {"@API"}, features = {"src/main/resources/features"}, glue = {"com.example.demoForCucumber"}, plugin = { "pretty", "html:target/cucumber", "json:target/cucumberReportJsonFiles/cucumber-report.json", "rerun:target/failed_rerun.txt" } ) public class DemoForCucumberApplicationTests { @AfterClass public static void generateReport(){ PrettyCucumberReport prettyCucumberReport = new PrettyCucumberReport(); prettyCucumberReport.generateCucumberReport(); } }
然后我们再建一个FailedReRun runner来执行这些case 即可, 代码如下:
package com.example.demoForCucumber; /* * @author Helen Lee * @create 2023/4/9 * @description: rerun those failed test case */ import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions( features = {"@target/failed_rerun.txt"}, glue = {"com.example.demoForCucumber"}, plugin = { "pretty", "html:target/cucumber", "json:target/cucumberReportJsonFiles/cucumber-report.json", "rerun:target/failed_rerun.txt" } ) public class FailedReRun { }