gpt4 book ai didi

java - Gradle + TestNG:设置要运行的类

转载 作者:行者123 更新时间:2023-12-03 06:08:15 24 4
gpt4 key购买 nike

我正在创建要与TestNG结合使用的Gradle项目。到目前为止,我正在运行以这种形式构造的TestNG文件,以运行测试:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default Suite">
<test name="Test">
<classes>
<class name="package1.class1"></class>
<class name="package2.class2"></class>
</classes>
</test>
</suite>

我想做的是让Gradle构建文件执行与运行此testng.xml文件时当前正在执行的操作相同的操作(即运行类1的测试套件,然后运行类2的套件)。经过一些搜索后,我的build.gradle文件当前如下所示:
apply plugin: 'java'

repositories {
mavenCentral()
}

dependencies {
// add the dependencies as needed
testCompile group: 'org.testng', name: 'testng', version:'6.9.10'
testCompile fileTree('lib')
}

test {

useTestNG() {

// Run the test suite textng.xml, which is in the same directory.
suites 'testng.xml';

include '**/*package1.class1.*'
include '**/*package2.class2.*'
}
}

...以及我的settings.gradle文件,如下所示:
include 'shared'
include 'api'
include 'services:webservice'

rootProject.name = 'Project1'

但是,当我使用此配置运行时,测试未按预期运行。我在此代码中缺少什么?

编辑:在进行更多挖掘之后,我决定尝试直接从Gradle运行TestNG,创建一个新的testng.xml文件:
apply plugin: 'java'

repositories {
mavenCentral()
}

dependencies {

// Add any necessary dependencies.
testCompile group: 'org.testng', name: 'testng', version:'6.9.10'
testCompile fileTree('lib')

compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.5'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.5'
}



task runTests(type: JavaExec, dependsOn: 'classes') {

classpath = files("${projectDir}/src", project.sourceSets.main.compileClasspath,
project.sourceSets.test.compileClasspath,
project.sourceSets.main.runtimeClasspath,
project.sourceSets.test.runtimeClasspath)

main = 'org.testng.TestNG'

args = ["-parallel", "methods", "-threadcount", "1", "-d", "./build/test-output", "testng.xml"]
}

我认为这两种方法都可能可行,除了,即使我已经包括了源目录的位置,我仍然遇到此错误:
Cannot find class in classpath: package1.class1

最佳答案

可能是您应该将包含/排除条件直接放在testng.xml文件中。

关于java - Gradle + TestNG:设置要运行的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37010265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com