如何在所有类中的所有测试开始之前执行一次方法?
我有一个程序,需要在开始任何测试之前设置系统属性。有什么办法吗?
注意:
\html
<select id="payments" name ="payments" onchange="payment(this.value)">
<option value="0" name ="yearlvllist">-- Payment Type --</option>
{% for type in payment_type %}
<option value="{{type.id}}" name ="payments">
{{ type.name }}</option>
{% endfor%}
{% endfor%}
</select>
或@BeforeClass
仅用于同一测试类。就我而言,我正在寻找一种在所有测试类开始之前执行方法的方法。
答案 0 :(得分:1)
如果您需要在所有测试开始之前运行该方法,则应使用注释@BeforeClass
,或者如果您每次执行该类的测试方法时都需要执行相同的方法,则必须使用{ {1}}
f
@Before
答案 1 :(得分:1)
要为您的测试用例设置前提条件,您可以使用类似这样的内容-
@Before
public void setUp(){
// Set up you preconditions here
// This piece of code will be executed before any of the test case execute
}
答案 2 :(得分:0)
测试套件
@RunWith(Suite.class)
@Suite.SuiteClasses({ TestClass.class, Test2Class.class, })
public class TestSuite {
@BeforeClass
public static void setup() {
// the setup
}
}
以及测试类
public class Test2Class {
@Test
public void test2() {
// some test
}
}
public class TestClass {
@Test
public void test() {
// some test
}
}
public class TestBase {
@BeforeClass
public static void setup() {
// setup
}
}
然后,测试类可以扩展基类
public class TestClass extends TestBase {
@Test
public void test() {
// some test
}
}
public class Test2Class extends TestBase {
@Test
public void test() {
// some test
}
}
但是,每次子类每次执行时,都会在setup
中为其所有子类调用TestBase
方法。