java android JUnit 测试未运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4905182/
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 JUnit Tests not running
提问by Prime
The problem is not that the tests are failing, but that they simply do not run at all. That is to say, the console tells me they run, but I see absolutely no results from them at all.
问题不在于测试失败,而在于它们根本没有运行。也就是说,控制台告诉我它们在运行,但我完全看不到它们的结果。
Note that I 'have' remembered to annotate methods with @Test
请注意,我“记得”用@Test 注释方法
Here's the code for the test class:
这是测试类的代码:
package module.jakway.JournalEntry.test;
import module.jakway.JournalEntry.Module_JournalEntry;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class MainTest extends android.test.ActivityInstrumentationTestCase2<Module_JournalEntry>
{
public MainTest(Class activityClass) {
super("module.jakway.JournalEntry", Module_JournalEntry.class);
// TODO Auto-generated constructor stub
}
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
super.setUp();
Module_JournalEntry journalentry = getActivity();
assertTrue(true);
}
@After
public void tearDown() throws Exception {
}
@Test
public void myTestCase()
{
assertTrue(false);
}
}
The project I'm testing is named Module_JournalEntry with package module.jakway.JournalEntry and Activity Module_JournalEntry.java
我正在测试的项目名为 Module_JournalEntry 包 module.jakway.JournalEntry 和 Activity Module_JournalEntry.java
Here's the console output:
这是控制台输出:
[2011-02-04 20:37:10 - Module_JournalEntryTest] Performing android.test.InstrumentationTestRunner JUnit launch
[2011-02-04 20:37:10 - Module_JournalEntryTest] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'my2.3Emulator'
[2011-02-04 20:37:12 - Module_JournalEntryTest] Application already deployed. No need to reinstall.
[2011-02-04 20:37:12 - Module_JournalEntryTest] Project dependency found, installing: Module_JournalEntry
[2011-02-04 20:37:14 - Module_JournalEntry] Application already deployed. No need to reinstall.
[2011-02-04 20:37:14 - Module_JournalEntryTest] Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
[2011-02-04 20:37:14 - Module_JournalEntryTest] Collecting test information
[2011-02-04 20:37:17 - Module_JournalEntryTest] Sending test information to Eclipse
[2011-02-04 20:37:17 - Module_JournalEntryTest] Running tests...
[2011-02-04 20:37:19 - Module_JournalEntryTest] Test run finished
and the logcat output:
和 logcat 输出:
02-04 20:37:10.266: DEBUG/AndroidRuntime(524): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
02-04 20:37:10.266: DEBUG/AndroidRuntime(524): CheckJNI is ON
02-04 20:37:11.236: DEBUG/AndroidRuntime(524): Calling main entry com.android.commands.pm.Pm
02-04 20:37:11.316: DEBUG/AndroidRuntime(524): Shutting down VM
02-04 20:37:11.336: INFO/AndroidRuntime(524): NOTE: attach of thread 'Binder Thread #3' failed
02-04 20:37:11.346: DEBUG/dalvikvm(524): GC_CONCURRENT freed 102K, 71% free 297K/1024K, external 0K/0K, paused 3ms+8ms
02-04 20:37:11.346: DEBUG/jdwp(524): Got wake-up signal, bailing out of select
02-04 20:37:11.346: DEBUG/dalvikvm(524): Debugger has detached; object registry had 1 entries
02-04 20:37:12.316: DEBUG/AndroidRuntime(534): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
02-04 20:37:12.316: DEBUG/AndroidRuntime(534): CheckJNI is ON
02-04 20:37:13.136: DEBUG/AndroidRuntime(534): Calling main entry com.android.commands.pm.Pm
02-04 20:37:13.186: DEBUG/AndroidRuntime(534): Shutting down VM
02-04 20:37:13.216: INFO/AndroidRuntime(534): NOTE: attach of thread 'Binder Thread #3' failed
02-04 20:37:13.216: DEBUG/dalvikvm(534): GC_CONCURRENT freed 102K, 71% free 297K/1024K, external 0K/0K, paused 1ms+1ms
02-04 20:37:13.216: DEBUG/dalvikvm(534): Debugger has detached; object registry had 1 entries
02-04 20:37:14.256: DEBUG/AndroidRuntime(544): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
02-04 20:37:14.256: DEBUG/AndroidRuntime(544): CheckJNI is ON
02-04 20:37:15.126: DEBUG/AndroidRuntime(544): Calling main entry com.android.commands.am.Am
02-04 20:37:15.176: INFO/ActivityManager(75): Force stopping package module.jakway.JournalEntry uid=10035
02-04 20:37:15.206: INFO/ActivityManager(75): Start proc module.jakway.JournalEntry for added application module.jakway.JournalEntry: pid=552 uid=10035 gids={1015}
02-04 20:37:15.876: WARN/TestGrouping(552): Invalid Package: '' could not be found or has no tests
02-04 20:37:15.976: INFO/ActivityManager(75): Force stopping package module.jakway.JournalEntry uid=10035
02-04 20:37:15.976: INFO/Process(75): Sending signal. PID: 552 SIG: 9
02-04 20:37:15.986: DEBUG/AndroidRuntime(544): Shutting down VM
02-04 20:37:16.016: DEBUG/dalvikvm(544): GC_CONCURRENT freed 103K, 71% free 299K/1024K, external 0K/0K, paused 1ms+4ms
02-04 20:37:16.036: INFO/AndroidRuntime(544): NOTE: attach of thread 'Binder Thread #3' failed
02-04 20:37:16.046: DEBUG/jdwp(544): Got wake-up signal, bailing out of select
02-04 20:37:16.046: DEBUG/dalvikvm(544): Debugger has detached; object registry had 1 entries
02-04 20:37:16.656: DEBUG/AndroidRuntime(563): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
02-04 20:37:16.665: DEBUG/AndroidRuntime(563): CheckJNI is ON
02-04 20:37:17.646: DEBUG/AndroidRuntime(563): Calling main entry com.android.commands.am.Am
02-04 20:37:17.716: INFO/ActivityManager(75): Force stopping package module.jakway.JournalEntry uid=10035
02-04 20:37:17.746: INFO/ActivityManager(75): Start proc module.jakway.JournalEntry for added application module.jakway.JournalEntry: pid=572 uid=10035 gids={1015}
02-04 20:37:18.606: WARN/TestGrouping(572): Invalid Package: '' could not be found or has no tests
02-04 20:37:18.826: INFO/ActivityManager(75): Force stopping package module.jakway.JournalEntry uid=10035
02-04 20:37:18.826: INFO/Process(75): Sending signal. PID: 572 SIG: 9
02-04 20:37:18.846: DEBUG/AndroidRuntime(563): Shutting down VM
02-04 20:37:18.906: INFO/AndroidRuntime(563): NOTE: attach of thread 'Binder Thread #4' failed
02-04 20:37:18.916: DEBUG/dalvikvm(563): GC_CONCURRENT freed 103K, 71% free 298K/1024K, external 0K/0K, paused 2ms+28ms
02-04 20:37:18.916: DEBUG/jdwp(563): Got wake-up signal, bailing out of select
02-04 20:37:18.926: DEBUG/dalvikvm(563): Debugger has detached; object registry had 1 entries
Thanks very much!
非常感谢!
回答by Diego Torres Milano
Android bases its testing framework on JUnit 3 which doesn't use annotations but reflection and test method names should include the testprefix.
Android 的测试框架基于 JUnit 3,它不使用注释,但反射和测试方法名称应包含测试前缀。
Move to JUnit 3 and run your tests from Eclipse as Run As -> Android JUnit Test
or using am instrument
from command line.
转移到 JUnit 3 并从 Eclipse 运行您的测试作为Run As -> Android JUnit Test
或am instrument
从命令行使用。
回答by Stan Sidel
Try changing this:
尝试改变这个:
public MainTest(Class activityClass) {
super("module.jakway.JournalEntry", Module_JournalEntry.class);
// TODO Auto-generated constructor stub
}
To this:
对此:
public MainTest() {
super("module.jakway.JournalEntry", Module_JournalEntry.class);
// TODO Auto-generated constructor stub
}
The constructor should have no parameters in this case. It's written in the Hello, Testingtutorial example under the Adding the test case constructorsection.
在这种情况下,构造函数应该没有参数。它是在Hello, Testing教程示例中编写的,位于添加测试用例构造函数部分下。
回答by Steven Mark Ford
"The Android testing API supports JUnit 3 code style, but not JUnit 4" - http://developer.android.com/tools/testing/testing_android.html
“Android 测试 API 支持 JUnit 3 代码样式,但不支持 JUnit 4” - http://developer.android.com/tools/testing/testing_android.html
1.Update your test to follow JUnit 3 standards
1.更新您的测试以遵循 JUnit 3 标准
//has to start with the word "test" for JUnit 3 to pick it up. public void testMyCase() { assertTrue(false); }
2.Remove any JUnit 4 references from the test project (right click project -> properties -> java build path -> Libraries (tab)
2.从测试项目中删除任何 JUnit 4 引用(右键单击项目 -> 属性 -> java 构建路径 -> 库(选项卡)