【问题标题】:Timeout for AndroidJUnitRunner + ActivityInstrumentationTestCase2?AndroidJUnitRunner + ActivityInstrumentationTestCase2 超时?
【发布时间】:2016-10-14 22:27:55
【问题描述】:

设置:

我继承的一个较旧的项目有很多遗留的仪器测试,我想对它们施加超时,因为它们中的很多可以无限期地挂起,这使得很难获得测试报告。我正在将测试更新为 Junit4 样式,但目前它们都在扩展 ActivityInstrumentationTestCase2。

到目前为止尝试过:

AndroidJUnitRunner 的文档中,它说要设置这个标志:

设置将应用于每个测试的超时(以毫秒为单位):-e timeout_msec 5000 ...

... 所有参数也可以通过元数据标签在 AndroidManifest 中指定

我已尝试将 AndroidJUnitRunner 配置添加到应用清单和测试清单,但 timeout_msec 元数据项到目前为止没有任何效果。

【问题讨论】:

    标签: android-instrumentation android-junit


    【解决方案1】:

    您可以使用规则为类中的每个测试提供超时,如下所示。

    @Rule public Timeout timeout = new Timeout(120000, TimeUnit.MILLISECONDS);
    

    您还可以使用以下命令指定每个测试的超时时间

    @Test(timeout = 100) // Exception: test timed out after 100 milliseconds
    public void test1() throws Exception {
        Thread.sleep(200);
    }
    

    您可以使用此链接了解更多关于差异的信息

    https://stackoverflow.com/a/32034936/2128442

    【讨论】:

    • 喜欢因为回答出现在谷歌搜索前 5 名的问题而被拒绝投票。填补空白不应导致反对票,特别是因为这应该是一个有益的社区,而不是愤世嫉俗的社区。​​span>
    猜你喜欢
    • 2016-09-05
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    相关资源
    最近更新 更多