【问题标题】:JUnit4 - Parameterized test extends parameterized base class - need Cartesian product of themJUnit4 - 参数化测试扩展参数化基类 - 需要它们的笛卡尔积
【发布时间】:2021-05-10 20:50:51
【问题描述】:

MyBaseIntegrationTestCase 是一个类,它是大约 150 个测试类的父类。 现在,我想让MyBaseIntegrationTestCase参数化(参数是外部软件的版本)

所以,我把它改成类似

@RunWith(Parameterized.class)
public abstract class MyBaseIntegrationTestCase extends MyBaseTestCase{

 @Parameterized.Parameters(name = "with ExternalSoftware-v{0}")
  public static List<String[]> allVersions() {
    return Arrays.asList( ...);
  }

  @Parameterized.Parameter
  public String externalSoftwareVersion;
  
}

MyBaseIntegrationTestCase 的所有子代都已参数化并成功运行,MyTestClass 除外,声明为

@RunWith(Parameterized::class)
class MyTestClass(override var myType: ParameterType) : MyBaseIntegrationTestCase() {
  companion object {
    @Parameterized.Parameters(name = "with {0}")
    @JvmStatic
    fun allTypes(): Collection<Array<ParameterType>> = ...
}

失败了

java.lang.Exception: 测试类应该有一个公共的零参数构造函数

我想让 MyBaseIntegrationTestCase 的所有子项按照父类中的定义参数化运行,并使 MyTestClass 在 allType 和 allVersions 的笛卡尔积上参数化

不可能使MyTestClass 不扩展MyBaseIntegrationTestCase,这将需要大量的东西来复制粘贴。

您能建议如何在 JUnit4 中执行此操作吗?

【问题讨论】:

    标签: junit junit4 parameterized parameterized-unit-test


    【解决方案1】:

    如果有人感兴趣 类应声明为:

    @RunWith(Parameterized.class)
    public abstract class MyBaseIntegrationTestCase extends MyBaseTestCase{
    
     @Parameterized.Parameters(name = "with ExternalSoftware-v{0}")
      public static List<String[]> allVersions() {
        return Arrays.asList( ...);
      }
    
      @Parameterized.Parameter(0) // important to set index explicitly
      public String externalSoftwareVersion;
      
    }
    

    @RunWith(Parameterized::class)
    class MyTestClass : MyBaseIntegrationTestCase() {
      @Parameterized.Parameter(1) // important to set index explicitly
      var myType: ParameterType
      companion object {
        @Parameterized.Parameters(name = "with {1} on version {0}")
        @JvmStatic
        fun allTypes(): Collection<Array<Any>> = ... //cartesian product should be here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      • 2023-03-29
      • 2017-12-01
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多