Java jUnit:在任何测试类之前运行的测试套件代码

发布于 2024-08-16 07:58:19 字数 618 浏览 3 评论 0原文

我有一个测试套件类:

@RunWith(Suite.class)
@Suite.SuiteClasses({
    GameMasterTest.class,
    PlayerTest.class,
})
public class BananaTestSuite { 

我需要使用什么注释来使此类中的函数在包含实际测试的任何类之前运行?现在,我正在这样做,它有效,但它的可读性不高:

static {
    try {
        submitPeelAction = new Player(new GameMaster(1)).getClass().getDeclaredMethod("submitPeelAction");
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    submitPeelAction.setAccessible(true);
}

我尝试了 @BeforeClass 但它不起作用。

I have a test suite class:

@RunWith(Suite.class)
@Suite.SuiteClasses({
    GameMasterTest.class,
    PlayerTest.class,
})
public class BananaTestSuite { 

What annotation do I need to use to make a function in this class run before any of the classes containing actual tests? Right now, I'm doing this, and it works, but it's not as readable as it could be:

static {
    try {
        submitPeelAction = new Player(new GameMaster(1)).getClass().getDeclaredMethod("submitPeelAction");
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    submitPeelAction.setAccessible(true);
}

I tried @BeforeClass but it didn't work.

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

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

发布评论

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

评论(5

我不是你的备胎 2024-08-23 07:58:19

制作一个超级测试类,其中包含用@BeforeClass注释的方法。从该类扩展所有测试类,例如,

@Ignore
public class BaseTest {
    @BeforeClass
    public static void setUpBaseClass() {
        //Do the necessary setup here
    }
}

该方法将在子类中任何带 @BeforeClass 注释的方法之前运行: 来源
确保此方法名称未在任何子类中使用,以防止隐藏。

如果您只需要运行一次(例如,如果您正在创建/初始化一个大型资源供所有测试使用),请在超类中设置一个标志来检查该方法是否运行。

这种设计还将确保如果您更改测试运行程序,则无需更改其他任何内容。

Make a super test class containing the method annotated with @BeforeClass. Extend all the test classes from this class, eg,

@Ignore
public class BaseTest {
    @BeforeClass
    public static void setUpBaseClass() {
        //Do the necessary setup here
    }
}

This method will run before any @BeforeClass annotated method in the subclasses: Source.
Ensure that this method name is not used in any subclass to prevent shadowing.

If you need to run this just once (eg, if you are creating/initializing a large resource for all tests to use), set a flag in the super class to check whether the method ran or not.

This design will also ensure that if you change the test runner, you need not change anything else.

无尽的现实 2024-08-23 07:58:19

对 setUp() 使用 @Before ,对拆解方法使用 @After

编辑:经过一些测试后,我发现 @Before@After 不适用于测试套件。
在您的情况下,您应该使用 @BeforeClass 和静态方法来封装初始化代码。

@RunWith(Suite.class)
@SuiteClasses( { ContactTest.class })
public class AllTests {

    @BeforeClass
    public static void init() {
        System.out.println("Before all");
    }

}

Use @Before for setUp() and @After for tearDown methods.

EDIT: after some tests here I found that @Before and @After does not work for Test Suite.
In your case you should use @BeforeClass and a static method to encapsulate your initialization code.

@RunWith(Suite.class)
@SuiteClasses( { ContactTest.class })
public class AllTests {

    @BeforeClass
    public static void init() {
        System.out.println("Before all");
    }

}
小兔几 2024-08-23 07:58:19

首先:如果您使用的是最新的 JUnit,即 4.7 或更高版本,您也可以使用规则来完成此操作。有关起点,请参阅 http://www.infoq.com/news /2009/07/junit-4.7-规则

据我了解,套件类中的静态块是一个很好的解决方案。

缺点是它在您运行整个套件时被调用,而不是单独的测试或测试类。因此,另一种方法是从所有类中的@BeforeClass方法调用套件类中相同的静态方法。

First of all: if you are up to the latest JUnit, ie 4.7 or higher, you might also get this done with Rules. For a starting point see http://www.infoq.com/news/2009/07/junit-4.7-rules.

To my understanding, a static block in the suite class is a nice solution.

The downside is that it will only be called when your run the entire suit and not separate tests or test classes. So an alternative would be to call the same static method in the suite class from an @BeforeClass methods in all you classes.

望喜 2024-08-23 07:58:19

使用 @BeforeClass 注释方法,使其在所有测试运行之前运行在那堂课上。使用 @Before 注释方法,使其在 之前运行该班级的每个测试。

关于。您的评论,@BeforeClass 效果很好。您是否在该方法中抛出异常?

Annotate a method with @BeforeClass to make it run before all tests run in that class. Annotate a method with @Before to make it run before each test in that class.

re. your comments, @BeforeClass works fine. Are you throwing an exception within that method ?

撩发小公举 2024-08-23 07:58:19

尝试使用@before并创建一个方法来实例化您需要的所有对象,如果您需要在结束后清除某些内容,只需使用@After,

如下所示:

@Before
public void instanceMeasureList() {
    /* all what you need to execute before starting your tests
    */
}

@Test
public void testRequest() {
    fail("Not yet implemented");
}

@Test
public void testEpochDate() {
    fail("Not yet implemented");
}
@After 
public void deleteOutputFile() {
      fail("Not yet implemented");
}

try using @before and creating a method which instances all objects you need if you need to clear something after ending just use @After

something like this:

@Before
public void instanceMeasureList() {
    /* all what you need to execute before starting your tests
    */
}

@Test
public void testRequest() {
    fail("Not yet implemented");
}

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