我试图通过本文开始使用Espresso FW进行自动化UI测试:
http://developer.android.com/training/testing/ui-testing/espresso-testing.html
所以我安装了所有依赖项,并为MainActivity创建了以下测试类。
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!")));
}
}
但如果我点击&#34;运行活动测试&#34;按钮我总是收到以下错误消息:
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$1.runTest(TestSuite.java:97)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.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
但没有运气。
我如何解决它对主要活动进行简单测试呢?
非常感谢您的任何建议。
答案 0 :(得分:2)
如果要运行JUnit 4样式测试,则需要使用@RunWith(AndroidJUnit4.class)
即
@RunWith(AndroidJUnit4.class)
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
答案 1 :(得分:1)
您需要在功能之前添加test
:
@Test
public void testbuttonShouldUpdateText(){
onView(withId(R.id.goToSecondActivityBtn)).perform(click());
//onView(withId(getResourceId("Click"))).check(matches(withText("Hello world!")));
}
答案 2 :(得分:0)
您应该将buttonShouldUpdateText()
重命名为testButtonShouldUpdateText()
。
所有方法都应以test
开头。
另外,你应该使用JUnit 4样式,它只为要测试的方法添加一个注释。