Skip to content

Commit

Permalink
feat: added support for JUnit Theory tests (#1968)
Browse files Browse the repository at this point in the history
[JUnit Theories](https://junit.org/junit4/javadoc/4.12/org/junit/experimental/theories/Theories.html) were not parsed by Flank perviously. This PR adds support for them.

## Test Plan
> How do we know the code works?
Added test.

## Checklist

- [ ] Documented
- [x] Unit tested
- [ ] Integration tests updated
  • Loading branch information
asadsalman authored May 26, 2021
1 parent 6c6eb53 commit 4b5b92e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import ftl.shard.TestMethod as ShardTestMethod

suspend fun AndroidArgs.createAndroidTestContexts(): List<AndroidTestContext> = resolveApks().setupShards(this)

private val customTestAnnotations = listOf("org.junit.experimental.theories.Theory")

private suspend fun List<AndroidTestContext>.setupShards(
args: AndroidArgs,
testFilter: TestFilter = TestFilters.fromTestTargets(args.testTargets, args.testTargetsForShard)
Expand Down Expand Up @@ -118,7 +120,8 @@ internal fun InstrumentationTestContext.getFlankTestMethods(
testFilter: TestFilter
): List<FlankTestMethod> =
getParametrizedClasses().let { parameterizedClasses: List<TestMethod> ->
DexParser.findTestMethods(test.local).asSequence()
DexParser.findTestMethods(test.local, customTestAnnotations)
.asSequence()
.distinctBy { it.testName }
.filter(testFilter.shouldRun)
.filterNot(parameterizedClasses::belong)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ class CreateAndroidTestContextKtTest {
assertEquals(expected, actual)
}

@Test
fun `should pick up @Theory tests`() {
val testInstrumentationContext = InstrumentationTestContext(
FileReference("", ""),
FileReference("../test_projects/android/apks/app-theory-androidTest.apk", "")
)

val parsedTests = testInstrumentationContext
.getFlankTestMethods(TestFilter("filter", shouldRun = { true }))
.map { it.testName }
.map { it.substringAfterLast("#") }
.toSet()

assertTrue(parsedTests.isNotEmpty())
assertTrue(parsedTests.contains("exampleTheoryTest"))
}

@Test
fun `should filter out methods by distinct name`() {
// given
Expand All @@ -84,7 +101,7 @@ class CreateAndroidTestContextKtTest {

// when
mockkObject(DexParser) {
every { findTestMethods(any()) } returns listOf(
every { findTestMethods(any(), any()) } returns listOf(
TestMethod("testMethod", listOf(mockk(relaxed = true))),
TestMethod("testMethod", listOf(mockk(relaxed = true))),
TestMethod("testMethod", listOf()),
Expand All @@ -110,7 +127,7 @@ class CreateAndroidTestContextKtTest {
)

mockkObject(DexParser) {
every { findTestMethods(any()) } returns listOf(
every { findTestMethods(any(), any()) } returns listOf(
TestMethod("foo.bar.TestClass1#test1", emptyList()),
TestMethod("foo.bar.TestClass1#test2", emptyList()),
TestMethod("foo.bar.TestClass2#test1", emptyList()),
Expand Down

0 comments on commit 4b5b92e

Please sign in to comment.