你可以使用Maven来运行之前失败的JUnit 5测试。

huangapple go评论74阅读模式
英文:

How can I use Maven to run only previously failed JUnit 5 tests?

问题

我有一组基于junit 5、maven和java的自动化测试。我需要编写一个脚本或方法来检查之前执行的测试中是否有失败的测试。如果有这样的测试,然后运行它们。

在我的TestWatcher中,我获取了一系列失败的测试,但我不知道如何告诉maven只运行它们。我知道可以通过mvn test -Dtest="TestClass1, TestClass2"来运行测试,但如果有20个测试,这不是一个选项。

@RepeatedTest junit注解以及surefire rerunFailingTestsCount属性都不合适,因为它们会立即触发失败的测试。当然,这很好,但对于这个特定的任务不适用。

英文:

I have a set of autotests on junit 5, maven, java. I need to write a script or method that checks if there are failed tests from previous execution. And if there are such tests, then run them.

In my TestWatcher I get a list of fallen tests, but I don't understand how to tell maven to only run them. I know of an option to run tests via mvn test -Dtest="TestClass1, TestClass2", but this is not an option if there are, for example, 20 tests.

The @RepeatedTest junit annotations, as well as the surefire rerunFailingTestsCount property, are not appropriate because they are triggered immediately on a dropped test. Which is good, of course, but not for this specific task.

答案1

得分: 0

以下是您要翻译的内容:

根据我理解,存在使用LauncherFactory运行特定测试的选项。但还有另一种选项,不特别设计用于大量测试。

总结一下 - 我捕获了失败的测试并将它们写入文件。然后我遍历它们并将它们替换为mvn test命令中的内容。这是实现的样子:

  1. 此实用程序将获得的每个值写入文件
public static void writeLine(final String value, final String filepath) throws IOException {
    try (FileWriter fileWriter = new FileWriter(filepath, true)) {
        fileWriter.append(value);
        fileWriter.append("\n");
        fileWriter.flush();
    }
}
  1. 在实现TestWatcher的类的testFailed方法中,我触发将失败方法的名称写入文件的操作。FileUtils - 实用程序所在类的名称。Substring用于删除方法中的()
try {
    FileUtils.writeLine(context.getDisplayName().substring(0, context.getDisplayName().length() - 2),
            "failedTests");
}
  1. 最后,我有一个sh脚本,读取文件并遍历方法的名称,然后清空文件
filepath="failedTests.txt"
testclass="*Test#"
script=""
for var in $(cat $filepath)
do
  script+="${testclass}${var}, "
done
mvn -Dtest="$script" test
true > "$filepath"

输出将是类似于"mvn -Dtest=*Test#failmethod1, *Test#failmethod2, "的字符串。运行该脚本将只运行上次运行中失败的测试。

filepath="failedTests.txt" - 记录测试的文件以及从中获取失败测试的文件。

我有所有带有"Test"签名的类。因此,我使用"*Test"构造。也可以在这里阅读关于基于模板的类选择的信息。它还解释了为什么需要#符号(用于选择特定方法)。

true > "$filepath" - 清空文件

英文:

As far as I understand, there is an option to run specific tests using LauncherFactory. But there is another option, which is not particularly designed for a large number of tests.
In summary - I catch the failed tests and write them to the file. Then I go through them and substitute them in the mvn test command. This is what the implementation looks like:

  1. This utility writes each value obtained to a file

     public static void writeLine(final String value, final String filepath) throws IOException {
     try (FileWriter fileWriter = new FileWriter(filepath, true)) {
         fileWriter.append(value);
         fileWriter.append("\n");
         fileWriter.flush();
     }
    
  2. In the testFailed method of the class that implements TestWatcher I trigger writing the name of the fallen method to a file. FileUtils - the name of the class where the utility is located. Substring is needed to trim () from the method

        try {
         FileUtils.writeLine(context.getDisplayName().substring(0, context.getDisplayName().length() - 2),
                 "failedTests"); }
    
  3. Finally, I have a sh script that reads the file and flips through the name of the methods.And then clears the file

<code>

filepath=&quot;failedTests.txt&quot;
testclass=&quot;*Test#&quot;
script=&quot;&quot;
for var in $(cat $filepath)
do
  script+=&quot;${testclass}${var}, &quot;
  done
mvn -Dtest=&quot;$script&quot; test
true &gt; &quot;$filepath&quot;

</code>

The output will be a string like mvn -Dtest=*Test#failmethod1, *Test#failmethod2, " test. Running the script will only run the tests that fell in the last run.

filepath="failedTests.txt" - file, where the tests were recorded and where to take the dropped tests from.

I have all classes with the nameTest signature. So I use the *Test construct. That is, I select all classes. You can read about template-based class selection here. It also explains why the # symbol is needed (to choose a specific method).

true > "$filepath" - clean the file

huangapple
  • 本文由 发表于 2023年5月22日 23:53:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定