I am trying to run following Android JUnit3 test with robotium:
import android.test.ActivityInstrumentationTestCase2;
import com.package.sample.MyActivityClass;
import com.jayway.android.robotium.solo.Solo;
public class TestSample extends ActivityInstrumentationTestCase2<MyActivityClass> {
private Solo solo;
public TestSample() {
super("com.package.sample", MyActivityClass.class);
}
protected void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
public void clickbutton1() throws Exception{
solo.clickOnButton("abc");
solo.setActivityOrientation(Solo.LANDSCAPE);
assertTrue(solo.searchText("load"));
}
#Override
protected void tearDown() throws Exception{
solo.finishOpenedActivities();
}
}
The JUnit explores shows 0/0 tests runs. The debugger does not hit any code path mentioned here and Console outputs "test run finished"??:
Uploading MyAppTest.apk onto device 'emulator-5554'
Installing MyAppTest.apk...
Success!
Project dependency found, installing: MyApp
Uploading MyApp.apk onto device 'emulator-5554'
Installing MyApp.apk...
Success!
Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
Collecting test information
Sending test information to Eclipse
Running tests...
Test run finished
Does anyone has an idea what is wrong? I have followed all steps in robotium tutorials religiously. I know there have been many questions around this but I have take care of all answers.
Also Robotium has similar problem with JUnit4 here.
Yes, I know what is wrong.
In order to run your tests, you need to begin them with test. So your code should be like this:
import android.test.ActivityInstrumentationTestCase2;
import com.package.sample.MyActivityClass;
import com.jayway.android.robotium.solo.Solo;
public class TestSample extends ActivityInstrumentationTestCase2<MyActivityClass> {
private Solo solo;
public TestSample() {
super("com.package.sample", MyActivityClass.class);
}
protected void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
//the test methods MUST begin with test...
public void testClickbutton1() throws Exception{
solo.clickOnButton("abc");
solo.setActivityOrientation(Solo.LANDSCAPE);
assertTrue(solo.searchText("load"));
}
#Override
protected void tearDown() throws Exception{
solo.finishOpenedActivities();
}
}
I had a similar issue awhile ago, and Robotium doesn't have the greatest documentation. I hope this helps you out :)
I did everything what you said, but I still got error with 0/0 test.I had found the solution, which is related to constructors:
// I DELETED THIS CONSTRUCTOR
public TestHomeScreenTest(String pkg, Class<ScreenSlidePagerActivity> activityClass) {
super(pkg, activityClass);
}
//GOOD SOLUTION:
public TestHomeScreenTest() {
super("com.dolphin.homescreen", ScreenSlidePagerActivity.class);
}
and it now WOOORKS ! :)
Related
Does anyone try to do black-box testing with Android Espresso?
Could anyone provides me with some simple example?
I had tried some example before, but failed every time!
Example, I had tried this one:
public class ApplicationTest extends ActivityInstrumentationTestCase2
{
private static final String ACTIVITY_CLASSNAME = "com.example.kai_yu.blackboxtest";
private static Class launchActivityClass;
static
{
try
{
launchActivityClass = Class.forName(ACTIVITY_CLASSNAME);
}
catch (ClassNotFoundException e)
{
throw new RuntimeException(e);
}
}
public ApplicationTest()
{
super(launchActivityClass);
}
#Test
public void testClick()
{
}
}
But Android Studio said:
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.kai_yu.blackboxtest"
com.example.kai_yu.blackboxtest is applicationId which is another installed application on my phone
Thank you!
Espresso can only run as part of an instrumentation test.
Instrumentation tests can only act upon the app under test ( i.e. the target of the instrumentation ).
UIAutomator might be better for your use case.
https://developer.android.com/tools/testing-support-library/index.html#UIAutomator
In Espresso docs you would find this line:
While it can be used for black-box testing, Espresso's full power is unlocked by those who are familiar with the code base under test."
For that reason Espresso testing is called by gray-box testing.
If you're not familiar with programming in Java or Android, or you want just to write black-box testing in the clearest way as possible try to learn instead of Espresso this framework
Calabash-iOS and Calabash-Android are the underlying low-level libraries that empower the Cucumber tool to run automated functional tests on Android...
Website: https://calaba.sh/
GitHub: https://github.com/calabash
Here would you find how and why to start using this framework:
http://blog.teddyhyde.com/2013/11/04/a-better-way-to-test-android-applications-using-calabash/
#RunWith(AndroidJUnit4.class)
#LargeTest
public class EspressoTest1 extends ActivityInstrumentationTestCase2<MainActivity>{
public EspressoTest1() {
super(MainActivity.class);
}
#Before
public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
}
#Test
public void test1ChatId() {
getActivity();
onView(withId(R.id.anuja)).check(matches(isDisplayed()));
}
#After public void tearDown() throws Exception {
super.tearDown();
}
}
There are two ways to write Espresso Test case one is as per shown above
The Examples are taken from this blog
http://qaautomated.blogspot.in/p/blog-page.html
Where you can find details of hot to run the espresso test case in detail.
I am using Android Studio to try and test my activity. Here is the basic code:
public class MyActivityTest extends ActivityUnitTestCase<MyActivity> {
public MyActivityTest() {
super(MyActivity.class);
}
#Override
protected void setUp() throws Exception {
super.setUp();
}
#SmallTest
public void testSomething() {
Assert.assertNotNull("something is null", null);
}
}
I would expect that this test case fails. Everything I try passes though. This seems like a strange question, but how can I make my test case fail? What am I doing wrong?
I managed to get this working, sort of. I found this on a bug report:
We are in the process of deprecating ActivityUnitTestCase. We recommend to move business logic to a separate class and unit test it with gradle unit test support (mockable android.jar).
So I extended ActivityInstrumentationTestCase2 instead and ran the test as an Instrumentation Test rather than a Unit Test. That worked. Here is basically what I have now:
public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity> {
public MyActivityTest() {
super(MyActivity.class);
}
public void testSomething() throws Exception {
//test goes here
Assert.assertEquals(message, expectedObject, actualObject);
}
}
I'm still not sure why I was seeing the behavior I was earlier, but at least I can test now. Here is a screenshot of my Test Build Configuration:
I'm writing many tests for my Android application, meaning that there are about 15 tests cases. how can I implement them all? I tried to make several .java files in the same project for each testcase, but it ran only the first one. Then I made one Test,java and wrote several method inside it. like public void test1() throws Exception{...} public void test2() throws Exception{...} . But it also ran just the first test case. In Run Configurations I chose Run all tests in the selected project, while running I could see them all under JUnit window on the left part of the screen, it successfully ran the first, showed that the next is in process but it did nothing((((
Remember to use solo.finishOpenedActivities() in your tearDown(). Then the execution will not hang.
if you are using robotium to perform black box testing this is how ur class should look like:
public class TestAPK extends ActivityInstrumentationTestCase2 {
private static final String TARGET_PACKAGE_ID="com.android.example";//your package name
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.android.example.MainActivity"; //your main activity full class name
private static Class launcherActivityClass;
static{
try{
launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
}catch(ClassNotFoundException e){
throw new RuntimeException(e);
}
}
public TestAPK() throws ClassNotFoundException{
super(TARGET_PACKAGE_ID,launcherActivityClass);
}
private Solo solo;
protected void setUp() throws Exception{
solo=new Solo(getInstrumentation(),getActivity());
}
public void test1() throws Exception{...}
public void test2() throws Exception{...}
}
When running a unit test (Run as Android JUnit test) it never completes.
Output:
[2011-03-03 21:45:43 - TestMyProj] Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
[2011-03-03 21:45:43 - TestMyProj] Collecting test information
[2011-03-03 21:45:47 - TestMyProj] Sending test information to Eclipse
[2011-03-03 21:45:47 - TestMyProj] Running tests...
...and nothing more.
Code:
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
public MainActivityTest() {
super("my.app.MainActivity", MainActivity.class);
}
private MainActivity mActivity;
#Override
protected void setUp() throws Exception {
super.setUp();
mActivity = this.getActivity();
}
public void testOneEqualsOne() {
assertEquals(1,1);
}
}
What could be wrong?
Kind regards,
Christian
The constructor you are using is deprecated. This may not be the root cause of the problem but it might help.
Use:
public MainActivityTest() {
super(MainActivity.class);
}
So, I'm new to android unit testing. I'm trying to write a unit test for the Phone application:
package com.android.phone;
import android.content.Intent;
import android.net.Uri;
import android.test.ApplicationTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import com.android.phone.PhoneApp;
import dalvik.annotation.TestTargetClass;
#TestTargetClass(PhoneApp.class)
public class TestPhone extends ApplicationTestCase<PhoneApp> {
public TestPhone() {
super(PhoneApp.class);
}
private PhoneApp phone;
#Override
protected void setUp() throws Exception {
super.setUp();
phone = getApplication();
}
#MediumTest
public void testDialerIsUp() {
assertNotNull("Phone app does not exist", phone);
// TODO add tests
}
}
Then I start an emulator, wait till it boots up, and run those tests:
adb shell am instrument -e class com.android.phone.TestPhone -r -w com.android.phone.tests/android.test.InstrumentationTestRunner
And now I'm getting a junit.framework.AssertionFailedError: PhoneApp does not exist. What is wrong here, why isn't PhoneApp up?
Actually, I'd recommend calling createApplication() in your setUp() method before calling phone = getApplication().
You don't show the code for your PhoneApp. Did you derive a PhoneApp class from the android.app.Application class? Or are you expecting that there is just something called PhoneApp out there that you can test?
You will need to write an android.app.Application class as part of your project, if you expect to test something.
Or, perhaps, you are talking about something that I do not understand. That is always possible.
How does this even compile with "PhoneApp.class" in it if you just stick to the SDK?
I know you can use Robotium to test existing apps though.
Elaborating on Karim's answer (it does work), this is the setup method:
MyApplication application;
#Override
protected void setUp() throws Exception {
super.setUp();
createApplication();
application = getApplication();
}