使用静态块

虽然你可以使用 @testSetup 批注指定在执行测试之前运行的方法,但此方法通常只运行一次。如果你需要在每次测试之前运行代码,则可以使用 static 块:

@isTest
public class MyTest {
  static {
    // code here will be run before each test is executed
  }
}