8000 Add junit 5 engine for zio tests. Resolves zio/zio#6724 by vincent-raman · Pull Request #9214 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add junit 5 engine for zio tests. Resolves zio/zio#6724 #9214

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

Merged
merged 1 commit into from
Nov 11, 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
73 changes: 69 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ addCommandAlias(
)
addCommandAlias(
"testJVM",
";coreTestsJVM/test;stacktracerJVM/test;streamsTestsJVM/test;testTestsJVM/test;testMagnoliaTestsJVM/test;testRefinedJVM/test;testRunnerJVM/test;testRunnerJVM/Test/run;examplesJVM/Test/compile;benchmarks/Test/compile;macrosTestsJVM/test;testJunitRunnerTests/test;concurrentJVM/test;managedTestsJVM/test"
";coreTestsJVM/test;stacktracerJVM/test;streamsTestsJVM/test;testTestsJVM/test;testMagnoliaTestsJVM/test;testRefinedJVM/test;testRunnerJVM/test;testRunnerJVM/Test/run;examplesJVM/Test/compile;benchmarks/Test/compile;macrosTestsJVM/test;testJunitRunnerTests/test;testJunitEngineTests/test;concurrentJVM/test;managedTestsJVM/test"
)
addCommandAlias(
"testJVMNoBenchmarks",
Expand Down Expand Up @@ -97,6 +97,8 @@ lazy val rootJVM213 = project
scalafixTests,
testJunitRunner,
testJunitRunnerTests,
testJunitEngine,
testJunitEngineTests,
testMagnolia.jvm,
testMagnoliaTests.jvm,
testRefined.jvm,
Expand All @@ -111,7 +113,9 @@ lazy val rootJVM3 = project
.aggregate(
List[ProjectReference](
testJunitRunner,
testJunitEngine,
// testJunitRunnerTests, TODO: fix test
testJunitEngineTests,
testMagnolia.jvm,
testMagnoliaTests.jvm,
testRefined.jvm,
Expand Down Expand Up @@ -161,7 +165,9 @@ lazy val root213 = project
benchmarks,
scalafixTests,
testJunitRunner,
testJunitRunnerTests
testJunitEngine,
testJunitRunnerTests,
testJunitEngineTests
)) *
)

Expand All @@ -180,7 +186,9 @@ lazy val root3 = project
).flatMap(p => List[ProjectReference](p.jvm, p.js)) ++
List[ProjectReference](
testJunitRunner,
testJunitRunnerTests
testJunitEngine,
testJunitRunnerTests,
testJunitEngineTests
)) *
)

Expand Down Expand Up @@ -494,7 +502,7 @@ lazy val testScalaCheck = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.settings(crossProjectSettings)
.settings(
libraryDependencies ++= Seq(
("org.scalacheck" %%% "scalacheck" % "1.18.0")
"org.scalacheck" %%% "scalacheck" % "1.18.0"
)
)
.jsSettings(jsSettings)
Expand Down Expand Up @@ -586,6 +594,62 @@ lazy val testJunitRunnerTests = project.module
.value
)

lazy val testJunitEngine = project.module
.in(file("test-junit-engine"))
.settings(stdSettings("zio-test-junit-engine"))
Comment on lines +597 to +599
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the rationale behind creating a new module instead of updating the existing one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is different. Test-junit implements a JUnit 4 Runner that allows "junit-vintage" engine (compatibility layer to run JUnit 3 and 4 tests with the JUnit 5 platform) to discover the tests that extend this class. In this case, it's a new JUnit test engine able to discover by itself the ZIO tests

.settings(
libraryDependencies ++= Seq(
"org.junit.platform" % "junit-platform-engine" % "1.11.0",
"org.scala-lang.modules" %% "scala-collection-compat" % "2.12.0"
)
)
.dependsOn(tests.jvm)

lazy val testJunitEngineTests = project.module
.in(file("test-junit-engine-tests"))
.settings(stdSettings("test-junit-engine-tests"))
.settings(Test / fork := true)
.settings(Test / javaOptions ++= {
Seq(
s"-Dproject.dir=${baseDirectory.value}",
s"-Dproject.version=${version.value}",
s"-Dscala.version=${scalaVersion.value}",
s"-Dscala.compat.version=${scalaBinaryVersion.value}"
)
})
.settings(publish / skip := true)
.settings(
libraryDependencies ++= Seq(
"junit" % "junit" % "4.13.2" % Test,
"org.scala-lang.modules" %% "scala-xml" % "2.2.0" % Test,
// required to run embedded maven in the tests
"org.apache.maven" % "maven-embedder" % "3.9.6" % Test,
"org.apache.maven" % "maven-compat" % "3.9.6" % Test,
"com.google.inject" % "guice" % "4.0" % Test,
"org.eclipse.sisu" % "org.eclipse.sisu.inject" % "0.3.5" % Test,
"org.apache.maven.resolver" % "maven-resolver-connector-basic" % "1.9.18" % Test,
"org.apache.maven.resolver" % "maven-resolver-transport-http" % "1.9.18" % Test,
"org.codehaus.plexus" % "plexus-component-annotations" % "2.2.0" % Test,
"org.slf4j" % "slf4j-simple" % "1.7.36" % Test
)
)
.dependsOn(
tests.jvm,
testRunner.jvm
)
// publish locally so embedded maven runs against locally compiled zio
.settings(
Test / Keys.test :=
(Test / Keys.test)
.dependsOn(testJunitEngine / publishM2)
.dependsOn(tests.jvm / publishM2)
.dependsOn(core.jvm / publishM2)
.dependsOn(internalMacros.jvm / publishM2)
.dependsOn(streams.jvm / publishM2)
.dependsOn(stacktracer.jvm / publishM2)
.value
)

lazy val concurrent = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.in(file("concurrent"))
.dependsOn(core)
Expand Down Expand Up @@ -881,6 +945,7 @@ lazy val docs = project.module
concurrent.jvm,
tests.jvm,
testJunitRunner,
testJunitEngine,
testMagnolia.jvm,
testRefined.jvm,
testScalaCheck.jvm,
Expand Down
123 changes: 123 additions & 0 deletions test-junit-engine-tests/maven/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dev.zio</groupId>
<version>1.0</version>
<artifactId>zio_test_junit_engine_test</artifactId>
<packaging>pom</packaging>
<name>${project.artifactId}</name>
<description>Testing ZIO Test Junit engine test project</description>
<inceptionYear>2024</inceptionYear>

<properties>
<encoding>UTF-8</encoding>
<scala.version>2.13.13</scala.version>
<scala.compat.version>2.12</scala.compat.version>
<zio.version>2.0.22</zio.version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for using version 2.0.22?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't matter, it's overwritten by the test with the current version. This is only a template. The concept was taken from "test-junit-tests". The idea is the same as this project: we run simple tests with maven to check that the engine picks them and reports them correctly

</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>dev.zio</groupId>
<artifactId>zio-test_${scala.compat.version}</artifactId>
<version>${zio.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>dev.zio</groupId>
<artifactId>zio_${scala.compat.version}</artifactId>
<version>${zio.version}</version>
</dependency>
<dependency>
<groupId>dev.zio</groupId>
<artifactId>zio-test-junit-engine_${scala.compat.version}</artifactId>
<version>${zio.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.0</version>
</dependency>
</dependencies>

<profiles>
<profile>
<id>scala2</id>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>scala3</id>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala3-library_${scala.compat.version}</artifactId>
<version>${scala.version}</version>
</dependency>
</dependencies>
</profile>
</profiles>


<build>
<plugins>
<plugin>
<!-- see http://davidb.github.com/scala-maven-plugin -->
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.8.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<!--<arg>-make:transitive</arg>-->
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
<arg>-Ywarn-value-discard</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<goal>test</goal>
<parallel>methods</parallel>
<threadCount>10</threadCount>
<includes>
<include>**/*Spec.*</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

</project>

5 changes: 5 additions & 0 deletions test-junit-engine-tests/maven/settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

</settings>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package zio.test.junit.maven

import zio.test.Assertion.equalTo
import zio.test.{Spec, TestEnvironment, assert, ZIOSpecDefault}
import zio.{Scope, Task, ZIO, ZLayer}

trait Ops {
def targetHost: String
}

object OpsTest extends Ops {
override def targetHost: String = null
}

trait MyService {
def readData : Task[List[String]]
}

class MyServiceTest(targetHostName: String) extends MyService {

val url = s"https://${targetHostName.toLowerCase}/ws" // <- null pointer exception here

override def readData: Task[List[String]] = {
ZIO.succeed(List("a","b"))
}
}

object DefectSpec extends ZIOSpecDefault {
override def spec: Spec[TestEnvironment with Scope, Any] = suite("nul test")(
test("test with defect") {
for {
ms <- ZIO.service[MyService]
result <- ms.readData
}
yield assert(result.size)(equalTo(2))
}.provideLayer(ZLayer.succeed(new MyServiceTest(OpsTest.targetHost)))
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package zio.test.junit.maven

import zio.test.junit._
import zio.test._
import zio.test.Assertion._

object FailingSpec extends ZIOSpecDefault {
override def spec = suite("FailingSpec")(
test("should fail") {
assert(11)(equalTo(12))
},
test("should fail - isSome") {
assert(Some(11))(isSome(equalTo(12)))
},
test("should succeed") {
assert(12)(equalTo(12))
}
)
}
Loading
Loading
0