简体   繁体   English

如何在所有测试类开始之前执行一段代码?

[英]How to execute a piece of code once before all test classes start?

How can I execute a method once before all tests in all classes start?如何在所有类中的所有测试开始之前执行一次方法?

I have a program that needs a system property to be set before any test start.我有一个程序需要在任何测试开始之前设置系统属性。 Is there any way to do that?有没有办法做到这一点?

Note: @BeforeClass or @Before are used just for the same test class.注意: @BeforeClass@Before仅用于相同的测试 class。 In my case, I'm looking for a way to execute a method before all test classes start.就我而言,我正在寻找一种在所有测试类开始之前执行方法的方法。

if you need to run that method before the start of all test you should use the annotation @BeforeClass or if you need to execute the same method every time you will execute a test method of that class you must use @Before如果您需要在所有测试开始之前运行该方法,则应使用注释@BeforeClass或者如果您每次执行该 class 的测试方法时都需要执行相同的方法,则必须使用@Before

fe

@Before
public void executedBeforeEach() {
   //this method will execute before every single test
}

@Test
public void EmptyCollection() {
  assertTrue(testList.isEmpty());     
}

To setup precondition to your test cases you can use something like this -要为您的测试用例设置前提条件,您可以使用这样的东西 -

@Before
public void setUp(){
    // Set up you preconditions here
    // This piece of code will be executed before any of the test case execute 
}

You can make use of a Test Suite.您可以使用测试套件。

The test suite测试套件

@RunWith(Suite.class)
@Suite.SuiteClasses({ TestClass.class, Test2Class.class, })
public class TestSuite {

    @BeforeClass
    public static void setup() {

        // the setup
    }
}

and, the test classes并且,测试类

public class Test2Class {

    @Test
    public void test2() {

        // some test
    }
}
public class TestClass {

    @Test
    public void test() {

        // some test
    }
}

Or, you can have a base class which handles the setup或者,您可以拥有一个基础 class 来处理设置

public class TestBase {

    @BeforeClass
    public static void setup() {

        // setup
    }
}

and, then the test classes can extend the base class然后,测试类可以扩展基础 class

public class TestClass extends TestBase {

    @Test
    public void test() {

        // some test
    }
}
public class Test2Class extends TestBase {

    @Test
    public void test() {

        // some test
    }
}

However, this will call the setup method in TestBase for all its subclasses everytime each of them executes.但是,这将在每个子类每次执行时为其所有子类调用TestBase中的setup方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确保 CompletableFuture 在执行一段代码之前完全完成? - How to ensure that CompletableFuture is completely finished before execute a piece of code? 如何使用Spring Boot运行所有测试类,但仅启动嵌入式Tomcat一次? - How to run all test classes but only start embedded Tomcat once when using Spring Boot? 仅在所有线程完成后如何执行一段代码 - How to execute a piece of code only after all threads are done 在spring上下文初始化开始之前执行一段java代码 - Execute piece of java code before spring context initialization starts 如何随机执行一段代码 - How to execute some piece of code randomly 在Java中运行所有测试类之前,只需设置一次系统属性和静态变量 - Setup system properties and statics just once before running all test classes in java JUnit执行所有包的所有测试类 - JUnit execute all test classes of all packages 在catch子句中为类中的所有方法执行同一段代码 - Execute same piece of code in catch clause for all methods in a class 如何在测试类中为所有测试启动Jersey测试容器(Grizzly)一次? - How to start a Jersey Test Container (Grizzly) once for all tests in a test class? 如何在启动期间仅使一次邮政编码功能一次? - How to make a piece code function only once during launch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM