-
Notifications
You must be signed in to change notification settings - Fork 38
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
Extend support for JUnit 5 via the new roborazzi-junit5 artifact #355
Open
mannodermaus
wants to merge
7
commits into
takahirom:main
Choose a base branch
from
mannodermaus:feature/full-junit5-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9863427
Add roborazzi-junit5 artifact to bridge the gap for all kinds of JUni…
mannodermaus d532805
Add concurrent tests for verifying that CurrentTestInfo can handle pa…
mannodermaus 93da9db
Reinstate basic detection of JU5 annotation in stack trace strategy
mannodermaus a900982
Add more unit tests to Compose Desktop JVM sample
mannodermaus 715eea9
Introduce Android sample project for using Roborazzi with JUnit 5
mannodermaus 624193f
Accommodate toolchain update to Java 17
mannodermaus 0015851
Bump Robolectric plugin to latest & write a basic doc section on JUnit 5
mannodermaus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# JUnit 5 support | ||
|
||
Roborazzi supports the execution of screenshot tests with JUnit 5, | ||
powered by the combined forces of the [Android JUnit 5](https://github.com/mannodermaus/android-junit5) plugin | ||
and the [JUnit 5 Robolectric Extension](https://github.com/apter-tech/junit5-robolectric-extension). | ||
|
||
### Setup | ||
|
||
To get started with Roborazzi for JUnit 5, make sure to set up your project for the new testing framework first | ||
and add the dependencies for JUnit Jupiter and the Robolectric extension to your project (check the readme files | ||
of either project linked above to find the latest version). Then, add the `roborazzi-junit5` dependency | ||
next to the existing Roborazzi dependency. The complete build script setup looks something like this: | ||
|
||
```kotlin | ||
// Root moduls's build.gradle.kts: | ||
plugins { | ||
id("io.github.takahirom.roborazzi") version "$roborazziVersion" apply false | ||
id("de.mannodermaus.android-junit5") version "$androidJUnit5Version" apply false | ||
id("tech.apter.junit5.jupiter.robolectric-extension-gradle-plugin") version "$robolectricExtensionVersion" apply false | ||
} | ||
``` | ||
|
||
```kotlin | ||
// App module's build.gradle.kts: | ||
plugins { | ||
id("de.mannodermaus.android-junit5") | ||
id("tech.apter.junit5.jupiter.robolectric-extension-gradle-plugin") | ||
} | ||
|
||
dependencies { | ||
testImplementation("org.robolectric:robolectric:$robolectricVersion") | ||
testImplementation("io.github.takahirom.roborazzi:$roborazziVersion") | ||
testImplementation("io.github.takahirom.roborazzi-junit5:$roborazziVersion") | ||
|
||
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion") | ||
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion") | ||
} | ||
``` | ||
|
||
You are now ready to write a JUnit 5 screenshot test with Roborazzi. | ||
|
||
### Write a test | ||
|
||
JUnit 5 does not have a concept of `@Rule`. Instead, extension points to the test framework have to be registered, | ||
and Roborazzi is no exception to this. Add the `@ExtendWith` annotation to the test class and insert both the | ||
extension for Robolectric (from the third-party dependency defined above) and Roborazzi (from `roborazzi-junit5`). | ||
If you also have JUnit 4 on the classpath, make sure to import the correct `@Test` annotation (`org.junit.jupiter.api.Test` | ||
instead of `org.junit.Test`): | ||
|
||
```kotlin | ||
// MyTest.kt: | ||
@ExtendWith(RobolectricExtension::class, RoborazziExtension::class) | ||
class MyTest { | ||
@Test | ||
fun test() { | ||
// Your ordinary Roborazzi setup here, for example: | ||
ActivityScenario.launch(MainActivity::class.java) | ||
onView(isRoot()).captureRoboImage() | ||
} | ||
} | ||
``` | ||
|
||
### Automatic Extension Registration | ||
|
||
You may tell JUnit 5 to automatically attach the `RoborazziExtension` to applicable test classes, | ||
minimizing the redundancy of having to add `@ExtendWith(RoborazziExtension::class)` to every class. | ||
This is done via a process called [Automatic Extension Registration](https://junit.org/junit5/docs/current/user-guide/#extensions-registration-automatic) and must be enabled in the build file. | ||
Be aware that you still need `ExtendWith` for the `RobolectricExtension`, since it is not eligible for | ||
automatic registration. Think of it as the JUnit 5 equivalent of `@RunWith(RobolectricTestRunner::class)`: | ||
|
||
```kotlin | ||
// App module's build.gradle.kts: | ||
junitPlatform { | ||
configurationParameter( | ||
"junit.jupiter.extensions.autodetection.enabled", | ||
"true" | ||
) | ||
} | ||
``` | ||
|
||
```kotlin | ||
// MyTest.kt: | ||
@ExtendWith(RobolectricExtension::class) | ||
class MyTest { | ||
// ... | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...mmonJvmMain/kotlin/com/github/takahirom/roborazzi/StackTraceTestNameExtractionStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.github.takahirom.roborazzi | ||
|
||
import org.junit.Test | ||
import java.lang.reflect.Method | ||
|
||
/** | ||
* Default strategy for finding a suitable output name for [DefaultFileNameGenerator]. | ||
* This implementation looks up the test class and method from the current stack trace. | ||
*/ | ||
internal object StackTraceTestNameExtractionStrategy : TestNameExtractionStrategy { | ||
override fun extract(): Pair<String, String>? { | ||
// Find test method name | ||
val allStackTraces = Thread.getAllStackTraces() | ||
val filteredTraces = allStackTraces | ||
// The Thread Name is come from here | ||
// https://github.com/robolectric/robolectric/blob/40832ada4a0651ecbb0151ebed2c99e9d1d71032/robolectric/src/main/java/org/robolectric/internal/AndroidSandbox.java#L67 | ||
.filterKeys { | ||
it.name.contains("Main Thread") | ||
|| it.name.contains("Test worker") | ||
} | ||
val traceElements = filteredTraces | ||
.flatMap { it.value.toList() } | ||
val stackTraceElement = traceElements | ||
.firstOrNull { | ||
try { | ||
val method = Class.forName(it.className).getMethod(it.methodName) | ||
method.isJUnit4Test() || method.isJUnit5Test() | ||
} catch (e: NoClassDefFoundError) { | ||
false | ||
} catch (e: Exception) { | ||
false | ||
} | ||
} | ||
|
||
return stackTraceElement?.let { | ||
it.className to it.methodName | ||
} | ||
} | ||
|
||
private fun Method.isJUnit4Test(): Boolean { | ||
return getAnnotation(Test::class.java) != null | ||
} | ||
|
||
// This JUnit 5 check works for basic usage of kotlin.test.Test with JUnit 5 | ||
// in basic Compose desktop and multiplatform applications. For more complex | ||
// support including dynamic tests, [JUnit5TestNameExtractionStrategy] is required | ||
@Suppress("UNCHECKED_CAST") | ||
private val jupiterTestAnnotationOrNull = try { | ||
Class.forName("org.junit.jupiter.api.Test") as Class<Annotation> | ||
} catch (e: ClassNotFoundException) { | ||
null | ||
} | ||
|
||
@Suppress("USELESS_CAST") | ||
private fun Method.isJUnit5Test(): Boolean { | ||
return (jupiterTestAnnotationOrNull != null && | ||
(getAnnotation(jupiterTestAnnotationOrNull) as? Annotation) != null) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ore/src/commonJvmMain/kotlin/com/github/takahirom/roborazzi/TestNameExtractionStrategy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.github.takahirom.roborazzi | ||
|
||
interface TestNameExtractionStrategy { | ||
fun extract(): Pair<String, String>? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
plugins { | ||
id 'org.jetbrains.kotlin.jvm' | ||
} | ||
if (System.getenv("INTEGRATION_TEST") != "true") { | ||
pluginManager.apply("com.vanniktech.maven.publish") | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
systemProperty("junit.jupiter.extensions.autodetection.enabled", true) | ||
systemProperty("junit.jupiter.execution.parallel.enabled", true) | ||
} | ||
|
||
dependencies { | ||
// Please see settings.gradle | ||
implementation "io.github.takahirom.roborazzi:roborazzi-core:$VERSION_NAME" | ||
implementation libs.junit.jupiter.api | ||
|
||
testImplementation libs.junit | ||
testImplementation libs.junit.jupiter.api | ||
testImplementation libs.junit.jupiter.params | ||
testRuntimeOnly libs.junit.jupiter.engine | ||
testRuntimeOnly libs.junit.vintage.engine | ||
} |
Empty file.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is easy-to-understand documentation! Thanks!