Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test failing scenarios and kotest #145

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
- Makes integration classpath extend test classpath and main classpath (in this order).
- Makes sure IntelliJ idea treats `src/integration/*` as test sources.
- Exposes kotlin internal scope (from main and test module) to integration tests.
- Integrates with [Jacoco](https://docs.gradle.org/current/userguide/jacoco_plugin.html)
- Integrates with test coverage tools like [Jacoco](https://docs.gradle.org/current/userguide/jacoco_plugin.html)
and [Kover](https://github.com/Kotlin/kotlinx-kover).
- Integrates with test frameworks like [JUnit5](https://junit.org/junit5/), [Spock](https://spockframework.org/) and
[Kotest](https://kotest.io/).

## Using the plugin

Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
kotlin("jvm") version "2.0.21"
id("com.coditory.integration-test") version "2.0.2"
id("java-gradle-plugin")
id("maven-publish")
id("com.gradle.plugin-publish") version "1.3.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package com.coditory.gradle.integration

import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION
import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION
import com.coditory.gradle.integration.base.TestProject
import com.coditory.gradle.integration.base.TestProjectBuilder
import org.assertj.core.api.Assertions.assertThat
import org.gradle.testkit.runner.TaskOutcome
import org.junit.jupiter.api.AutoClose
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource

class CommandLineTest {
companion object {
@AutoClose
private val project = createProject()

@AutoClose
private val failingProject = createProject(passingIntgTests = false)

private fun createProject(passingIntgTests: Boolean = true): TestProject {
val name = listOf(
"project",
CommandLineTest::class.simpleName,
if (passingIntgTests) "passing" else "failing",
).joinToString("-")
return TestProjectBuilder
.project(name)
.withBuildGradle(
"""
plugins {
id 'com.coditory.integration-test'
}

repositories {
mavenCentral()
}

dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-api:${Versions.junit}"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${Versions.junit}"
}

tasks.withType(Test) {
useJUnitPlatform()
testLogging {
events("passed", "failed", "skipped")
setExceptionFormat("full")
}
}
""",
).withFile(
"src/integration/java/TestIntgSpec.java",
"""
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

public class TestIntgSpec {
@Test
public void shouldPass() {
assertEquals(true, $passingIntgTests);
}
}
""",
).withFile(
"src/test/java/TestUnitSpec.java",
"""
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

public class TestUnitSpec {
@Test
public void shouldPass() {
assertEquals(true, true);
}
}
""",
)
.build()
}
}

@ParameterizedTest(name = "should run unit tests and integration tests on check command for gradle {0}")
@ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION])
fun `should run unit tests and integration tests on check command`(gradleVersion: String?) {
// when
val result = project.runGradle(listOf("check"), gradleVersion)
// then
assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
}

@ParameterizedTest(name = "should run integration tests on integrationTest command for gradle {0}")
@ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION])
fun `should run integration tests on integrationTest command`(gradleVersion: String?) {
// when
val result = project.runGradle(listOf("integrationTest"), gradleVersion)
// then
assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
}

@ParameterizedTest(name = "should run integration tests on integration command for gradle {0}")
@ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION])
fun `should run integration tests on integration command`(gradleVersion: String?) {
// when
val result = project.runGradle(listOf("integration"), gradleVersion)
// then
assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
}

@Test
fun `should not run integration tests during test task`() {
// when
val result = project.runGradle(listOf("test"))
// then
assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(result.task(":integration")?.outcome).isNull()
}

@Test
fun `should run integration tests and unit tests during testAll task`() {
// when
val result = project.runGradle(listOf("testAll"))
// then
assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
}

@Test
fun `should exclude integration tests on -x integrationTest`() {
// when
val result = project.runGradle(listOf("check", "-x", "integrationTest"))
// then
assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SKIPPED)
}

@Test
fun `should fail check command when integration tests fail`() {
// when
val result = failingProject.runGradleAndFail(listOf("check"))
// then
assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.FAILED)
assertThat(result.task(":check")?.outcome).isNull()
}

@Test
fun `should skip integration tests -PskipIntegrationTest`() {
// when
val result = project.runGradle(listOf("check", "-PskipIntegrationTest"))
// then
assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SKIPPED)
}

@Test
fun `should skip all tests on -PskipTest`() {
// when
val result = project.runGradle(listOf("check", "-PskipTest"))
// then
assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SKIPPED)
assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SKIPPED)
}

@Test
fun `should skip unit tests on -PskipUnitTest`() {
// when
val result = project.runGradle(listOf("check", "-PskipUnitTest"))
// then
assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SKIPPED)
assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.coditory.gradle.integration

import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MAX_SUPPORTED_VERSION
import com.coditory.gradle.integration.base.GradleTestVersions.GRADLE_MIN_SUPPORTED_VERSION
import com.coditory.gradle.integration.base.TestProject
import com.coditory.gradle.integration.base.TestProjectBuilder
import org.assertj.core.api.Assertions.assertThat
import org.gradle.testkit.runner.TaskOutcome
import org.junit.jupiter.api.AutoClose
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource

class JUnitBasicTest {
companion object {
@AutoClose
private val project = createProject()

@AutoClose
private val failingProject = createProject(passingIntgTests = false)

private fun createProject(passingIntgTests: Boolean = true): TestProject {
val name = listOf(
"project",
JUnitBasicTest::class.simpleName,
if (passingIntgTests) "passing" else "failing",
).joinToString("-")
return TestProjectBuilder
.project(name)
.withBuildGradleKts(
"""
plugins {
id("com.coditory.integration-test")
}

repositories {
mavenCentral()
}

dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:${Versions.junit}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${Versions.junit}")
}

tasks.withType<Test> {
useJUnitPlatform()
testLogging {
events("passed", "failed", "skipped")
setExceptionFormat("full")
}
}
""",
).withFile(
"src/integration/java/TestIntgSpec.java",
"""
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestIntgSpec {
@Test
public void shouldPass() {
assertEquals(true, $passingIntgTests);
}
}
""",
).withFile(
"src/test/java/TestUnitSpec.java",
"""
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestUnitSpec {
@Test
public void shouldPass() {
assertEquals(true, true);
}
}
""",
)
.build()
}
}

@ParameterizedTest(name = "should pass unit tests and integration tests on check command for gradle {0}")
@ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION])
fun `should run unit tests and integration tests on check command`(gradleVersion: String?) {
// when
val result = project.runGradle(listOf("check"), gradleVersion)
// then
assertThat(result.task(":test")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
}

@ParameterizedTest(name = "should fail integration tests on test failure for gradle {0}")
@ValueSource(strings = [GRADLE_MAX_SUPPORTED_VERSION, GRADLE_MIN_SUPPORTED_VERSION])
fun `should fail integration tests on test failure`(gradleVersion: String?) {
// when
val result = failingProject.runGradleAndFail(listOf("integration"), gradleVersion)
// then
assertThat(result.task(":integration")?.outcome).isEqualTo(TaskOutcome.FAILED)
}
}
Loading