【问题标题】:When using TestNG with IDEA and Gradle, the TestNG library and the test sources are not recognised, resulting in the tests not being run将 TestNG 与 IDEA 和 Gradle 一起使用时,无法识别 TestNG 库和测试源,导致无法运行测试
【发布时间】:2020-11-17 03:07:16
【问题描述】:

我有以下build.gradle

buildscript {
    ext {
        picocli = '4.3.2'
        janino = '3.1.2'
        questdb = '5.0.1'
        babl = '0.4.1'
        pac4j = '4.0.2'
        eclipse_collections = '10.2.0'
        logback = '1.2.3'
        junit = '4.12'
        testng = '7.1.0'
        kotlin_version = '1.3.72'
    }

    repositories {
        mavenLocal()
        mavenCentral()
        google()
        jcenter()
    }

    dependencies {
    }
}

plugins {
    id 'java'
    id 'org.jetbrains.kotlin.multiplatform' version "$kotlin_version"
}

apply from: 'activej.gradle'
apply from: 'pac4j.gradle'
apply from: 'kotlin.gradle'

description '...'

java {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}

kotlin {
    jvm {
        withJava()
    }
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11

    kotlinOptions {
        jvmTarget = '11'
        apiVersion = '1.3'
        languageVersion = '1.3'
    }
}

test {
    // enable TestNG support (default is JUnit)
    useTestNG()

    // show standard out and standard error of the test JVM(s) on the console
    testLogging.showStandardStreams = true

    // Fail the 'test' task on the first test failure
    failFast = false

    //we want display the following test events
    testLogging {
        events "PASSED", "FAILED", "SKIPPED"
    }
}

dependencies {
    implementation group: 'info.picocli', name: 'picocli', version: "$picocli"

    implementation group: 'org.codehaus.janino', name: 'janino', version: "$janino"

    implementation group: 'org.questdb', name: 'core', version: "$questdb"

    implementation group: 'com.aitusoftware', name: 'babl', version: "$babl", ext: 'pom'

    implementation group: 'org.eclipse.collections', name: 'eclipse-collections-api', version: "$eclipse_collections"
    implementation group: 'org.eclipse.collections', name: 'eclipse-collections', version: "$eclipse_collections"

    implementation group: 'ch.qos.logback', name: 'logback-classic', version: "$logback"

    // testImplementation group: 'junit', name: 'junit', version: "$junit"

    testImplementation group: 'org.testng', name: 'testng', version: "$testng"
}

文件 activej.gradlepac4j.gradle 和 `kotlin.gradle 只有其他的 depedecies。

我的目录结构是:

src
 |- main
      |- java
      |- kotlin
      |- resources
 |- test
      |- java
      |- kotlin
      |- resources

我正在使用 TestNG。

我暂时有一个mock测试文件:

package com.sirinath.activej.config

import org.testng.annotations.*;

@Test
class TestRouteBuilder {
    @Test
    public fun test() {
        System.out.println("Testing")
    }
}

当我执行testClasses 时,我得到:

11:08:29 pm: Executing task 'testClasses'...


> Configure project :server
Kotlin Multiplatform Projects are an experimental feature.

> Task :wrapper

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed

> Configure project :server
Kotlin Multiplatform Projects are an experimental feature.

> Task :server:compileKotlinJvm NO-SOURCE
> Task :server:compileJava NO-SOURCE
> Task :server:jvmProcessResources NO-SOURCE
> Task :server:processResources SKIPPED
> Task :server:classes UP-TO-DATE
> Task :server:jvmMainClasses UP-TO-DATE
> Task :server:compileTestKotlinJvm NO-SOURCE
> Task :server:compileTestJava NO-SOURCE
> Task :server:jvmTestProcessResources NO-SOURCE
> Task :server:processTestResources SKIPPED
> Task :server:testClasses UP-TO-DATE

BUILD SUCCESSFUL in 947ms
11:08:32 pm: Task execution finished 'testClasses'.

我这里有 3 个问题:

  1. 测试未运行。
  2. 当我在src\main\javasrc\main\kotlinsrc\test\kotlin 目录中有代码时,它会显示NO-SOURCE。如果找不到源,我想知道它在哪里寻找,因为有源文件。
  3. IntelliJ IDEA IntelliSense 无法识别org.testng.* 包以及其他依赖项,尽管它显示在 IDEA 的项目结构中。我已经尝试清除 IDE 缓存,但没有任何效果。

【问题讨论】:

  • 如果您使用 IntelliJ IDEA 而不是 gradle 运行测试,它们会运行吗?
  • 我尝试了 IDEA 和类似命令。 TestNG 是如何在 IDEA 的类路径中,但仍然不能通过代码完成访问包的注释。此外,它们在 IDE 中被标记为错误。但是在运行时不会抛出错误。只是测试没有运行。即使在 Gradle 打印 NO-SOURCE 的标准位置,似乎也找不到代码、测试和资源的来源。这可能是未运行测试的原因。
  • 我的意思是我在命令行和 IDE 上都尝试过使用 Gradle。
  • 如果使用 IDE 创建的 TestNG 运行配置运行它们会怎样?
  • 我不确定 IDEA 是否为 Gradle 项目创建了 TestNG 配置。

标签: java kotlin gradle intellij-idea code-completion


【解决方案1】:
  1. id 'org.jetbrains.kotlin.multiplatform' version "$kotlin_version" 更改为id 'org.jetbrains.kotlin.jvm' version "$kotlin_version"
  2. 正在删除
kotlin {
    jvm {
        withJava()
    }
}

解决所有 3 个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2015-12-28
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    相关资源
    最近更新 更多