groovy 如何在Vscode中使用Gradle、Junit和Spock运行Spock测试?

bzzcjhmw  于 2022-11-01  发布在  Vscode
关注(0)|答案(1)|浏览(466)

我在工作中尝试从Intellij过渡到vscode。目前,我们有用Groovy(利用Spock框架)编写的测试,Gradle作为构建工具,Junit作为运行者。
在安装了所有相关的扩展(Java、Jave和Gradle的Test Runner)并配置了launch.json文件之后,我仍然无法使用vscode运行测试。
我得到的错误:

Error: Could not find or load main class spock.TestSpec
Caused by: java.lang.ClassNotFoundException: spock.TestSpec

我的launch.json看起来像:

{
    "version": "0.2.0",
    "configurations": [
        {
            "mainClass": "spock.TestSpec",
            "type": "java",
            "name": "Launch Current File",
            "request": "launch",
            "classPaths": [
                "tester.test"
            ]
        }
    ]
}

关于我的系统的一些信息:

第一个
我要运行的测试位于:

src/test/groovy/spock/TestSpec.groovy

TestSpec.groovy

package spock

import base.BaseSpec

class TestSpec{

    def "SimpleTest"() {
        expect:
        name.size() == length

        where:
        name     | length
        "Spock"  | 5
        "Kirk"   | 4
        "Scotty" | 6
    }

gradle.properties

ar_root=tmp
download_location=snapshot
cloudfront_enabled=false
ar_version=*master*
test_scope=quick
test_part=1
Database = "postgresql"
ar_contextUrl=https://se.jd.io/ar
docker_url=http://localhost:4243/
containerized=true

# Credentials for admin user to run the test

test_ar_user=admin
test_ar_password=password

####### PUPPET versions #######

puppet_version.milestone=6.0.9

settings.gradle

rootProject.name = 'federated-repository-tester'

build.gradle

import org.jf.qa.common.CommonEnums
import tasks.StartarTask
import tasks.CollectLogsTask
import tasks.DeleteContainersTask
import org.jf.qa.gradle.tasks.ReportPortalTask
import org.jf.qa.common.VersionUtil
plugins {
    id 'groovy'
}
apply from: 'gradle/common.gradle'

// ar version set for all nodes
def ar_version = getProp("ar_version")
def pr1_ar_version = getProp("pr1_version")
def pr2_ar_version = getProp("pr2_version")
def ha_ar_version = getProp("ha_version")
def test_scope = getProp("test_scope")
String database = getProp("database")
def ar_type = CommonEnums.atp.PRO.toString()

def arAdminPassword = project.hasProperty("ar_admin_password") ? ar_admin_password : System.getenv("ar_admin_password")

def dockerUrl = project.hasProperty("DOCKER_URL") ? DOCKER_URL : "http://localhost:4243/"

def containerized = containerized

// first ar node properties
def arpr1_url = project.hasProperty("arpr1_url") ? project.property("arpr1_url") : null

// second ar node properties
def arpr2_url = project.hasProperty("arpr2_url") ? project.property("arpr2_url") : null

// ha-primary ar node properties
def arHaMaster_url = project.hasProperty("arHaMaster_url") ? project.property("arHaMaster_url") : null

// ha-secondary ar node properties
def arHaSlave_url = project.hasProperty("arHaSlave_url") ? project.property("arHaSlave_url") : null

// ar nginx properties
def arNginx_url = project.hasProperty("arNginx_url") ? project.property("arNginx_url") :null

def dockerRegistry = project.hasProperty("DOCKER_REGISTRY") ? DOCKER_REGISTRY : "qa.jf.io"
def federationApproach = project.hasProperty("federation_approach") ? project.property("federation_approach") : null
def build_number = project.hasProperty("build_number") ? project.property("build_number") : ""
def branch_name = project.hasProperty("branch_name") ? project.property("branch_name") : ""
def ci_type = project.hasProperty("ci_type") ? project.property("ci_type") : "jenkins"
def build_url = project.hasProperty("build_url") ? project.property("build_url") : ""
boolean send_report = Boolean.parseBoolean(getProp("send_report") ?: "false")
String spec = project.hasProperty("Spec") ? project.property("Spec") : ""

ext {
    // arNames, Containers and Urls
    arpr1_container = null
    arpr1_url

    arpr2_container = null
    arpr2_url

    arHaMaster_container = null
    arHaMaster_url

    arHaSlave_container = null
    arHaSlave_url

    arNginx_container = null
    arNginx_url

    db_container = null
}

dependencies {
    compile 'org.spockframework:spock-core:1.3-groovy-2.5'
    compile 'ch.qos.logback:logback-classic:1.2.3'
    //report portal dependencies
    implementation 'com.epam.reportportal:commons-model:5.3.3'
    implementation 'com.epam.reportportal:logger-java-logback:5.1.0-RC-1'
    implementation 'org.jf.qa.report-portal:agent-java-spock:5.1.0-RC-4-jf-SNAPSHOT'
    implementation 'com.epam.reportportal:client-java:5.1.0-RC-12'
    testImplementation(platform('org.junit:junit-bom:5.9.1'))
    testImplementation('org.junit.jupiter:junit-jupiter')
}

task reportPortalTask(type: ReportPortalTask) {
    onlyIf {
        (!VersionUtil.isSnapshot(ar_version) && (branch_name.contains("release") || branch_name.contains("master"))) || send_report
    }

    doFirst {
        HashMap<String,String> attributesMap = new HashMap<String, String>()
        String projectName = project.name + test_scope == "BC" ? "-bc" : ""
        String preReleaseVersion = VersionUtil.getPreReleaseVersion(ar_version)
        String launchName = "${projectName}_${preReleaseVersion}_${spec}"

        attributesMap.put("launch_name", launchName)
        attributesMap.put("project_name", projectName)
        attributesMap.put("test_scope", test_scope)
        attributesMap.put("ar_version", ar_version)
        attributesMap.put("ar_type", ar_type)
        attributesMap.put("build_number", "build-$build_number")
        attributesMap.put("preReleaseVersion", preReleaseVersion)
        if(build_url != ""){
            attributesMap.put("build_url", build_url)
        }

        attributesMap.put("group", "core")
        attributesMap.put("spec", spec)
        attributesMap.put("ci_type", ci_type)
        attributesMap.put("data_base", database)
        attributesMap.put("branch_name", "preRelease/f-$preReleaseVersion-rc")

        setAttributesMap(attributesMap)
        setPropertyFilePath(projectDir.toString() + "/src/test/resources/reportportal.properties")
        setLaunchName(launchName)
    }
}

task startar(type: StartarTask) {
    System.setProperty("project_name", project.name)
    System.setProperty("build_directory", project.getBuildDir().path)
    setarVersion(ar_version)
    setpr1Version(pr1_ar_version)
    setpr2Version(pr2_ar_version)
    setHaVersion(ha_ar_version)
    setDbType(database)
    setFederationApproach(federationApproach)
}

task deleteContainers(type: DeleteContainersTask) {
    setDockerUrl(dockerUrl)

    doFirst {
        List<String> containerNames = createarContainerNames()
        containerNames.add(project.ext.arNginx_container)
        containerNames.add(project.ext.db_container)
        setarContainerNames(containerNames)
    }
}

task collectLogs(type: CollectLogsTask) {
    setDockerUrl(dockerUrl)
    setatp(ar_type)
    setSpec(spec)

    doFirst {
        List<String> arContainerNames = createarContainerNames()
        setarContainerNames(arContainerNames)
        List<String> arUrls = createarUrls()
        setarUrls(arUrls)
    }
}

collectLogs.shouldRunAfter(test)
deleteContainers.shouldRunAfter(collectLogs)

tasks.withType(Test) {

    testLogging {
        events 'started', 'passed', 'skipped', 'failed'
    }

    systemProperty "ar_ADMIN_PASSWORD", arAdminPassword
    systemProperty "arpr1_URL", arpr1_url
    systemProperty "arpr2_URL", arpr2_url
    systemProperty "arHaMaster_URL", arHaMaster_url
    systemProperty "arHaSlave_URL", arHaSlave_url
    systemProperty "ar_NGINX_URL", arNginx_url
    systemProperty "ar_TYPE", ar_type
    systemProperty "ar_VERSION", ar_version
    systemProperty "database", database
    systemProperty "build_directory", buildDir
    systemProperty 'DOCKER_REGISTRY', dockerRegistry
    systemProperty 'DOCKER_URL', dockerUrl

    doFirst {
        systemProperty "arpr1_url", project.ext.arpr1_url ?: getProp("arpr1_url")
        systemProperty "arpr2_url", project.ext.arpr2_url ?: getProp("arpr2_url")
        systemProperty "arHaMaster_url", project.ext.arHaMaster_url ?: getProp("arHaMaster_url")
        systemProperty "arHaSlave_url", project.ext.arHaSlave_url ?: getProp("arHaSlave_url")
        systemProperty "platform_url_pr1", project.ext.platform_url_pr1 ?: getProp("platform_url_pr1")
        systemProperty "platform_url_pr2", project.ext.platform_url_pr2 ?: getProp("platform_url_pr2")
        systemProperty "platform_url_ha", project.ext.platform_url_ha ?: getProp("platform_url_ha")
        systemProperty "arNginx_url", project.ext.arNginx_url ?: getProp("arNginx_url")
        systemProperty 'arpr1_container', project.ext.arpr1_container ?: getProp("arpr1_container")
        systemProperty 'arpr2_container', project.ext.arpr2_container ?: getProp("arpr2_container")
        systemProperty 'arHaMaster_container', project.ext.arHaMaster_container ?: getProp("arHaMaster_container")
        systemProperty 'arHaSlave_container', project.ext.arHaSlave_container ?: getProp("arHaSlave_container")
        systemProperty 'arNginx_container', project.ext.arNginx_container ?: getProp("arNginx_container")
        systemProperty 'db_container', project.ext.db_container ?: getProp("db_container")
        systemProperty 'federation_approach', project.federation_approach ?: getProp("federation_approach")
        systemProperty 'debug_logs', project.debug_logs ?: getProp("debug_logs")
    }

    ignoreFailures = System.getenv('BUILD_NUMBER') != null
}

private String getProp(String prop) {
    if (System.getProperty(prop)) {
        return System.getProperty(prop)
    } else if (System.getenv(prop)) {
        return System.getenv(prop)
    } else {
        return project.getProperties()[prop]
    }
}

private List<String> createarContainerNames() {
    List<String> arContainerNames = []
    arContainerNames.add(project.ext.arpr1_container)
    arContainerNames.add(project.ext.arpr2_container)
    arContainerNames.add(project.ext.arHaMaster_container)
    arContainerNames.add(project.ext.arHaSlave_container)
    return arContainerNames
}

private List<String> createarUrls() {
    List<String> arUrls = []
    arUrls.add(project.ext.arpr1_url)
    arUrls.add(project.ext.arpr2_url)
    arUrls.add(project.ext.arHaMaster_url)
    arUrls.add(project.ext.arHaSlave_url)
    return arUrls
}
ffdz8vbo

ffdz8vbo1#

我没有使用VsCode,建议使用IntelliJ进行开发。这就是我能告诉你的。
Spock类不是主类,您需要使用JUnit的控制台启动器,或者更好地委托Gradle来运行测试。
不幸的是,Spock似乎不受Test Runner for Java扩展的支持。

相关问题