JunitParams在Android 19及更低版本上运行检测测试的例外情况

时间:2017-09-07 20:07:42

标签: android-espresso android-testing

描述

在Android 19及更低版本上使用JunitParams运行程序时出现异常。

重现步骤

  1. 编写简单的测试套件,并使用最新的Android测试支持库在Android 19或更低版本上执行
  2. 代码

    以下测试套件将重现问题

    @RunWith(JUnitParamsRunner.class)
    public class SimpleTest {
        @Test
        @Parameters({ "false", "true" })
        public void simpleParameterizedTest(boolean enabled) {
            // Should execute and pass
        }
    }
    

    预期行为

    测试应该执行并通过。

    实际行为

    测试没有运行,因为"没有找到测试"

    日志

    我检查了日志,发现以下异常。

    09-07 12:17:01.187 3338-3355/com.twilio.video.test W/dalvikvm: Exception Ljava/util/regex/PatternSyntaxException; thrown while initializing Ljunitparams/naming/MacroSubstitutionNamingStrategy;
    09-07 12:17:01.187 3338-3355/com.twilio.video.test E/TestExecutor: Fatal exception when running tests
                                                                       java.lang.ExceptionInInitializerError
                                                                           at junitparams.internal.TestMethod$1.computeValue(TestMethod.java:40)
                                                                           at junitparams.internal.TestMethod$1.computeValue(TestMethod.java:35)
                                                                           at junitparams.internal.Memoizer.get(Memoizer.java:11)
                                                                           at junitparams.internal.TestMethod.describe(TestMethod.java:116)
                                                                           at junitparams.internal.ParameterisedTestClassRunner.describeParameterisedMethod(ParameterisedTestClassRunner.java:161)
                                                                           at junitparams.JUnitParamsRunner.describeMethod(JUnitParamsRunner.java:502)
                                                                           at junitparams.internal.ParametrizedTestMethodsFilter.filteredMethods(ParametrizedTestMethodsFilter.java:30)
                                                                           at junitparams.JUnitParamsRunner.getListOfMethods(JUnitParamsRunner.java:498)
                                                                           at junitparams.JUnitParamsRunner.getDescription(JUnitParamsRunner.java:487)
                                                                           at org.junit.runners.Suite.describeChild(Suite.java:123)
                                                                           at org.junit.runners.Suite.describeChild(Suite.java:27)
                                                                           at org.junit.runners.ParentRunner.shouldRun(ParentRunner.java:434)
                                                                           at org.junit.runners.ParentRunner.filter(ParentRunner.java:382)
                                                                           at org.junit.runner.manipulation.Filter.apply(Filter.java:97)
                                                                           at android.support.test.internal.runner.TestRequestBuilder$LenientFilterRequest.getRunner(TestRequestBuilder.java:413)
                                                                           at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
                                                                           at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
                                                                           at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
                                                                           at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
                                                                        Caused by: java.util.regex.PatternSyntaxException: Look-behind pattern matches must have a bounded maximum length near index 40:
                                                                       (?=\{[^\}]{0,50}\})|(?<=\{[^\}]{0,50}\})
                                                                                                               ^
                                                                           at java.util.regex.Pattern.compileImpl(Native Method)
                                                                           at java.util.regex.Pattern.compile(Pattern.java:411)
                                                                           at java.util.regex.Pattern.<init>(Pattern.java:394)
                                                                           at java.util.regex.Pattern.compile(Pattern.java:381)
                                                                           at junitparams.naming.MacroSubstitutionNamingStrategy.<clinit>(MacroSubstitutionNamingStrategy.java:13)
                                                                            ... 19 more
    

    版本

    以下是我的build.gradle文件的摘录。

    androidTestCompile 'pl.pragmatists:JUnitParams:1.1.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.1'
    androidTestCompile 'com.android.support.test:runner:1.0.1'
    androidTestCompile 'com.android.support.test:rules:1.0.1'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    

2 个答案:

答案 0 :(得分:1)

我们刚刚在JUnitParams中解决了这个问题并发布了新版本。

现在您可以获取最新版本并开始在Android 19及更低版本中使用它!

androidTestCompile 'pl.pragmatists:JUnitParams:1.1.1'

答案 1 :(得分:0)

在Studio的较新版本中,您需要编写:

testImplementation 'pl.pragmatists:JUnitParams:1.1.1'

androidTestImplementation ...不起作用。