简体   繁体   English

JUnit:@Before 仅适用于某些测试方法?

[英]JUnit: @Before only for some test methods?

I have some common set up code that I've factored out to a method marked with @Before .我有一些常见的设置代码,我已将其分解为标有@Before的方法。 However, it is not necessary for all this code to run for every single test.但是,不必为每个测试都运行所有这些代码。 Is there a way to mark it so the @Before method only runs before certain tests?有没有办法标记它,以便@Before方法只在某些测试之前运行?

Just move out the tests that don't need the setup code into a separate test class. 只需将不需要安装代码的测试移到单独的测试类中即可。 If you have some other code common to the tests that would be helpful to keep, move that out into a helper class. 如果您有一些测试通用的其他代码,可以帮助保留这些代码,请将其移到帮助程序类中。

Or use TestNG . 或使用TestNG It gives you finer grained control over tests. 它使您可以更好地控制测试。

It is possible to achieve also via Assume from JUnit. 也可以通过JUnit的Assume实现。 And then you can check the method name for which you want to process @Before . 然后,您可以检查要处理@Before的方法名称。

public class MyTest {
     @Rule
     public TestName testName = new TestName();

     @Before
     public void setUp() {
       assumeTrue(testName.getMethodName().equals("myMethodName"));
       // setup follows
     }
}

Check the topic for more insights about @Rule . 检查主题以获取有关@Rule更多见解。

Not sure about @Before, but I recently came up with a strategy for @After block to run selectively. 不确定@Before,但是最近我想出了一个策略,可以有选择地运行@After块。 The implementation was straight forward. 实施是直接的。 I have some flags set to default values as part of the test class. 我将一些标志设置为默认值,作为测试类的一部分。 They are reset to default values in @Before class. 它们被重置为@Before类中的默认值。 In the class I need to do things specific to a flag, I set those flags & in @After I check for flag values to do the respective jobs. 在该类中,我需要做特定于标志的事情,我在@标志中设置了这些标志并检查了标志值以完成各自的工作。

@Nested + @ForEach @嵌套+ @ForEach

Totally agree with the point of moving the related code to an inner class. 完全同意将相关代码移至内部类的观点。 So here what I have done. 所以这就是我所做的。

  1. Create an inner class inside your test class 在测试类中创建一个内部类
  2. Annotate the inner class with @Nested 用@Nested注释内部类
  3. Move all the test methods you want to use in the inner class 移动您要在内部类中使用的所有测试方法
  4. Write the init code inside the inner class and annotate it with @ForEach 在内部类中编写初始化代码,并使用@ForEach对其进行注释

Here is the code: 这是代码:

class Testing {

    @Test
    public void testextmethod1() {

      System.out.println("test ext method 1");

    }

    @Nested
    class TestNest{

       @BeforeEach
       public void init() {
          System.out.println("Init");
       }

       @Test
       public void testmethod1() {
          System.out.println("This is method 1");
       }

       @Test
       public void testmethod2() {
          System.out.println("This is method 2");
       }

       @Test
       public void testmethod3() {
          System.out.println("This is method 3");
       }

     }

     @Test
     public void testextmethod2() {

         System.out.println("test ext method 2");

     }

}

Here is the output 这是输出

test ext method 1
test ext method 2
Init
This is method 1
Init
This is method 2
Init
This is method 3

Note: I am not sure if this is supported in Junit4. 注意:我不确定Junit4是否支持此功能。 I am doing this in JUnit5 我在JUnit5中这样做

JUnit 4.12 provide Enclosed Runner like JUnit 4.12 提供 Enclosed Runner 之类的

@RunWith(Enclosed.class) 
public class GlobalTest{

    @RunWith(MockitoJUnitRunner.class)
    public class InnerTest{
    
    }

}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM