spek icon indicating copy to clipboard operation
spek copied to clipboard

Is it possible to write a parameterized test?

Open RIP21 opened this issue 3 years ago • 8 comments

Will be nice to have an example of one.

Also. Is it possible to make it work with Spring?

RIP21 avatar Nov 29 '20 23:11 RIP21

is that what you are looking for ?

describe("calculator") {
    listOf(
            Triple(8, 12, 20),
            Triple(6, 2, 8),
            Triple(6, -12, -6),
    ).forEach { (a, b, expected) ->
        it("$a + $b should be equal to $expected") {
            assertThat(a + b).isEqualTo(expected)
        }
    }
}

CedrickFlocon avatar Dec 14 '20 14:12 CedrickFlocon

Yup :) Exactly, thanks :)

BTW, any idea why my Spek tests are running no matter what Gradle test pattern I asked?

I mean task is

Task execution finished ':test --tests "com.whatever.graphql.mutation.OnboardingMutationsTest"'.

But it runs all Spek tests that are in different test packages etc. I have such a setup.

tasks.withType<Test> {
    useJUnitPlatform {
        includeEngines = setOf("spek2", "junit-jupiter")
    }
}

So if in IntelliJ I select one of the JUnit tests to run, it runs all Spek tests and the selected test :)

RIP21 avatar Dec 14 '20 14:12 RIP21

@RIP21 I have fixed this before, what JUnit platform and gradle version are you using? If you can reliably replicate it can you create a ticket and attach a reproducer?

raniejade avatar Dec 14 '20 23:12 raniejade

@raniejade which versions of Gradle and and Spek and platform are expected to have this fixed? I'll tell tomorrow what version I have (from phone now)

RIP21 avatar Dec 15 '20 00:12 RIP21

@raniejade

------------------------------------------------------------
Gradle 6.7.1
------------------------------------------------------------

Build time:   2020-11-16 17:09:24 UTC
Revision:     2972ff02f3210d2ceed2f1ea880f026acfbab5c0

Kotlin:       1.3.72
Groovy:       2.5.12
Ant:          Apache Ant(TM) version 1.10.8 compiled on May 10 2020
JVM:          11.0.9.1 (Ubuntu 11.0.9.1+1-Ubuntu-0ubuntu1.20.04)
OS:           Linux 5.3.0-42-generic amd64

build.gradle.kt

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.3.4.RELEASE"
    id("io.spring.dependency-management") version "1.0.10.RELEASE"
    kotlin("jvm") version "1.4.20"
    kotlin("plugin.spring") version "1.4.20"
    kotlin("plugin.allopen") version "1.4.20"
    kotlin("plugin.jpa") version "1.4.20"
}

val spekVersion = "2.0.14"

group "io.whatever"
version "1.0-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
    maven {
        url = uri("https://dl.bintray.com/spekframework/spek/")
    }
}

dependencies {
    implementation("org.jetbrains.kotlin", "kotlin-stdlib-jdk8")
    implementation("org.junit.jupiter", "junit-jupiter", "5.7.0")
    implementation("org.postgresql", "postgresql")
    testImplementation("org.springframework.boot", "spring-boot-starter-test") {
        exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    }
    testImplementation("org.testcontainers", "postgresql", "1.15.1")
    testImplementation("org.testcontainers", "junit-jupiter", "1.15.1")
    testImplementation("org.spekframework.spek2", "spek-dsl-jvm", spekVersion)
    testRuntimeOnly("org.spekframework.spek2", "spek-runner-junit5", spekVersion)
}

tasks.withType<Test> {
    useJUnitPlatform {
        includeEngines = setOf("spek2", "junit-jupiter")
    }
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "11"
    }
}

RIP21 avatar Dec 15 '20 18:12 RIP21

@RIP21 thanks! Can you try using Junit Jupiter 5.6.0 and see if it fixes the issue?

raniejade avatar Dec 17 '20 10:12 raniejade

@raniejade not fixing.

RIP21 avatar Dec 17 '20 11:12 RIP21

Okay, that's weird. Mind filing an issue?

raniejade avatar Dec 17 '20 11:12 raniejade