Reputation: 11632
I'm trying to start with Espresso FW for automatized UI testing by this article:
http://developer.android.com/training/testing/ui-testing/espresso-testing.html
So i installed all dependencies and i created following testing class for the 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!")));
}
}
But if i clicked on the "Run activity test" button i always get following error message:
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)
I tried to use different annotations by this article:
http://www.vogella.com/tutorials/AndroidTestingEspresso/article.html
But without the luck.
How can i solve it to run a simple test on Main Activity please ?
Many thanks for any advice.
Upvotes: 1
Views: 1710
Reputation: 77
you should rename your buttonShouldUpdateText()
to testButtonShouldUpdateText()
.
all the method should start with test
.
also, you should use JUnit 4 style,it only add a annotation to the method to be tested.
Upvotes: 0
Reputation: 21
You need to add test
before your function:
@Test
public void testbuttonShouldUpdateText(){
onView(withId(R.id.goToSecondActivityBtn)).perform(click());
//onView(withId(getResourceId("Click"))).check(matches(withText("Hello world!")));
}
Upvotes: 1
Reputation: 4374
If you want to run JUnit 4 style tests, you need to annotate the test class with @RunWith(AndroidJUnit4.class)
i.e.
@RunWith(AndroidJUnit4.class)
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
Upvotes: 2