Spek: Programmatically ignore/skip test

Created on 20 Jun 2017  Â·  7Comments  Â·  Source: spekframework/spek

Is there currently a way to ignore/skip tests programmatically? I saw #5, but I'm not sure if anything came out of it.

In TestNG we can simply throw a SkipException to skip a test. For JUnit 4, this StackOverflow post seems to say that if an assumption failed, the test would be skipped. Maybe I'm doing something wrong, but this doesn't seem to be the case with Spek (or maybe in JUnit 5 as a whole)

class SampleSpek : Spek({
    describe("foo") {
        it("should be skipped") {
            assumeTrue(false)
        }
    }
})
org.junit.AssumptionViolatedException: got: <false>, expected: is <true>
question

Most helpful comment

Yes there is a way to have conditional Spek branches:

MySpec : Spek({
    context("something happens") {

        fun SpecBody.itChecksSomethingSometimes(check: Boolean) {
            // You can access local variables, etc.
            if (check) {
                it("is something") {
                    assertThat(something).isEqualTo(expectedThing)
                }
            }
        }

        it("makes the world a better place") {
            assertThat(world).isABetterPlaceNow()
        }

        itChecksSomethingSometimes(checkOrNot)
    }
})

With such extension functions with receiver you can create conditional Spek branches programmatically.

All 7 comments

@thatJavaNerd each scope (given, describe, on, it, etc..) has a version prefixed w/ an x, spek will skip them.

class SampleSpek : Spek({
    describe("foo") {
        xit("should be skipped") {
            assumeTrue(false)
        }
    }
})

Is there a way we can skip these scopes programmatically?

Yes there is a way to have conditional Spek branches:

MySpec : Spek({
    context("something happens") {

        fun SpecBody.itChecksSomethingSometimes(check: Boolean) {
            // You can access local variables, etc.
            if (check) {
                it("is something") {
                    assertThat(something).isEqualTo(expectedThing)
                }
            }
        }

        it("makes the world a better place") {
            assertThat(world).isABetterPlaceNow()
        }

        itChecksSomethingSometimes(checkOrNot)
    }
})

With such extension functions with receiver you can create conditional Spek branches programmatically.

I didn't know that! Is there a way we can skip tests while we're inside an it body? Something like

it("should be skipped") {
    skip()
    assertTrue(false)
}

Not really, probably just skip the body of the test, however you'll still
see it in the report as passed.

I think you better go with dynamic spec branch generation as I showed
above, you can even produce xit() branch to clearly see that it was
ignored.

On Thu, Jun 22, 2017, 04:34 Matt Dean notifications@github.com wrote:

I didn't know that! Is there a way we can skip tests while we're inside an
it body? Something like

it("should be skipped") {
skip()
assertTrue(false)
}

—
You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/JetBrains/spek/issues/219#issuecomment-310250841, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AA7B3OsKIH2Cl8CKNCmGVSDVpw-KaMezks5sGcSPgaJpZM4N_wZ3
.

You could also use test:

```kotlin
fun someCondition(condition: Boolean, reason: String = "no reason") = if (condition) Pending.No else Pending.Yes(reason)

class MySpec: Spek({
test("do something", pending = someCondition()) {
...
}
})

Thank you!

I ended up using something similar to what @artem-zinnatullin suggested:

fun SpecBody.assume(check: () -> Boolean, description: String, body: TestBody.() -> Unit) {
    if (check())
        it(description, body)
    else
        xit(description, reason = "assumption failed", body = body)
}

Are there any plans to include something like this in the API?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pilgr picture pilgr  Â·  3Comments

fabiocarballo picture fabiocarballo  Â·  3Comments

snowe2010 picture snowe2010  Â·  5Comments

ruigoncalo picture ruigoncalo  Â·  7Comments

windyboy picture windyboy  Â·  3Comments