Java jUnit:在任何测试类之前运行的测试套件代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1967308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java jUnit: Test suite code to run before any test classes
提问by Nick Heiner
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.
我试过了,@BeforeClass
但没有用。
回答by nandokakimoto
Use @Beforefor setUp() and @Afterfor tearDown methods.
setUp()使用@Before,tearDown 方法使用@After。
EDIT: after some tests here I found that @Beforeand @Afterdoes not work for Test Suite. In your case you should use @BeforeClassand a static method to encapsulate your initialization code.
编辑:在这里进行一些测试后,我发现@Before和@After不适用于测试套件。在您的情况下,您应该使用@BeforeClass和一个静态方法来封装您的初始化代码。
@RunWith(Suite.class)
@SuiteClasses( { ContactTest.class })
public class AllTests {
@BeforeClass
public static void init() {
System.out.println("Before all");
}
}
回答by Brian Agnew
Annotate a method with @BeforeClassto make it run before all tests run in that class. Annotate a method with @Beforeto make it run before eachtest in that class.
使用@BeforeClass注释一个方法,使其在该类中的所有测试运行之前运行。使用@Before注释一个方法,使其在该类中的每个测试之前运行。
re. your comments, @BeforeClass works fine. Are you throwing an exception within that method ?
关于。您的评论,@BeforeClass 工作正常。您是否在该方法中抛出异常?
回答by akuhn
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.
首先:如果您使用的是最新的 JUnit,即 4.7 或更高版本,您也可以使用规则来完成这项工作。有关起点,请参阅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 onlybe 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 allyou classes.
缺点是只有在您运行整个套装时才会调用它,而不是单独的测试或测试类。因此,另一种方法是从所有类中的 @BeforeClass 方法调用套件类中的相同静态方法。
回答by Anubhav
Make a super test class containing the method annotated with @BeforeClass. Extend all the test classes from this class, eg,
制作一个包含用@BeforeClass 注释的方法的超级测试类。从这个类扩展所有测试类,例如,
@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.
此方法将在子类中的任何 @BeforeClass 注释方法之前运行:Source。确保此方法名称未在任何子类中使用以防止出现阴影。
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.
这种设计还将确保如果您更改测试运行器,则无需更改任何其他内容。
回答by sebas sierra
try using @before and creating a method which instances all objects you need if you need to clear something after ending just use @After
尝试使用 @before 并创建一个方法来实例化您需要的所有对象,如果您在结束后需要清除某些内容,只需使用 @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");
}