【问题标题】:gradle junit category additional testtask with different categoriesgradle junit category 不同类别的附加测试任务
【发布时间】:2016-08-10 14:49:54
【问题描述】:

基于How to specify @category in test task in gradle?

我想要两个不同的测试任务:

  • test 用于快速测试
  • testLongRunning 用于快速和慢速测试。

我已经成功地修改了构建任务test,以允许“SlowTest”-s:

org.namespace.some.MySlowTestClass#someReallyLongRunningTest在执行任务“test”时没有按预期执行

我的问题:是否可以添加一个额外的 gradle 任务“testLongRunning”来执行所有测试(包括 org.namespace.some.MySlowTestClass#someReallyLongRunningTest),而 gradle 任务“test”不执行慢速测试?

我的工作示例跳过SlowTest看起来像这样:


// subproject/build.gradle   
apply plugin: 'java'

dependencies {
    testCompile 'junit:junit:4.11'
}

test {
    useJUnit {
        excludeCategories 'org.junit.SlowTest' // userdefined interface in "subproject/src/test/java/org/junit/SlowTest.java"
    }
}

// subproject/src/test/java/org/junit/SlowTest.java
package org.junit;

// @Category see https://stackoverflow.com/questions/38872369/cannot-include-exclude-junit-tests-classes-by-category-using-gradle
public interface SlowTest {
 /* category marker for junit 
    via @Category(org.junit.SlowTest.class) */ 
}

// subproject/src/test/org/namespace/some/MySlowTestClass.java
package org.namespace.some;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.junit.experimental.categories.*;

public class MySlowTestClass {

    // @Category see https://stackoverflow.com/questions/38872369/cannot-include-exclude-junit-tests-classes-by-category-using-gradle
    @Category(org.junit.SlowTest.class)
    @Test
    public void someReallyLongRunningTest(){
    }
}

我尝试过的:

当我将它添加到 subproject/build.gradle 时

// this is line 65
task testLongRunning (type: test){
    dependsOn test
    useJUnit {
        includeCategories 'org.junit.SlowTest'
    }
}

我收到这个错误

FAILURE:构建失败并出现异常。 * 在哪里: 构建文件 '...\subproject\build.gradle' 行:66 * 什么地方出了错: 评估项目“:子项目”时出现问题。 > org.gradle.api.tasks.testing.Test_Decorated 不能强制转换为 java.lang.Class

【问题讨论】:

    标签: java gradle junit task categories


    【解决方案1】:

    看来您的类型可能不正确。尝试将(type: test) 更改为(type: Test)。我认为dependsOn test 试图将test 作为类型传递给我们,而不是将其视为实际任务。

    【讨论】:

      【解决方案2】:

      面临同样的问题。以下对我有用(类似于 Tim VanDoren 的建议):

      test {
          useJUnit {
              includeCategories 'com.common.testing.UnitTest'
          }
      }
      
      task integrationTest (type: Test) { // Use 'Test' instead of 'test' here
          dependsOn test
          useJUnit {
              includeCategories 'com.common.testing.IntegrationTest'
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-03-24
        • 2016-03-13
        • 1970-01-01
        • 2015-01-31
        • 1970-01-01
        • 2020-07-22
        • 2018-10-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多