Skip to content
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
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,22 @@ The following Swift Testing features are supported:
- `#expect` with equality (`==`), inequality (`!=`), comparisons (`>`, `<`, `>=`, `<=`), and boolean expressions
- `#require` for optional unwrapping

Not all Swift Testing features are currently transpiled. Parameterized tests, traits, and tags are not supported.
Not all Swift Testing features are currently transpiled. Traits and tags are not supported.

### Parameterized tests

`@Test(arguments:)` runs each argument as a separate test case with a fresh test instance.

```swift
@Test(arguments: [1, 2, 3])
func positive(value: Int) {
#expect(value > 0)
}
```

Collections of tuples and combinations of two collections are also supported, including async and throwing tests. Argument collections must not depend on instance state. Generic, variadic, and `inout` test parameters are not supported. Empty collections produce a skipped test.

For suites with stored state, prefer a class with a single parameterless initializer. Transpiled structs can have additional constructors that JUnit does not support.

**Note**: The Skip transpiler currently does not have access to the internal API of the module being tested. If you take advantage of Swift's `@testable imports` to exercise internal API, the transpiler will not be able to perform its usual type inference when translating your test code. This just means that you might have to be more explicit about types and to fully-qualify values (e.g. `MyType.value` instead of just `.value`) when unit testing internal API.

Expand Down
44 changes: 44 additions & 0 deletions Sources/SkipUnit/Skip/ParameterizedTesting.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2023–2026 Skip
package skip.unit

import com.google.testing.junit.testparameterinjector.TestParameterValuesProvider
import org.junit.runner.Description
import org.junit.runner.Runner
import org.junit.runner.manipulation.Filter
import org.junit.runner.manipulation.Filterable
import org.junit.runner.manipulation.Sortable
import org.junit.runner.manipulation.Sorter
import org.junit.runner.notification.RunNotifier

/** Uses the existing parameter injector for both host and instrumented tests. */
class SkipParameterizedTestRunner(testClass: Class<*>): Runner(), Filterable, Sortable {
private val delegate: Runner = run {
val runnerName = if (System.getProperty("java.vm.name") == "Dalvik") {
"com.google.testing.junit.testparameterinjector.TestParameterInjector"
} else {
"org.robolectric.RobolectricTestParameterInjector"
}
Class.forName(runnerName).getConstructor(Class::class.java).newInstance(testClass) as Runner
}

override fun getDescription(): Description = delegate.description
override fun run(notifier: RunNotifier) = delegate.run(notifier)
override fun filter(filter: Filter) = (delegate as Filterable).filter(filter)
override fun sort(sorter: Sorter) = (delegate as Sortable).sort(sorter)
}

/** Argument providers are generated from Swift and evaluated in the test's class loader. */
abstract class SkipTestArgumentsProvider: TestParameterValuesProvider() {
abstract fun arguments(): List<List<Any?>>

override fun provideValues(context: Context): List<*> {
val cases = arguments()
if (cases.isEmpty()) {
// JUnit reports an empty collection as skipped rather than an initialization error.
return listOf(value(null).withName("no arguments"))
}
return cases.mapIndexed { index, arguments ->
value(arguments).withName("$index: $arguments")
}
}
}
6 changes: 6 additions & 0 deletions Sources/SkipUnit/Skip/skip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ settings:
- 'library("kotlin-test", "org.jetbrains.kotlin", "kotlin-test").withoutVersion()'
- 'library("kotlin-test-junit", "org.jetbrains.kotlin", "kotlin-test-junit").withoutVersion()'

- 'version("test-parameter-injector", "1.25")'
- 'library("test-parameter-injector", "com.google.testparameterinjector", "test-parameter-injector").versionRef("test-parameter-injector")'

- 'version("robolectric", "4.18.1")'
- 'library("robolectric", "org.robolectric", "robolectric").versionRef("robolectric")'

Expand All @@ -107,6 +110,7 @@ build:
- 'api(testLibs.androidx.test.runner)'
- 'api(testLibs.kotlinx.coroutines.test)'
- 'api(testLibs.kotlin.test.junit)'
- 'api(testLibs.test.parameter.injector)'
- 'testImplementation(testLibs.asm)'
- 'testImplementation(testLibs.robolectric)'
- 'testImplementation(testLibs.androidx.test.core)'
Expand Down Expand Up @@ -146,6 +150,8 @@ build:

- block: 'tasks.withType<Test>().configureEach'
contents:
# Share one Robolectric sandbox between regular and parameterized tests.
- 'systemProperties.put("android.junit.runner", "org.robolectric.RobolectricTestParameterInjector")'
- 'systemProperties.put("robolectric.logging", "stdout")'
- 'systemProperties.put("robolectric.graphicsMode", "NATIVE")'
- block: 'testLogging'
Expand Down
73 changes: 73 additions & 0 deletions Tests/SkipUnitTests/Skip/ParameterizedTestingTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2023–2026 Skip
package skip.unit

import com.google.testing.junit.testparameterinjector.TestParameter
import com.google.testing.junit.testparameterinjector.TestParameterInjector
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.JUnitCore
import org.junit.runner.RunWith

class ParameterizedTestingTests {
class Cases: SkipTestArgumentsProvider() {
override fun arguments(): List<List<Any?>> = listOf(listOf(1), listOf(2), listOf(3))
}

@RunWith(TestParameterInjector::class)
class Fixture {
private var calls = 0

companion object {
var verifyFailure = false
}

@Test
fun independentCases(@TestParameter(valuesProvider = Cases::class) arguments: List<Any?>) {
assertEquals(0, calls++)
if (verifyFailure) assertNotEquals(2, arguments[0])
}
}

@Test
fun failureDoesNotStopOtherCases() {
Fixture.verifyFailure = true
val result = try {
JUnitCore.runClasses(Fixture::class.java)
} finally {
Fixture.verifyFailure = false
}
assertEquals(3, result.runCount)
assertEquals(1, result.failureCount)
assertTrue(result.failures.single().description.displayName.contains("2"))
}
}

private object SharedTestEnvironment {
fun verifyClassLoader() {
val key = "skip.unit.sharedTestEnvironment"
val identity = System.identityHashCode(this).toString()
val previous = System.getProperty(key)
if (previous == null) {
System.setProperty(key, identity)
} else {
assertEquals(previous, identity)
}
}
}

@RunWith(androidx.test.ext.junit.runners.AndroidJUnit4::class)
class RegularEnvironmentTests {
@Test
fun sharesEnvironment() {
SharedTestEnvironment.verifyClassLoader()
}
}

@RunWith(SkipParameterizedTestRunner::class)
class ParameterizedEnvironmentTests {
@Test
fun sharesEnvironment(@TestParameter(valuesProvider = ParameterizedTestingTests.Cases::class) arguments: List<Any?>) {
assertNotNull(arguments)
SharedTestEnvironment.verifyClassLoader()
}
}
Loading