jUnit 4 中的 TestSuite 设置 [英] TestSuite Setup in jUnit 4

查看:20
本文介绍了jUnit 4 中的 TestSuite 设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法找出如何在 jUnit 4 中创建一个 TestSuite,但我真的很想念在 TestSetup 中包装一个套件的 v3 可能性.

I've managed to find out how to make a TestSuite in jUnit 4, but I really miss the v3 possibility of wrapping a suite in a TestSetup.

关于如何在 jUnit 4 中为一组测试用例执行一些 @BeforeClass/@AfterClass 设置的任何想法?

Any ideas as to how to get some @BeforeClass/@AfterClass setup executed for a suite of test cases in jUnit 4?

@RunWith(Suite.class)
@Suite.SuiteClasses({Test1.class, Test2.class})
public class MyTestSuite {
    @BeforeClass public static void setUpClass() {
        // Common initialization done once for Test1 + Test2
    }
    @AfterClass public static void tearDownClass() {
        // Common cleanup for all tests
    }
}

不幸的是,上面的代码片段不起作用.@BeforeClass 仅适用于每个测试类.

Unfortunately the above code fragment doesn't work. @BeforeClass only works on a per-test-class basis.

推荐答案

这是我所拥有的,它运行得很好.

Here is what I have and it runs just fine.

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ TestSuite1.class, TestSuite2.class })
public class CompleteTestSuite {

    @BeforeClass 
    public static void setUpClass() {      
        System.out.println("Master setup");

    }

    @AfterClass public static void tearDownClass() { 
        System.out.println("Master tearDown");
    }

}

这是我的测试套件 1(对测试套件 2 执行相同操作).

Here is my test suite 1 (do the same for test suite 2).

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(value = Suite.class)
@SuiteClasses(value = { TestCase1.class })
public class TestSuite1 {}

这是我的测试课.创建 testcase1 和 testcase2.

And here is my test class. Create both testcase1 and testcase2.

import static org.junit.Assert.assertEquals;

import org.junit.BeforeClass;
import org.junit.Test;

public class TestCase1 {

    @BeforeClass 
    public static void setUpClass() {      
        System.out.println("TestCase1 setup");
    }

    @Test
    public void test1() {
        assertEquals(2 , 2);
    }
}    

你应该有 5 个班级全套套房 1套房2测试1测试2

you should have 5 classes completesuite suite1 suite2 test1 test2

并确保您的构建路径中有 Junit.这应该运行!

and make sure you have Junit in your build path. This should run!

这是输出

Master setup
TestCase1 setup
Master tearDown

这篇关于jUnit 4 中的 TestSuite 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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