参数化 jUnit 测试,无需更改 runner

发布于 2024-10-08 11:39:10 字数 191 浏览 4 评论 0原文

是否有一种干净的方法来运行参数化 jUnit 4 测试而不更改运行器,即不使用 @RunWith(Parameterized.class)

我的单元测试已经需要一个特殊的运行程序,并且我无法用参数化替换这个运行程序。也许有某种“跑步者链接”,所以我可以同时跑步? (只是一个疯狂的猜测......)

Is there a clean way to run parameterized jUnit 4 tests without changing the runner, i.e. without using
@RunWith(Parameterized.class)?

I have unit tests which require a special runner already and I can't replace this one with Parameterized. Maybe there is some kind of "runner chaining" so I could both runners at the same time? (Just a wild guess...)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随波逐流 2024-10-15 11:39:10

我发布了一个包含几个运行程序的框架,这些运行程序能够在测试类上强制参数化,同时允许您链接任意第 3 方运行程序以进行实际的测试执行。

框架是 CallbackParams - (http://callbackparams.org) - 这些是运行器:

  • CallbackParamsRunner
  • BddRunner

通过使用框架注释 ...

  • @WrappedRunner

... 您可以通过以下方式指定任意第 3 方运行器:

@RunWith(CallbackParamsRunner.class) // or @RunWith(BddRunner.class)
@WrappedRunner(YourSpecialRunner.class)
public class YourTest {
...

参数化然而,使用 CallbackParams 的测试与传统的测试参数化方法有很大不同。这篇教程文章中解释了原因,BddRunner 解释了教程文章末尾处

对于您的第一个 CallbackParams 测试,您可能更喜欢 BddRunner,因为它需要较少的样板文件,但是当您开始在不同测试类之间重用参数值时,您可能最好使用 CallbackParamsRunner,它需要更强的类型检查。

另外 - 使用 BddRunner 你不能有任何 @Test-methods。相反,您必须使用框架注释@Given、@When 和@Then。该要求有时会与第三方运行程序的要求发生冲突,但通常效果很好。

祝你好运!

I have released a framework with a couple of runners that are able to enforce parameterization on the test-class while allowing you to chain an arbitrary 3rd-party runner for the actual test-execution.

The framework is CallbackParams - (http://callbackparams.org) - and these are the runners:

  • CallbackParamsRunner
  • BddRunner

By using the framework annotation ...

  • @WrappedRunner

... you can specify an arbitrary 3rd-party runner in this manner:

@RunWith(CallbackParamsRunner.class) // or @RunWith(BddRunner.class)
@WrappedRunner(YourSpecialRunner.class)
public class YourTest {
...

Parameterized tests with CallbackParams differ considerably from the traditional approach to test-parameterization, however. The reasons are explained in this tutorial article with BddRunner explained near the end of the tutorial article.

For your first CallbackParams test you would probably prefer BddRunner, since it requires less boiler-plate stuff, but when you start reusing parameter values between different test-classes you are probably better off with CallbackParamsRunner, which demands stronger type-checking.

Also - with BddRunner you must not have any @Test-methods. Instead you must use the framework annotations @Given, @When and @Then. That requirement sometimes clash with those of the third-party runner but it usually works out quite well.

Good Luck!

烂柯人 2024-10-15 11:39:10

org.junit.runners.Parameterized是由org.junit.internal.builders.AnnotatedBuilder通过反射机制创建的。也许您可以将 Parameterized 扩展为您自己的 Runner:@RunWith(MyParameterized.class)。

org.junit.runners.Parameterized is created by org.junit.internal.builders.AnnotatedBuilder by reflect mechanism. Maybe you could extend Parameterized as your own Runner: @RunWith(MyParameterized.class).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文