diff --git a/README.md b/README.md index ad85313..5075f31 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/Sources/SkipUnit/Skip/ParameterizedTesting.kt b/Sources/SkipUnit/Skip/ParameterizedTesting.kt new file mode 100644 index 0000000..2450184 --- /dev/null +++ b/Sources/SkipUnit/Skip/ParameterizedTesting.kt @@ -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> + + 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") + } + } +} diff --git a/Sources/SkipUnit/Skip/skip.yml b/Sources/SkipUnit/Skip/skip.yml index 99b3e17..e3623f8 100644 --- a/Sources/SkipUnit/Skip/skip.yml +++ b/Sources/SkipUnit/Skip/skip.yml @@ -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")' @@ -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)' @@ -146,6 +150,8 @@ build: - block: 'tasks.withType().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' diff --git a/Tests/SkipUnitTests/Skip/ParameterizedTestingTests.kt b/Tests/SkipUnitTests/Skip/ParameterizedTestingTests.kt new file mode 100644 index 0000000..73cbac1 --- /dev/null +++ b/Tests/SkipUnitTests/Skip/ParameterizedTestingTests.kt @@ -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> = 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) { + 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) { + assertNotNull(arguments) + SharedTestEnvironment.verifyClassLoader() + } +}