测试调用:如何设置所有测试套件的通用 [英] Test invocation: how to do set up common to all test suites

查看:95
本文介绍了测试调用:如何设置所有测试套件的通用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果Class是由Test-Suite发起的,有没有办法知道JUnit 4测试类?
我想在所有测试之前运行全局事物(关于内存数据库),所以我认为在测试套件中这样做。但是,我仍然希望能够在没有测试套件的情况下一次启动一个测试,所以我需要知道是否需要在测试的@Before部分初始化全局事物...是否有人知道如果它是可能的 ?

Is there a way to know in a JUnit 4 test Class, if the Class was initiated by a Test-Suite ? I have global things that I want to run before all tests (regarding in-memory DB), so I thought doing it in the test-suit. However, I still want to be able to initiate one test at a time without a Test-Suit, So I need to know if I need to initialize the global things in the @Before part of the test... Does any-one know if it's possible ?

推荐答案

有几种方法可以实现这一目标。最简单和最简单的方法是在套件的开头和结尾运行测试,设置数据库,然后设置全局标志。在你的@Before和@After测试中,你检查这个标志,如果有必要,进行设置/拆卸。

There are several ways to achieve this. The easiest and simplest is to have a 'test' which is run at the beginning and end of your suite which set up your database and then set a global flag. In your @Before and @After tests you check this flag and if necessary do the setup/teardown.

@RunWith(Suite.class)
@SuiteClasses({SetupTest.class, RealTest.class, TeardownTest.class});

这是最简单的解决方案,但它不是很好,所以一个更简洁的解决方案是使用 TestRule 。请查看扩展 ExternalResource 。这实现了之前和之后在围绕您的测试方法的逻辑之后。这将允许您分解@Before和@After方法,在任何地方重用相同的代码。

This is the simplest solution, but it isn't very nice, so a neater solution would be to use a TestRule. Look at extending ExternalResource. This implements before & after logic which surrounds your test methods. This would allow you to factor out your @Before and @After methods, to reuse the same code everywhere.

然后,对于您的套件,您需要在之前/之后实现逻辑也是如此。遗憾的是,使用@RunWith(Suite.class)注释的类实际上并未实例化,因此您不能使用该类的构造函数,但可以扩展套件。根据你将如何运行它,你需要实现一个构造函数,以@RunWith为例:

Then, for your suite, you need to implement before/after logic as well. Unfortunately, the class annotated with @RunWith(Suite.class) isn't actually instantiated, so you can't use the constructor of that class, but you can extend Suite. Depending upon how you will run this, you will will need to implement one of the constructors, using @RunWith as an example:

public class MySuite extends Suite {
    /**
     * Called reflectively on classes annotated with <code>@RunWith(Suite.class)</code>
     * 
     * @param klass the root class
     * @param builder builds runners for classes in the suite
     * @throws InitializationError
     */
    public MySuite(Class<?> klass, RunnerBuilder builder) throws InitializationError {
        this(builder, klass, getAnnotatedClasses(klass));
        // put your global setup here
        // set global variable
    }
}

然后运行你的测试套件

@RunWith(MySuite.class)

在不同情况下使用了几个构造函数,查看每个构造函数旁边的注释以了解具体细节。您仍然需要使用全局变量,以便规则不会重新执行设置代码。如果您只想执行设置代码,执行拆卸代码更难,但上述操作可以完成。如果您需要,请告诉我: - )

There are several constructors which are used in different situations, look at the comments next to each for specifics. You still need to use a global variable so that your Rules don't re-execute the setup code. The above will work if you're wanting to execute only setup code, executing teardown code is harder, but can be done. Let me know if you need it :-)

如果您想要更灵活(例如仅针对特定方法执行设置代码),请参阅我对如何在套件中定义JUnit方法规则?。

If you're wanting more flexibility (say executing setup code only for specific methods), then see my answer to How to define JUnit method rule in a suite?.

这篇关于测试调用:如何设置所有测试套件的通用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆