![](/img/trans.png)
[英]How to ensure that CompletableFuture is completely finished before execute a piece of code?
[英]How to execute a piece of code once before all test classes start?
如何在所有类中的所有测试开始之前执行一次方法?
我有一个程序需要在任何测试开始之前设置系统属性。 有没有办法做到这一点?
注意: @BeforeClass
或@Before
仅用于相同的测试 class。 就我而言,我正在寻找一种在所有测试类开始之前执行方法的方法。
如果您需要在所有测试开始之前运行该方法,则应使用注释@BeforeClass
或者如果您每次执行该 class 的测试方法时都需要执行相同的方法,则必须使用@Before
铁
@Before
public void executedBeforeEach() {
//this method will execute before every single test
}
@Test
public void EmptyCollection() {
assertTrue(testList.isEmpty());
}
要为您的测试用例设置前提条件,您可以使用这样的东西 -
@Before
public void setUp(){
// Set up you preconditions here
// This piece of code will be executed before any of the test case execute
}
测试套件
@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
}
}
然后,测试类可以扩展基础 class
public class TestClass extends TestBase {
@Test
public void test() {
// some test
}
}
public class Test2Class extends TestBase {
@Test
public void test() {
// some test
}
}
但是,这将在每个子类每次执行时为其所有子类调用TestBase
中的setup
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.