Java jUnit:在任何测试类之前运行的测试套件代码
我有一个测试套件类:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
制作一个超级测试类,其中包含用@BeforeClass注释的方法。从该类扩展所有测试类,例如,
该方法将在子类中任何带 @BeforeClass 注释的方法之前运行: 来源。
确保此方法名称未在任何子类中使用,以防止隐藏。
如果您只需要运行一次(例如,如果您正在创建/初始化一个大型资源供所有测试使用),请在超类中设置一个标志来检查该方法是否运行。
这种设计还将确保如果您更改测试运行程序,则无需更改其他任何内容。
Make a super test class containing the method annotated with @BeforeClass. Extend all the test classes from this class, eg,
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.
对 setUp() 使用 @Before ,对拆解方法使用 @After 。
编辑:经过一些测试后,我发现 @Before 和 @After 不适用于测试套件。
在您的情况下,您应该使用 @BeforeClass 和静态方法来封装初始化代码。
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.
首先:如果您使用的是最新的 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.
使用 @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 ?
尝试使用@before并创建一个方法来实例化您需要的所有对象,如果您需要在结束后清除某些内容,只需使用@After,
如下所示:
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: