Java 如何在Junit中使用基类@BeforeMethod为所有测试用例执行for循环
假设我有一个Java 如何在Junit中使用基类@BeforeMethod为所有测试用例执行for循环,java,testing,java-8,automation,junit4,Java,Testing,Java 8,Automation,Junit4,假设我有一个Base类: Class BaseClass{ @BeforeClass public static void testBefore(){ //some work done here } } Class TestClass extends BaseClass{ @Test public void testmethod1(){ for (String x: listOfX){ // some other assertions
Base
类:
Class BaseClass{
@BeforeClass
public static void testBefore(){
//some work done here
}
}
Class TestClass extends BaseClass{
@Test
public void testmethod1(){
for (String x: listOfX){
// some other assertions here here
}
}
@Test
public void testmethod2(){
for (String x: listOfX){
// some other assertions here here
}
}
}
我的问题是,在每个
testmethod()
中,避免这些for循环的好方法是什么。是否有办法将此for循环移动到@BeforeMethod
等中,以便testMethod()
每个X
执行一次。非常感谢您的建议,感谢您抽出时间。正如Michael提到的,这不是一个好主意,您应该使用参数化测试。在测试中运行for循环并不可怕,但您肯定不想为每个值创建新的测试。每次启动新测试的开销比重复for循环要慢得多。不要这样做。这只是一个穷人的家