Android 工作室说 AndroidTestCase 为“空测试套件”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20636471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android studio says "Empty Test Suite" for AndroidTestCase
提问by Pratik Mandrekar
I have created an example test case that extends AndroidTestCase. When I run the test case, it errors out by saying
我创建了一个扩展 AndroidTestCase 的示例测试用例。当我运行测试用例时,它说
Running tests
Test running startedTest running failed:
Instrumentation run failed due to 'java.lang.RuntimeException'
Empty test suite.
The test case
测试用例
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import static org.junit.Assert.assertEquals;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.lang.Exception;
import java.lang.Override;
public class DateFormatTest extends AndroidTestCase{
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public DateFormatTest(){
super(DateFormatTest.class);
}
@SmallTest
public void testMultiply() {
assertEquals("10 x 5 must be 50", 50, 10*5);
}
}
回答by Toerndev
Since nobody else mentions it: methods in AndroidTestCase
subclasses need to be public
and have names starting with "test"!
由于没有其他人提到它:AndroidTestCase
子类中的方法需要以public
“test”开头!
The OP and the answers all got this right but I missed it and got the exact same error.
OP 和答案都正确,但我错过了它并得到了完全相同的错误。
回答by emidander
I got this error when running tests from Android Studio. Turned out I had placed my test cases in the wrong folder. When running Gradle/Android Studio, all Android tests should be in the folder src/instrumentTest/java
.
从 Android Studio 运行测试时出现此错误。结果我把我的测试用例放在了错误的文件夹中。当运行 Gradle/Android Studio 时,所有的 Android 测试都应该在文件夹中src/instrumentTest/java
。
Edit:In Gradle plugin version 0.9 or later the correct name of the folder is androidTest
. (http://tools.android.com/tech-docs/new-build-system/migrating_to_09)
编辑:在 Gradle 插件版本 0.9 或更高版本中,文件夹的正确名称是androidTest
. (http://tools.android.com/tech-docs/new-build-system/migrating_to_09)
回答by kuhnmi
I understand that this question is old, and the development tools have changed significantly since this question has been asked.
我知道这个问题很老了,自从提出这个问题以来,开发工具已经发生了重大变化。
However, I had a similar issue (in AndroidStudio 2.1.1) and it turned out that it was just the error message that was quite misleading. For me it said:
但是,我遇到了类似的问题(在 AndroidStudio 2.1.1 中),结果证明这只是具有误导性的错误消息。对我来说,它说:
Test running started
Test running failed: Instrumentation run failed due to 'java.lang.IllegalStateException'
Empty test suite.
(Note: The difference to the original question is in IllegalStateException vs. RuntimeException)
(注意:与原始问题的区别在于 IllegalStateException 与 RuntimeException)
It turned out that this IllegalStateException
was actually thrown in the initialization of the application context as can be seen by inspecting the logcat output. As a result, no test-methods were run, and Android Studio writes this somewhat misleading "Empty test suite" error.
事实证明,这IllegalStateException
实际上是在应用程序上下文的初始化中抛出的,通过检查 logcat 输出可以看出。结果,没有运行任何测试方法,Android Studio 编写了这个有点误导性的“空测试套件”错误。
I.e. "Empty test suite" can mean what it actually says (you don't have any test methods declared, e.g. because you forgot to annotate them with @Test
), but it can also mean that something (like a runtime exception thrown in your application initialization code) prevents the test-runner from reaching any test methods (which seems to be your case).
即“空测试套件”可能意味着它实际上说(你没有任何的测试方法申报,如因您忘了与注释它们@Test
),但它也可能意味着某些因素(如在应用程序初始化抛出一个运行时异常代码)阻止测试运行程序访问任何测试方法(这似乎是您的情况)。
Check adb-logcat and search for RuntimeException
s. This will probably find you the root-cause of your problem.
检查 adb-logcat 并搜索RuntimeException
s。这可能会为您找到问题的根本原因。
回答by dabluck
I got this error because one of my test methods didn't include "throws exception" after the method signature. might help somebody
我收到此错误是因为我的一个测试方法在方法签名后没有包含“抛出异常”。可能会帮助某人
回答by ahmed_khan_89
I got the same issue. I was having testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
in my defaultConfig {...}
. I have just removed that line, and now it's working fine (The IDE is picking the right runner config on build time).
我遇到了同样的问题。我testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
在我的defaultConfig {...}
. 我刚刚删除了那条线,现在它工作正常(IDE 在构建时选择了正确的运行器配置)。
I hope this will help someone.
我希望这会帮助某人。
回答by Lukas Hanacek
My problem was I had default constructor generated by android studio, looked like this
我的问题是我有 android studio 生成的默认构造函数,看起来像这样
public class SomeTest extends ActivityUnitTestCase<ActivityYouWantToTest>{
public SomeTest(Class<NewsActivity> activityClass) {
super(activityClass);
}
}
and I had to change it to this to get rid of the problem
我不得不把它改成这个来解决这个问题
public class SomeTest extends ActivityUnitTestCase<ActivityYouWantToTest>{
public SomeTest() {
super(ActivityYouWantToTest.class);
}
}
回答by MariusVolkhart
We use the AndroidTestCase
class to define that we are testing components which are specific to Android. The main benefit of AndroidTestCase
is that it gives us access to the application's Resources such as strings and layouts, etc.
我们使用AndroidTestCase
该类来定义我们正在测试特定于 Android 的组件。的主要好处AndroidTestCase
是它让我们可以访问应用程序的资源,例如字符串和布局等。
The AndroidTestCase
does not require you to overwrite the default constructor as it does not provide a particular Activity
as the Context
, but rather provides you a general one so you can still call getContext()
.
TheAndroidTestCase
不要求您覆盖默认构造函数,因为它不提供特定Activity
的Context
,而是为您提供一个通用的,以便您仍然可以调用getContext()
.
In your example, you are not using any Android components, so for you, the following would make sense:
在您的示例中,您没有使用任何 Android 组件,因此对您而言,以下内容是有意义的:
import android.test.suitebuilder.annotation.SmallTest;
import junit.framework.TestCase;
public class DateFormatTest2 extends TestCase {
@SmallTest
public void testMultiply() {
assertEquals("10 x 5 must be 50", 50, 10 * 5);
}
}
Notice the use of TestCase
rather than AndroidTestCase
.
注意使用TestCase
而不是AndroidTestCase
。
For AndroidTestCase
to be applicable, a test that requires resources would be necessary:
为了AndroidTestCase
适用,需要资源的测试是必要的:
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
public class DateFormatTest extends AndroidTestCase {
@SmallTest
public void testAppTitle() {
assertEquals("MyApp", getContext().getResources().getString(R.string.app_name));
}
}
Here we use the AndroidTestCase
because we need to access the application's resources.
这里我们使用 ,AndroidTestCase
因为我们需要访问应用程序的资源。
回答by Gal Bracha
This guide might help - http://www.slideshare.net/tobiaspreuss/how-to-setup-unit-testing-in-android-studio
本指南可能会有所帮助 - http://www.slideshare.net/tobiaspreuss/how-to-setup-unit-testing-in-android-studio
On the latest gradle (0.9+) the test should be under androidTestdir
在最新的 gradle (0.9+) 上,测试应该在androidTest目录下
Also in your gradle.build:
同样在您的 gradle.build 中:
dependencies {
androidTestCompile 'junit:junit:4.+'
}
also add those under defaultConfig{
还添加那些在defaultConfig{
testPackageName "test.java.foo"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
}
回答by r-hold
Did you configure your testRunner in your gradleConfig? We use different TestRunners for different tests (to speed things up. My config looks like this
您是否在 gradleConfig 中配置了 testRunner?我们使用不同的 TestRunners 进行不同的测试(以加快速度。我的配置看起来像这样
android {
// Some stuff
defaultConfig {
// Some other stuff
//junit test
testInstrumentationRunner "de.qabel.qabelbox.QblJUnitRunner"
//ui tests
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
If i disable one of this lines the corresponding test will also report "Empty Test Suite".
如果我禁用此行之一,相应的测试还将报告“空测试套件”。
回答by StackTrace
I already have a default constructor in my test case but still it was giving me error "Empty test suite" and was stuck at "Instantiating tests...".
我的测试用例中已经有一个默认构造函数,但它仍然给我错误“空测试套件”并且卡在“实例化测试......”。
Tried creating new workspace, resetting Android Studio, but that didn't work.
尝试创建新工作区,重置 Android Studio,但这没有用。
Finally, close Android SDK and emulator.
最后,关闭 Android SDK 和模拟器。
Go to your android-sdks/platform-tools.
Clear all Android temp files with these commands:
a.
rm -rf ~/Library/Application Support/AndroidStudio
b.
rm -rf ~/Library/Caches/AndroidStudio
c.
rm -rf ~/Library/Application Support/AndroidStudio
d.
rm -rf ~/Library/Preferences/AndroidStudio
Run:
./adb kill-server ./adb start-server
Start Android and run test case.
转到您的 android-sdks/platform-tools。
使用以下命令清除所有 Android 临时文件:
一种。
rm -rf ~/Library/Application Support/AndroidStudio
湾
rm -rf ~/Library/Caches/AndroidStudio
C。
rm -rf ~/Library/Application Support/AndroidStudio
d.
rm -rf ~/Library/Preferences/AndroidStudio
跑:
./adb kill-server ./adb start-server
启动 Android 并运行测试用例。