多次重新运行一组 JUnit 测试

发布于 2024-10-01 09:06:00 字数 300 浏览 2 评论 0原文

我有一组测试来测试一些 timzeone 相关功能,

我有一个 Before 方法,将默认时区设置为我选择的一个,以及一个 after 方法,用于恢复测试之前的默认值。

所以我基本上想做 将

时区设置为 UTC

运行测试

恢复时区

将时区设置为 EST

运行测试

恢复时区

将时区设置为 JST

运行测试

恢复时区

每种情况下的测试都是相同的。

有没有简单的方法可以做到这一点?

I've got a set of tests that test some timzeone related functionality

I have a Before method that sets the default timezone to one of my choosing, and an after method that restores whatever the default was before the tests.

So I basically want to do

Set timezone to UTC

Run tests

Restore timezone

Set timezone to EST

Run tests

Restore timezone

Set timezone to JST

Run tests

Restore timezone

The tests are identical in each case.

Is there an easy way to do this?

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

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

发布评论

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

评论(4

堇年纸鸢 2024-10-08 09:06:00

是的,有一个很好的方法可以一遍又一遍地进行相同的测试,只需使用其他一些数据集。解决方案是“@RunWith(Parameterized.class)” - 链接到 javadoc< /a>.我假设您正在使用 JUnit 4 - 但据我所知,TestNG 也具有此类功能。

您需要编写的代码将类似于以下内容:

 @RunWith(Parameterized.class)
 public class FibonacciTest {
        @Parameters
        public static Collection<Object[]> data() {
                return Arrays.asList(new Object[][] {
                                Fibonacci,
                                { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 },
                                                { 6, 8 } } });
        }

        private int fInput;

        private int fExpected;

        public FibonacciTest(int input, int expected) {
                fInput= input;
                fExpected= expected;
        }

        @Test
        public void test(@HeresHowYouGetValue Type value) {
                assertAnswerKey(new Object[][] {
                                Fibonacci,
                                { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 },
                                                { 6, 8 } } });
                assertEquals(fExpected, Fibonacci.compute(fInput));
        }
 }

Yup, there is a nice way to do the same test over and over again, just with some other dataset. The solution is "@RunWith(Parameterized.class)" - link to javadoc. I'm assuming you're using JUnit 4 - but TestNG also has such functionality from what I remember.

The code you'll need to write will look somewhat like this:

 @RunWith(Parameterized.class)
 public class FibonacciTest {
        @Parameters
        public static Collection<Object[]> data() {
                return Arrays.asList(new Object[][] {
                                Fibonacci,
                                { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 },
                                                { 6, 8 } } });
        }

        private int fInput;

        private int fExpected;

        public FibonacciTest(int input, int expected) {
                fInput= input;
                fExpected= expected;
        }

        @Test
        public void test(@HeresHowYouGetValue Type value) {
                assertAnswerKey(new Object[][] {
                                Fibonacci,
                                { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 },
                                                { 6, 8 } } });
                assertEquals(fExpected, Fibonacci.compute(fInput));
        }
 }
暗喜 2024-10-08 09:06:00

我通过使用参数化和 RunWith 符号解决了这个问题:

请参阅: http://ourcraft.wordpress.com/2008/08/27/writing-a-parameterized-junit-test/

I solved the problem by using the Parameterized and RunWith notations:

See: http://ourcraft.wordpress.com/2008/08/27/writing-a-parameterized-junit-test/

星星的軌跡 2024-10-08 09:06:00

这是我写的一篇文章,展示了通过代码示例重复运行测试的几种方法:
http://codehowtos.blogspot.com/2011/04/run -junit-test-repeatedly.html

您可以使用@Parametrized运行器,或使用帖子中包含的特殊运行器

Here is a post I wrote that shows several ways of running the tests repeatedly with code examples:
http://codehowtos.blogspot.com/2011/04/run-junit-test-repeatedly.html

You can use the @Parametrized runner, or use the special runner included in the post

心在旅行 2024-10-08 09:06:00

最近我创建了 zohhak 项目。你可能会发现它很有用。它可以让你写:

@TestWith({
   "EST",
   "UTC",
   "null"
})
public void testMethod(TimeZone timeZone) {
   setTimeZone(timeZone);
   ...
}

@After
public void restoreTimezone() {
  ...
}

recently I created zohhak project. you may find it useful. it lets you write:

@TestWith({
   "EST",
   "UTC",
   "null"
})
public void testMethod(TimeZone timeZone) {
   setTimeZone(timeZone);
   ...
}

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