重试失败的自动化测试用例有助于提高自动化测试用例的稳定性,那么我们来看看python和java生态中的具体做法吧?怎么做如果你在python生态系统中使用pytest作为测试驱动,你可以使用pytest插件pytest-rerunfailures来重新运行失败的案例。有两种具体的使用方法。一种是通过命令行指定pytest--reruns2--reruns-delay1,reruns表示重复运行的次数,reruns-delay表示重复运行的延迟时间。另一种方法是使用@pytest.mark.flaky(reruns=2,reruns_delay=1)。一般采用这种方法。您不想在全局范围内重新运行所有测试用例,而是只需要运行特定的测试用例。在您的测试方法上使用此标志。如果你使用junit作为java生态中的测试驱动,junit5提供了注解@RepeatTest(2),你可以尝试重复测试类或测试方法,也可以自定义控制测试用例,实现一个TestRule接口跑步。公共类MyTestClass{@RulepublicRepeatRulerepeatRule=newRepeatRule();@Test@Repeat(10)publicvoidtestMyCode(){//你的测试代码在这里}}importstaticjava.lang.annotation.ElementType.ANNOTATION_TYPE;importstaticjava.lang.annotation.ElementType.METHOD;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)@Target({METHOD,ANNOTATION_TYPE})public@interfaceRepeat{intvalue()默认值1;}importorg.junit.rules.TestRule;importorg.junit.runner.Description;importorg.junit.runners.model.Statement;publicclassRepeatRuleimplementsTestRule{privatestaticclassRepeatStatementextendsStatement{privatefinalStatement语句;私人最终诠释重复;publicRepeatStatement(Statementstatement,intrepeat){this.statement=statement;这个.repeat=重复;}@Overridepublicvoidevaluate()throwsThrowable{for(inti=0;i
