Android:Espresso 测试 FW 抛出 "No tests found.." 异常

Android : Espresso test FW throwing "No tests found.." exception

我正尝试从 Espresso FW 开始,以通过本文进行自动化 UI 测试:

http://developer.android.com/training/testing/ui-testing/espresso-testing.html

所以我安装了所有依赖项,并为 MainActivity 创建了以下测试 class。

import android.support.test.InstrumentationRegistry;
import android.test.ActivityInstrumentationTestCase2;
import org.junit.Before;
import org.junit.Test;

import activities.MainActivity;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;


/**
 * Created by xxx on 19.8.2015.
 */
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

    private MainActivity mActivity;

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

    @Before
    public void setUp() throws Exception {
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        mActivity = getActivity();
    }

    @Test
    public void buttonShouldUpdateText(){
        onView(withId(R.id.goToSecondActivityBtn)).perform(click());
        //onView(withId(getResourceId("Click"))).check(matches(withText("Hello world!")));
    }
}

但是如果我点击 "Run activity test" 按钮,我总是会收到以下错误消息:

Running tests
Test running started
junit.framework.AssertionFailedError: No tests found in com.example.mypackage.test.MainActivityTest
at junit.framework.Assert.fail(Assert.java:50)
at junit.framework.TestSuite.runTest(TestSuite.java:97)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult.protect(TestResult.java:115)
at junit.framework.TestResult.runProtected(TestResult.java:133)
at android.support.test.internal.runner.junit3.DelegatingTestResult.runProtected(DelegatingTestResult.java:90)
at junit.framework.TestResult.run(TestResult.java:118)
at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:49)
at junit.framework.TestCase.run(TestCase.java:124)
at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103)
at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:63)
at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:228)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)

本文尝试使用不同的注解:

http://www.vogella.com/tutorials/AndroidTestingEspresso/article.html

但没有运气。

请问我如何解决 运行 一个简单的 Main Activity 测试?

非常感谢您的任何建议。

如果你想运行 JUnit 4风格的测试,你需要用@RunWith(AndroidJUnit4.class)

注释测试class

@RunWith(AndroidJUnit4.class)
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

您需要在函数前添加test

@Test
public void testbuttonShouldUpdateText(){
    onView(withId(R.id.goToSecondActivityBtn)).perform(click());
    //onView(withId(getResourceId("Click"))).check(matches(withText("Hello world!")));
}

您应该将 buttonShouldUpdateText() 重命名为 testButtonShouldUpdateText()。 所有方法都应以 test.

开头

另外,你应该使用JUnit 4风格,它只是在要测试的方法上添加一个注解。