Maven JUnit 5:使用 JUnit5 标签,默认运行所有单元测试或仅从命令行运行特定测试套件

发布于 2025-01-14 15:27:21 字数 2846 浏览 5 评论 0原文

我发布了这个: Maven JUnit 5:默认运行所有单元测试或仅从命令行运行特定测试套件

我只是用几乎完全相同的标题再次发布它,因为我的基本问题没有任何改变,但我将包括我尝试对标签执行的操作,这些标签对我不起作用。

我们正在将服务升级到相对较新的 SpringBoot 版本以及 JUnit 5。我们通常仅使用“mvn package”运行我们的构建,它默认运行我们所有的单元测试。

我们还有“组件测试”,它们从单独的命令行运行,并且全部在具有特定名称的单个测试套件中指定。在 JUnit 5 之前,我们会使用类似以下内容来运行它:

mvn -Dtest=ComponentTestSuite test

This was working well with JUnit 4.

该类看起来像这样:

@Suite
@SelectClasses(<testclassname>.class)
public class ComponentTestSuite {
}

使用 JUnit 5,这最终会显示“未找到测试”。

我们使用的是 Spring-Boot v2.3.12,默认情况下包含较旧版本的 JUnit 5。我将覆盖这些默认值,并包含 junit-platform 组件的 v1.8.2 和 jupiter 组件的 v5.8.2。

对于我正在测试的服务,测试套件只有一个组件测试(这是不寻常的)。我可以简单地将“ComponentTestSuite”替换为组件测试类的名称,这样就可以运行单个组件测试。

我注意到这个线程: Junit5带有 @Suite 注释的测试套件不会使用 mvn test 命令执行测试

不幸的是,我发现将“-Dtest”更改为“-Dinclude”只是运行了所有默认单元测试并忽略了测试套件。

所以,我被告知我“不应该这样做”,尽管我不清楚我到底不应该做哪些部分,但我应该使用“标签”。好的,所以我尝试使用标签。

我还注意到这篇文章: https://www.baeldung.com/junit-filtering-tests< /a> ,尽管那篇文章似乎使用了一些已弃用的机制,例如“@RunWith(JUnitPlatform.class)”,所以我不确定我能从那篇文章中相信多少。

我一直在尝试几种变体,我的最后一次尝试是这样的:

@Suite
@Tag("ComponentTest")
@IncludeTags("ComponentTest")
public class ComponentTestSuite {
}

我将 @Tag("ComponentTest") 添加到我的一个组件测试类中。

这还是不行。我尝试运行 mvn -Dtest=ComponentTestSuite test,它显示“未找到测试”。如果我尝试 mvn -Dtests=ComponentTest test ,它只会运行我的所有单元测试并忽略我的组件测试套件。如果我改为运行 mvn test -Dgroups=ComponentTest ,这会给我“未找到测试”的另一种变体,因为它不会打印它,它只是字面上不执行任何测试而不会给出错误。

现在看看那个套件类,它确实看起来很荒谬,因为它不再真正“包含”任何东西,这是不幸的。将组件测试类列表放在一个地方是件好事。现在,套件类似乎毫无意义,但无论如何我都无法让任何东西工作。

请注意,我还没有将“groups”元素添加到我的 Surefire 配置中,并且我还没有编辑所有单元测试来添加 @Tag("UnitTest") 注释。我只编辑了套件类和一个组件测试。

更新:

为了解决评论,我指定以下依赖项(除其他外):

版本 1.8.2

  • junit-platform-suite
  • junit-platform-suite-api
  • junit-platform-launcher
  • junit-platform-commons
  • junit-platform-engine
  • junit-platform-runner
  • junit-platform-suite-engine

junit-bom版本 5.8.2

  • junit-jupiter
  • (可能不必要)

整个套件类是这样的:

import org.junit.jupiter.api.Tag;
import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@Tag("ComponentTest")
@IncludeTags("ComponentTest")
public class ComponentTestSuite {
}

I posted this: Maven JUnit 5: Run all unit tests by default or run only a specific test suite from the command line .

I'm simply posting it again with almost exactly the same title because nothing about my base problem has changed, but I will include what I tried to do with Tags, which are not working for me.

We are upgrading our services to a relatively recent SpringBoot version, along with JUnit 5. We normally run our builds with just "mvn package", which runs all of our unit tests by default.

We also have "component tests", which are run from a separate command line, and which are all specified in a single test suite with a specific name. Before JUnit 5, we would run this with something like:

mvn -Dtest=ComponentTestSuite test

This was working fine with JUnit 4.

The class looks like this:

@Suite
@SelectClasses(<testclassname>.class)
public class ComponentTestSuite {
}

With JUnit 5, this ends up saying "No tests found".

We are using v2.3.12 of Spring-Boot, which by default includes a somewhat older version of JUnit 5. I am overriding those defaults and including v1.8.2 of the junit-platform components and v5.8.2 of the jupiter components.

For the service that I'm testing this with, the test suite only has a single component test (which is unusual). I WAS able to simply replace "ComponentTestSuite" with the name of the component test class, and that would run that single component test.

I noticed this thread: Junit5 test suites with @Suite annotation doesn't execute tests with mvn test command .

Unfortunately, what I found was that changing "-Dtest" to "-Dinclude" simply ran all my default unit tests and ignored the test suite.

So, I was told that I'm "not supposed to do it this way", although it's not clear to me exactly which parts I'm not supposed to do, but I am supposed to use "Tags". Ok, so I tried to use tags.

I also noticed this article: https://www.baeldung.com/junit-filtering-tests , although that article appears to use some deprecated mechanisms, like "@RunWith(JUnitPlatform.class)", so I'm not sure how much I can believe from that article.

I've been trying several variations, and my last attempt is this:

@Suite
@Tag("ComponentTest")
@IncludeTags("ComponentTest")
public class ComponentTestSuite {
}

And I added @Tag("ComponentTest") to my one component test class.

This still doesn't work. I try to run mvn -Dtest=ComponentTestSuite test, and it says "No tests found". If I instead try mvn -Dtests=ComponentTest test that just runs all of my unit tests and ignores my component test suite. If I instead run mvn test -Dgroups=ComponentTest, that gives me another variation of "No tests found" in that it doesn't print that, it just literally executes no tests without giving an error.

Looking at that suite class now, it does seem nonsensical, as it doesn't really "contain" anything anymore, which is unfortunate. It was good to have that list of component test classes in one place. Now, it seems like the suite class is pointless, but I can't get anything to work anyway.

Note that I haven't yet added a "groups" element to my surefire config, and I haven't edited all of my unit tests to add a @Tag("UnitTest") annotation. I've only edited the suite class and the one component test.

Update:

To address a comment, I am specifying the following dependencies (among others):

Version 1.8.2 of

  • junit-platform-suite
  • junit-platform-suite-api
  • junit-platform-launcher
  • junit-platform-commons
  • junit-platform-engine
  • junit-platform-runner
  • junit-platform-suite-engine

Version 5.8.2 of

  • junit-jupiter
  • junit-bom (probably unnecessary)

The entire suite class is this:

import org.junit.jupiter.api.Tag;
import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@Tag("ComponentTest")
@IncludeTags("ComponentTest")
public class ComponentTestSuite {
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文