spekframework / spek

A specification framework for Kotlin
Other
2.23k stars 179 forks source link

Spek setup ignores Spock and JUnit tests #232

Closed davidsowerby closed 5 years ago

davidsowerby commented 7 years ago

I have both Spock and JUnit tests in folders parallel to the Spek tests, but these are just ignored by both the IDE and Gradle CLI. Is there something else I need to configure for the JUnitPlatform?

Relevant parts of build.gradle:

buildscript { ext.kotlin_version = '1.1.3-2'

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

dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M4'
}

}

apply plugin: 'kotlin' apply plugin: 'org.junit.platform.gradle.plugin'

junitPlatform { filters { engines { include 'spek' } } }

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

version ='0.0.0.1'

dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

testCompile ('org.jetbrains.spek:spek-api:1.1.2') {
    exclude group: 'org.jetbrains.kotlin'
}
testRuntime ('org.jetbrains.spek:spek-junit-platform-engine:1.1.2') {
    exclude group: 'org.junit.platform'
    exclude group: 'org.jetbrains.kotlin'
}

testRuntime "org.junit.platform:junit-platform-launcher:1.0.0-M4" }

raniejade commented 7 years ago

You have to include the correct test engines for them. http://junit.org/junit5/docs/current/user-guide/#dependency-metadata

raniejade commented 7 years ago

If you're using JUnit 4 you just probably need to include the vintage test engine. Make sure it's x.x.x-M4 release.

davidsowerby commented 7 years ago

I added

testRuntime 'org.junit.vintage:junit-vintage-engine:4.12.0-M4'

but that made no difference

davidsowerby commented 7 years ago

Ah, got it, need to add to the filter:

junitPlatform {
    filters {
        engines {
            include 'spek'
            include 'junit-vintage'
        }
    }
}
davidsowerby commented 7 years ago

But that now excludes the Spek tests, I'm getting only Spock and JUnit. Tried this as well:

junitPlatform { filters { engines { include 'spek','junit-vintage' } } }

raniejade commented 7 years ago

@artem-zinnatullin can you help out? Never tried using more that one test engine.

artem-zinnatullin commented 7 years ago

@raniejade we don't have non-Spek JUnit4 tests mixed with Spek…

davidsowerby commented 7 years ago

Ok, I will drop Spek from this project, I have too many Spock and JUnit tests to even consider converting them.

Will we be able to run JUnit / Spock tests in the same build as Spek once #222 is released?

raniejade commented 7 years ago

@davidsowerby I'm going to take a look, it should be possible with the current version.

raniejade commented 7 years ago

@davidsowerby tried it on a personal project.

group = 'org.pandaframework'
version = "0.1.0"

buildscript {
    repositories {
        mavenCentral()
        maven {
            url "https://dl.bintray.com/kotlin/kotlin-eap-1.1"
        }
    }

    dependencies {
        classpath "io.spring.gradle:dependency-management-plugin:1.0.0.RC2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3'
    }
}

apply plugin: "kotlin"
apply plugin: "idea"

repositories {
    mavenCentral()
    maven {
        url "https://dl.bintray.com/jetbrains/spek"
    }
    maven {
        url "https://dl.bintray.com/kotlin/kotlin-eap-1.1"
    }
}

apply plugin: 'org.junit.platform.gradle.plugin'

junitPlatform {
    platformVersion '1.0.0-M3'
    filters {
        engines {
            include 'spek', 'junit-vintage'
        }
    }
}

apply plugin: "io.spring.dependency-management"

dependencyManagement {
    dependencies {
        dependency "com.natpryce:hamkrest:1.1.0.0"

        dependency "com.winterbe:expekt:0.2.0"

        dependencySet(group: 'org.junit.platform', version: '1.0.0-M3') {
            entry 'junit-platform-engine'
            entry 'junit-platform-runner'
            entry 'junit-platform-launcher'
        }

        dependency 'org.junit.vintage:junit-vintage-engine:4.12.0-M3'

        dependencySet(group: 'org.jetbrains.spek', version: '1.1.0-beta3') {
            entry ('spek-api') {
                exclude group: 'org.jetbrains.kotlin'

            }

            entry ('spek-subject-extension') {
                exclude group: 'org.jetbrains.kotlin'

            }

            entry ('spek-junit-platform-engine') {
                exclude group: 'org.jetbrains.kotlin'
            }
        }

        dependency "com.nhaarman:mockito-kotlin:1.2.0"

        dependencySet(group: 'org.jetbrains.kotlin', version: ext.kotlinVersion) {
            entry 'kotlin-stdlib'
            entry 'kotlin-reflect'
        }
    }
}

configurations {
    integrationTestCompile {
        extendsFrom testCompile
    }

    integrationTestRuntime {
        extendsFrom testRuntime
    }
}

sourceSets {
    integrationTest {
        kotlin {
            srcDirs += 'src/integrationTest/kotlin'
        }
        compileClasspath += sourceSets.main.output
    }
}

idea {
    module {
        sourceDirs -= file('src/integrationTest/kotlin')
        testSourceDirs += file('src/integrationTest/kotlin')
    }
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib"
    compile "org.jetbrains.kotlin:kotlin-reflect"

    testCompile 'com.natpryce:hamkrest'
    testCompile 'org.jetbrains.spek:spek-api'
    testCompile 'com.winterbe:expekt'
    testCompile 'com.nhaarman:mockito-kotlin'
    testCompile 'org.jetbrains.spek:spek-subject-extension'

    testCompile 'org.junit.vintage:junit-vintage-engine'

    testRuntime 'org.jetbrains.spek:spek-junit-platform-engine'
}

apply plugin: 'maven-publish'

task sourceJar(type: Jar) {
    from sourceSets.main.allSource
}

publishing {
    publications {
        maven(MavenPublication) {
            artifactId 'panda-ecs'

            from components.java

            artifact sourceJar {
                classifier "sources"
            }
        }
    }
}

When doing ./gradlew test it runs everything - Spek and JUnit4 tests.

davidsowerby commented 7 years ago

@raniejade Thanks for your help on this one, it is much appreciated ... I tried this, but actually could not run any tests at all - I suspect I have missed something, but this setup is very complicated just to run existing tests alongside Spek.

I don't really have much time to spend on this just now - so I will park Spek for now, and try and get back to it later ... or you can close this issue if you wish.

kintomiko commented 6 years ago

@davidsowerby I use gradle 4.6 instead of the gradle plugin, it works for me I mixed junit4 and spek testcases

davidsowerby commented 6 years ago

@kintomiko Thanks for that. I may try that. I did in fact start using Spek, and am gradually migrating tests to it, but have kept them in a separate project for now.

davidsowerby commented 6 years ago

It took me a while to get back to this, but with the setup provided by @raniejade and the Gradle version tip provided by @kintomiko , I now have this working.

I have also updated the versions and am using:

spek 1.1.5 junit-platform: 1.0.3 vintage-engine: 4.12.3

mmitic-lotusflare commented 5 years ago

I have similar issue to this one, I'm trying out Spek in Android studio on existing project, I've done integration as explained here and everything worked fine until I tried running existing JUnit 4 tests. It kept running spek tests instead of junit ones. After some attempts, I've figured out that whenever spek-runner-junit5 dependency is included, IDE(AS 3.2.1) and gradle (4.10.3) run only spek tests and ignore junit ones. Changing junitPlatform/filters/engines to include junit-vintage (or not include spek) have no impact at all. It's looks like spek-runner-junit5 is taking over tests no matter what. Btw, hi @artem-zinnatullin, just saw your KotlinConf 2017 Spek presentation, good work.

raniejade commented 5 years ago

@mmitic-lotusflare it's fixed in master, I'm hoping to release a new version soon - just got back from my vacation.

raniejade commented 5 years ago

Oops, wrong button..

mmitic-lotusflare commented 5 years ago

Thanks, @raniejade, I'll watch the releases.

antonshkurenko commented 5 years ago

It still doesn't work, even after 2.0 release. When we can expect this?

mmitic-lotusflare commented 5 years ago

@raniejade I've tried 2.0.0 release, it's better now, I can run individual JUnit tests (albeit it takes more time to initialize test framework, could be due to switch to JUnit 5). Running group of JUnit tests doesn't work (No tests were found) and "Run Spek tests in ..." has taken over usual "Run tests in ...", regardless of whether target contains Spek tests or not: image

raniejade commented 5 years ago

Can you open another issue? I’ll take a look tonight. If you can also share a sample project, that will be very helpful.

mmitic-lotusflare commented 5 years ago

I'll try when I find the time. What I tested with is a closed-source project, so I'll make a new sample project for this.

argonath88 commented 5 years ago

Do you have something similar for Maven setups? I'm having the same problem of not being able to run JUnit tests (written in Groovy) along with Spek tests (written in Kotlin)

raniejade commented 5 years ago

@argonath88 sorry no, I'm not very familiar with maven.

danielgomezrico commented 4 years ago

I did it fine on gradle and android with:

 testOptions {
        junitPlatform {
            filters {
                engines {
                    include 'spek2'
                    include 'junit-vintage'
                }
        }
}

and:

dependencies {
    testRuntimeOnly "org.junit.vintage:junit-vintage-engine:5.3.2"
}