如何跳过执行期间 hangs/stuck 的测试 Selenium - Maven - Jenkins

How to skip tests that hangs/stuck during execution Selenium - Maven - Jenkins

我想就我的问题寻求您的帮助。我有包含 50 个测试脚本的 Regression Suite(.xml)。我们能够使用 Maven 在 Jenkins 中执行回归套件,但它突然 hangs/stuck。例如:

测试用例1-15已经完成,但是在测试用例16的时候,build/execution突然stuck/hangs在某个步骤。我试图在我的 Eclipse 中执行测试用例 16 以了解 step/code 有什么问题,但它工作正常。

我想请问有没有maven-surefire功能,当它检测到一个测试脚本是stuck/hangs一定分钟数(例如5分钟)时,它会失败测试脚本然后继续下一个。

请看下面我的pom.xml

 <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <target>1.8</target>
                <source>1.8</source>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <inherited>true</inherited>
            <configuration>
                <testFailureIgnore>false</testFailureIgnore>
                <suiteXmlFiles>
                    <suiteXmlFile>${xmlPath}</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>
  </build>

您可以通过在 pom.xml

中将此 属性 设置为 true 让 Maven 忽略测试失败
<testFailureIgnore>true</testFailureIgnore>

您可以在测试本身的测试注释中添加超时:

@Test(timeout=600000)  //Fails if the method takes longer than 10 minutes

它以毫秒为单位,所以您只需要决定测试应该花费的最长时间,然后 calculate it from there

编辑:如果您只想设置一次而不必为每个单独的测试添加超时,那么您可以在基础 class:

中设置一个规则
import org.junit.Rule;
import org.junit.rules.TestWatcher;

...

@Rule
public TestWatcher watcher = new TestWatcher() {
    @Override
    public Statement apply(Statement base, Description description) {           
        if (description.getMethodName().toLowerCase().contains("thisTestTakesLonger")) {
            return new FailOnTimeout(base, 600000); //10 minutes = 600000
        } else {
            return new FailOnTimeout(base, 300000); //5 minutes = 300000
        }  
    }
};

else 是全局超时,if 是如果你有一个单独的测试需要更长的时间(如果每个测试的长度大致相同,你可能只想要 else 中的位)