繁体   English   中英

JUnit 5参数化测试:将CSVFileSource与Enclosed.Class一起使用

[英]JUnit 5 parameterized test: Using CSVFileSource with Enclosed.Class

我正在尝试使用以下命令在同一类中运行参数化和非参数化测试用例

@ExtendWith(MockitoExtension.class)
@RunWith(Enclosed.class)

但是不知何故测试没有运行。 我尝试了不带Enclosed类的csvFileSource ,它工作正常。 这是我的测试类骨架的样子:(请帮助)

@ExtendWith(MockitoExtension.class)
@RunWith(Enclosed.class)
public class MyTest {
   static class Base{
   }

   @RunWith(Parameterized.class)
   public static class ParameterizedTests extends Base {
       @ParameterizedTest(name = "testString:{0}")
       @CsvFileSource(resources = "testCases.csv")
       public void test(String testString) {
          ....
       }
   }
}

首先, @RunWith是JUnit 4的注释。运行JUnit Jupiter的@ParameterizedTest时不需要它。 有关详细信息,请参见https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests

接下来, 只有非静态嵌套类(即内部类)才能用作@Nested测试类。 有关详细信息和推理,请参见https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested

最小的工作示例:

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class MyTest {

  class Base {}

  @Nested
  class ParameterizedTests extends Base {
    @ParameterizedTest(name = "testString:{0}")
    @ValueSource(strings = "testCases.csv")
    void test(String testString) {
      System.out.println(testString);
    }
  }
}

暂无
暂无

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

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