Android Eclipse。在select项目中找不到测试类

时间:2013-12-12 13:52:54

标签: android eclipse junit eclipse-adt android-espresso

我创建测试项目。我的步骤:

  1. 文件 - 新项目;
  2. 选择 - Android测试项目;
  3. 设置名称的项目;
  4. 我想要测试时选择项目;
  5. 选择SDK目标版本;
  6. 单击“完成”。
  7. 完成后,我创建了libs文件夹并添加了espresso-1.0-SNAPSHOT-bundled.jar。项目的结构看起来 enter image description here

    完成后,我创建了测试类:

    public class TestT extends ActivityInstrumentationTestCase2<MainActivity>
    

    {

    public TestT(Class<MainActivity> activityClass)
    {
        super(activityClass);
    }
    
    @BeforeClass
    public static void setUpBeforeClass() throws Exception
    {}
    
    @AfterClass
    public static void tearDownAfterClass() throws Exception
    {}
    
    @Before
    public void setUp() throws Exception
    {}
    
    @After
    public void tearDown() throws Exception
    {}
    
    @Test
    public void test()
    {
        fail("Not yet implemented");
    }
    
    @SmallTest
    public void testTest()
    {
        Espresso.onView(ViewMatchers.withId(R.id.btnClick)).perform(ViewActions.click());
        Espresso.onView(ViewMatchers.withId(R.id.tvClick)).check(ViewAssertions.matches(ViewMatchers.withText(MainActivity.TEXT)));
    }
    

    }

    完成,我运行测试项目:

    1. 右键单击测试项目;
    2. 运行为 - Android JUnit项目;
    3. 项目正在运行,但它不是任何显示。 我打开Window - Show View - Java - JUnit: enter image description here

      双击emulator-5554 show me对话框: enter image description here

      我尝试使用所有版本的Eclipse和ADT插件。我做错了什么?

2 个答案:

答案 0 :(得分:1)

您正在使用JUnit 4.您必须使用JUnit 3,因为Espresso基于Android Instrumentation,目前仅支持JUnit 3.要执行此操作:

  1. 从项目中删除JUnit 4引用
  2. 将测试用例的语法更新为Junit 3,即删除@Test注释并更新您的测试方法,并以“test”为前缀
  3. 实施例: 不正确

    @Test
    public void test()
    {
        fail("Not yet implemented");
    }
    

    实施例: 正确

    public void test()
    {
        fail("Not yet implemented");
    }
    

答案 1 :(得分:0)

您需要使用GoogleInstrumentationTestRunner。请参阅此处的说明:https://code.google.com/p/android-test-kit/wiki/Espresso#Espresso_Setup_Instructions

另外,将测试类构造函数更改为:

public TestT()
{
    super(MainActivity.class);
}