为什么 Android 测试运行器报告“空测试套件"? [英] Why is the Android test runner reporting "Empty test suite"?

查看:31
本文介绍了为什么 Android 测试运行器报告“空测试套件"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里用头撞墙,试图找出 IntelliJ/Android 报告空测试套件"的原因.我有一个带有两个 IntelliJ 模块的小项目(Eclipse 中的项目").单元测试模块有自己的 AndroidManifest.xml,我已将其粘贴在底部.我正在尝试运行 ActivityUnitTestCase,因为测试将依赖于 Context-object.

I am banging my head against the wall here trying to figure out why IntelliJ/Android is reporting "Empty test suite". I have a small project with two IntelliJ Modules ("Projects" in Eclipse). The Unit test module has its own AndroidManifest.xml, which I have pasted at the bottom. I am trying to run an ActivityUnitTestCase, since the tests will be dependent upon the Context-object.

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

The package name of the main module is nilzor.myapp. The pacakge name of the test module is nilzor.myapp.tests

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

Why is not the test runner detecting the testBlah()-method as a test?

<?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>

这是我的测试课:;

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);
    }
}

我已阅读测试基础活动测试文档,并尝试遵循此Hello world 测试博客,即使它是针对 Eclipse 的.我无法让测试运行程序找到并运行我的测试.我做错了什么?

I have read the testing fundamentals, the activity testing document, and tried following this Hello world test blog, even though it is for Eclipse. I cannot get the test runner to find and run my test. What am I doing wrong?

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

Some of the questions I still feel unsure about are:

  1. 是否需要在单元测试方法上方添加注释?
  2. 我是否需要在方法前加上test"前缀,还是仅用于 JUnit 测试?
  3. 我可以在 nilzor.myapp.tests 的子包中进行测试吗?
  1. Do I need an Annotation above the Unit test method?
  2. Do I need to prefix the method with "test", or is that just for JUnit tests?
  3. Can I have tests in sub-packages of nilzor.myapp.tests?

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

But the main question of this post is why does not the test runner detect my test?

推荐答案

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

You need to provide default constructor for your test class, for example:

package nilzor.myapp.tests;

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

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

关于您的其他问题:

  1. 没有.我的测试仍然在没有任何注释的情况下运行,但我想拥有它们是一个很好的做法.它允许您指定要运行的测试的大小.参见 @SmallTest 的目的是什么Android 中的 、@MediumTest 和 @LargeTest 注释?了解更多详情.

是的,您需要test"前缀.InteliJ 在没有test"前缀时给出method never used"警告,并在测试运行期间跳过该方法.

Yes, you need "test" prefix. InteliJ gives "method never used" warning when there's no "test" prefix, and skips that method during test run.

是的.我将我的测试组织成子包,它似乎运行良好.

Yes. I have my tests organized into subpackages and it seems to be working well.

这篇关于为什么 Android 测试运行器报告“空测试套件"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆