首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么Android test runner报告“空测试套件”?

为什么Android test runner报告“空测试套件”?
EN

Stack Overflow用户
提问于 2013-01-17 22:50:19
回答 30查看 68.4K关注 0票数 98

我正试图弄清楚为什么IntelliJ/Android报告“空测试套件”。我有一个包含两个IntelliJ模块的小项目(Eclipse中的“项目”)。单元测试模块有它自己的AndroidManifest.xml,我已经将它粘贴在底部。我正在尝试运行ActivityUnitTestCase,因为测试将依赖于Context-object。

主模块的包名为nilzor.myapp。测试模块的包名称为nilzor.myapp.tests

为什么测试运行器没有将testBlah()-method检测为测试?

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<!-- package name must be unique so suffix with "tests" so package loader doesn't ignore us -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="nilzor.myapp.tests"
          android:versionCode="1"
          android:versionName="1.0">
    <!-- We add an application tag here just so that we can indicate that
         this package needs to link against the android.test library,
         which is needed when building test cases. -->
    <application>
        <uses-library android:name="android.test.runner"/>
    </application>
    <!--
    This declares that this application uses the instrumentation test runner targeting
    the package of nilzor.myapp.  To run the tests use the command:
    "adb shell am instrument -w nilzor.myapp.tests/android.test.InstrumentationTestRunner"
    -->
    <instrumentation android:name="android.test.InstrumentationTestRunner"
                     android:targetPackage="nilzor.myapp"
                     android:label="Tests for nilzor.myapp"/>
</manifest>

下面是我的测试类:

代码语言:javascript
复制
package nilzor.myapp.tests;

public class NilzorSomeTest<T extends Activity> extends ActivityUnitTestCase<T>{
    public NilzorSomeTest(Class<T> activityClass){
        super(activityClass);
    }

    @SmallTest
    public void testBlah(){
        assertEquals(1,1);
    }
}

我已经阅读了testing fundamentalsactivity testing document,并尝试使用这个Hello world test blog,尽管它是针对Eclipse的。我无法让测试运行者找到并运行我的测试。我做错了什么?

我仍然不确定的一些问题是:

  1. 单元测试方法上方是否需要批注?
  2. 是否需要在方法前加上" test“前缀,或者这是否仅用于JUnit测试?
  3. 是否可以在nilzor.myapp.tests

的子包中包含测试

但是这篇文章的主要问题是为什么测试运行者没有检测到我的测试

EN

回答 30

Stack Overflow用户

回答已采纳

发布于 2013-01-20 20:42:21

您需要为测试类提供默认构造函数,例如:

代码语言:javascript
复制
package nilzor.myapp.tests;

public class NilzorSomeTest extends ActivityUnitTestCase<ActivityYouWantToTest>{
    public NilzorSomeTest(){
        super(ActivityYouWantToTest.class);
    }

    @SmallTest
    public void testBlah(){
        assertEquals(1,1);
    }
}

关于你的其他问题:

  1. No.我的测试仍然在没有任何注释的情况下运行,但是我想有注释是一个很好的实践。它允许您指定要运行的测试的大小。更多细节见测试。
  2. 是的,你需要“What is the purpose of @SmallTest, @MediumTest, and @LargeTest annotations in Android?”前缀。当没有“InteliJ”前缀时,InteliJ会给出“从未使用过的方法”警告,并在测试运行期间跳过该方法。
  3. 是。我将我的测试组织到子包中,它似乎工作得很好。
票数 70
EN

Stack Overflow用户

发布于 2016-07-05 02:00:05

如果这是“突然”发生的,或者“它在5分钟前还在工作”,我的解决方案是进入运行/调试配置,并删除"Android测试“下的所有配置。有时,如果我重构测试中的类(例如,通过移动到新的包),这些配置就会被破坏。

票数 54
EN

Stack Overflow用户

发布于 2016-09-20 00:54:08

我也遇到过类似的问题。我不确定为什么会发生这种情况,但我可以通过在Android Studio中进入“文件”>“无效缓存/重启”来修复它。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14381694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档