Skip to content

Commit e236869

Browse files
Rawaalbin-mullvad
authored andcommitted
Add Konsist test to ensure that tests are named correctly
1 parent 81ca0ca commit e236869

File tree

1 file changed

+35
-0
lines changed
  • android/test/arch/src/test/kotlin/net/mullvad/mullvadvpn/test/arch

1 file changed

+35
-0
lines changed

android/test/arch/src/test/kotlin/net/mullvad/mullvadvpn/test/arch/JUnitTest.kt

+35
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package net.mullvad.mullvadvpn.test.arch
22

33
import com.lemonappdev.konsist.api.Konsist
44
import com.lemonappdev.konsist.api.verify.assertEmpty
5+
import com.lemonappdev.konsist.api.verify.assertTrue
56
import org.junit.jupiter.api.Test
67

78
class JUnitTest {
@@ -27,4 +28,38 @@ class JUnitTest {
2728
}
2829
}
2930
.assertEmpty()
31+
32+
@Test
33+
fun `ensure all non android tests are written with spaces`() =
34+
allNonAndroidTests().assertTrue { it.name.contains(' ') }
35+
36+
@Test
37+
fun `ensure all non android tests does start with lower case letter`() =
38+
allNonAndroidTests().assertTrue { it.name.first().isLowerCase() }
39+
40+
@Test
41+
fun `ensure all non android tests have 'ensure' or 'should' in function name`() =
42+
allNonAndroidTests().assertTrue { it.name.containsEnsureOrShould() }
43+
44+
private fun String.containsEnsureOrShould(): Boolean {
45+
return contains("ensure") || contains("should") || contains("then")
46+
}
47+
48+
private fun allNonAndroidTests() =
49+
Konsist.scopeFromTest()
50+
.functions()
51+
// withAnnotationOf is broken in latest Consist version, so we filter manually
52+
// https://github.com/LemonAppDev/konsist/discussions/738
53+
.filter { it.annotations.any { it.text == "@Test" } }
54+
.filter { it.sourceSetName != "androidTest" }
55+
.filter { function ->
56+
ignoredTestPackages.none { function.packagee!!.fullyQualifiedName.startsWith(it) }
57+
}
58+
59+
companion object {
60+
// The following packages are not following the naming convention since they are android
61+
// test that does not support spaces in function names.
62+
private val ignoredTestPackages =
63+
listOf("net.mullvad.mullvadvpn.test.e2e", "net.mullvad.mullvadvpn.test.mockapi")
64+
}
3065
}

0 commit comments

Comments
 (0)