简体   繁体   English

如何在Junit3中获得@BeforeClass和@AfterClass等价物?

[英]How can I get @BeforeClass and @AfterClass equivalent in Junit3?

I want to back up my application's database before replacing it with the test fixture. 我想在用测试夹具替换它之前备份我的应用程序的数据库。 I'm forced to use Junit3 because of Android limitations, and I want to implement the equivalent behavior of @BeforeClass an @AfterClass. 由于Android限制,我被迫使用Junit3,我想实现@BeforeClass和@AfterClass的等效行为。

UPDATE: There is now a tool ( Junit4Android ) to get support for Junit4 on Android. 更新:现在有一个工具( Junit4Android )可以在Android上获得对Junit4的支持。 It's a bit of a kludge but should work. 这有点像kludge,但应该工作。

To achieve the @BeforeClass equivalent, I had been using a static variable and initializing it during the first run like this, but I need to be able to restore the database after running all the tests. 为了实现@BeforeClass等价物,我一直在使用静态变量并在第一次运行期间初始化它,但我需要能够在运行所有测试后恢复数据库。 I can't think of a way of detecting when the last test has run (since I believe there is no guarantee on the order of test execution.) 我想不出有什么方法可以检测上次测试的运行时间(因为我认为测试执行顺序无法保证。)

public class MyTest extends ActivityInstrumentationTestCase2<MainActivity> {
    private static boolean firstRun = true;

    @Override
    protected void setUp() {
        if(firstRun) {
            firstRun = false;
            setUpDatabaseFixture();
        }
    }
    ...
}

From the junit website : 来自junit网站

Wrapped the setUp and tearDown method in the suite.This is for the case if you want to run a single YourTestClass testcase. 在套件中包装了setUp和tearDown方法。如果你想运行一个YourTestClass测试用例,那就是这种情况。

public static Test suite() {
    return new TestSetup(new TestSuite(YourTestClass.class)) {

        protected void setUp() throws Exception {
            System.out.println(" Global setUp ");
        }
        protected void tearDown() throws Exception {
            System.out.println(" Global tearDown ");
        }
    };
}

If you would like to run only one setUp and tearDown for all the testcase, make a suite and add testClass to it and pass the suite object in TestSetup constructor.But I think there is not much usage for this,and in a way it is violating JUnit philosophy. 如果您只想为所有测试用例运行一个setUp和tearDown,请创建一个套件并将testClass添加到它并在TestSetup构造函数中传递套件对象。但我认为这没有多少用处,并且在某种程度上它是违反JUnit哲学。

Recently, I was looking for a similar solution too. 最近,我也在寻找类似的解决方案。 Fortunately, in my case after the JVM exits after the last test is run. 幸运的是,在我的情况下,在最后一次测试运行后JVM退出。 So I was able to achieve this by adding a JVM shutdown hook. 所以我能够通过添加JVM关闭钩子来实现这一点。

// Restore database after running all tests
Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() {
        restoreDatabase();
    }
});

hope this helps. 希望这可以帮助。

Isn't this (dealing elegantly with data, so you don't have to worry about restoring it) what testing with mock objects are for? 不是这个(优雅地处理数据,所以你不必担心恢复它) 用mock对象进行的测试是什么? Android supports mocking . Android 支持模拟

I ask as a question, since I've never mocked Android. 我问一个问题,因为我从未嘲笑过Android。


In my experiences, and from this blog post , when the Android tests are made into a suite and run by the InstrumentationTestRunner - ActivityInstrumentationTestCase2 is an extension of ActivityTestCase which is an extendsion of InstrumentationTestCase - they are ordered alphabetically using android.test.suitebuilder.TestGrouping.SORT_BY_FULLY_QUALIFIED_NAME , so you can just restore you DB with a method that is the lowes in the alphabet out of your test names, like: 在我的经验,从这个博客帖子 ,当Android的测试做成一套房并通过运行InstrumentationTestRunner - ActivityInstrumentationTestCase2是一个扩展ActivityTestCase这是一个extendsion InstrumentationTestCase -他们使用的按字母顺序排列android.test.suitebuilder.TestGrouping.SORT_BY_FULLY_QUALIFIED_NAME ,这样您就可以使用测试名称中字母表中android.test.suitebuilder.TestGrouping.SORT_BY_FULLY_QUALIFIED_NAME的方法恢复数据库,例如:

// underscore is low in the alphabet
public void test___________Restore() { 
    ... 
}

Note: 注意:

You have to pay attention to inherited tests, since they will not run in this order. 您必须注意继承的测试,因为它们不会按此顺序运行。 The solution is to override all inherited test and simply call super() from the override. 解决方案是覆盖所有继承的测试,只需从覆盖中调用super()。 This will once again have everything execute alphabetically. 这将再次按字母顺序执行所有操作。

Example: 例:

// Reusable class w only one time setup and finish. 
// Abstract so it is not run by itself.
public abstract class Parent extends InstrumentationTestCase {
    @LargeTest
    public void test_001_Setup() { ... }

    @LargeTest
    public void test_____Finish() { ... }
}

/*-----------------------------------------------------------------------------*/

// These will run in order shown due to naming.
// Inherited tests would not run in order shown w/o the use of overrides & supers
public class Child extends Parent {
    @LargeTest
    public void test_001_Setup() { super.test_001_Setup(); }

    @SmallTest
    public void test_002_MainViewIsVisible() { ... }

    ...

    @LargeTest
    public void test_____Finish() { super.test_____Finish(); }
}

I would suggest avoiding these kind of dependencies where you need to know the order in which tests are run. 我建议避免这种依赖,你需要知道运行测试的顺序。 If all you need is to restore a real database that was replaced by setUpDatabaseFixture() probably you solution comes from the use of a RenamingDelegatingContext . 如果你需要的是恢复被替换一个真正的数据库setUpDatabaseFixture()可能是你的解决方案来自于使用的RenamingDelegatingContext Anyway, if you can't avoid knowing when the last test was run, you can use something like this: 无论如何,如果您无法避免知道上次测试的运行时间,您可以使用以下内容:

...

private static final int NUMBER_OF_TESTS = 5; // count your tests here
private static int sTestsRun = 0;

...

protected void tearDown() throws Exception {
    super.tearDown();
    sTestsRun += countTestCases();

    if ( sTestsRun >= NUMBER_OF_TESTS ) {
        android.util.Log.d("tearDow", "*** Last test run ***");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在TestSuite界面中使用junit @Beforeclass &amp;&amp; @Afterclass? - How can I have a junit @Beforeclass && @Afterclass in my TestSuite interface? 如何要求JUnit测试实现@ BeforeClass / @ AfterClass“抽象方法”? - How to require that JUnit tests implement a @BeforeClass/@AfterClass “abstract method”? 禁用JUnit并发测试-BeforeClass,AfterClass问题 - Disable JUnit concurrent tests - BeforeClass, AfterClass issue 使用套件时,JUnit 4 @BeforeClass和@AfterClass - JUnit 4 @BeforeClass & @AfterClass when using Suites 如何在JunitPerf中使用@BeforeClass和@AfterClass? - How to use @BeforeClass and @AfterClass in JunitPerf? 在多个测试用例的情况下,Junit @BeforeClass和@AfterClass行为 - Junit @BeforeClass and @AfterClass behavior in case of multiple test cases 如何在JerseyTest套件中使用@BeforeClass和@AfterClass - How to use @BeforeClass and @AfterClass within a JerseyTest suite @BeforeClass 和@AfterClass 方法没有被执行 - @BeforeClass and @AfterClass methodis not executed 想要使用共享的@ BeforeClass / @ AfterClass和Spring App Context运行许多jUnit测试类 - Want to run many jUnit test classes with a shared @BeforeClass/@AfterClass and Spring App Context 如何使用TestNG在循环中运行@beforeclass @test和@Afterclass案例 - How to run @beforeclass @test and @Afterclass cases in a loop using TestNG
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM