Robotium: Activity not launched for second test - android

I have been able to run an android junit test with one test method successfully, but when more than one test method are involved, it just runs the first test and after tearDown, the activity does not relaunch for the subsequent tests. As a result, all my test methods fail, save the first one.
On debugging, I noticed that setUp method launches the MainActivity successfully before running the first testMethod, but on being revisited before the start of second testMethod, the same activity does not get relaunched. The code is as below:
package PACKAGE.test;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
public class Login extends ActivityInstrumentationTestCase2 {
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "*.*.MainActivity";
private static Class<?> launcherActivityClass;
static {
try {
launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
}
catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
#SuppressWarnings("unchecked")
public Login() throws ClassNotFoundException {
super(launcherActivityClass);
}
private Solo solo;
public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
#Test
public void testLoginScreen() {
solo.enterText(0, "user-name");
solo.enterText(1, "pwd");
solo.clickOnButton("Login");
solo.waitForActivity("*.*.*.nextActivity");
solo.clickOnRadioButton(2);
}
#Test
public void testSearch(){
solo.enterText(0, "user-name");
solo.enterText(1, "pwd");
solo.clickOnButton("Login");
solo.waitForActivity("*.*.*.nextActivity");
solo.clickOnRadioButton(1);
}
public void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}
}

You may need to #Override your teardown method
#Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}

Related

It seems that setup() is not called when the test is running

I am developing a test class for my application. The test class code looks like this:
public class ProfileActivityTest extends ActivityInstrumentationTestCase2<ProfileActivity> {
ProfileActivity profileActivity;
#SuppressLint("NewApi")
public ProfileActivityTest(Class<ProfileActivity> activityClass) {
super(activityClass);
// TODO Auto-generated constructor stub
}
#SuppressLint("NewApi")
public ProfileActivityTest() {
super(ProfileActivity.class);
}
protected void setup() throws Exception {
super.setUp();
profileActivity = getActivity();
}
public void test_profileActivityLoggingIn() {
assertNotNull(profileActivity);
assertEquals(View.GONE, profileActivity.findViewById(R.id.btnAddOrEdit).getVisibility());
}
protected void tearDown() throws Exception {
super.tearDown();
}
}
The problem is that I get assertionFailure on assertNotNull(profileActivity) though profileActivity is instantiated in setup(). I don't understand what I am doing wrong;
As #njzk2 has mentioned, "setup" should be changed to "setUp".

run setUp() and tearDown() methods for each test suite InstrumentationTestCase Android

I'm implementing a Test automation tool and I have a class which extends InstrumentationTestCase. For example:
public class BaseTests extends InstrumentationTestCase {
#Override
protected void setUp() throws Exception {
super.setUp();
Log.d(TAG, "setUp()");
}
#Override
protected void tearDown() throws Exception {
super.tearDown();
Log.d(TAG, "tearDown()");
}
public void test_one() {
Log.d(TAG, "test_one()");
}
public void test_two() {
Log.d(TAG, "test_two()");
}
}
When I run the tests of BaseTests, the setUp() method is called 2 times. One time before executing test_one() and another after test_two(). The same happens with the tearDown(), it is called after executing each of both two methods.
What I would like to do here is to call setUp() and tearDown() methods only one time for the execution of all BaseTests tests. So the order of the method call would be like:
1) setUp()
2) test_one()
3) test_two()
4) tearDown()
Is there a way to do such thing?
I resolve this problem by using:
#BeforeClass
public static void setUpBeforeClass() throws Exception {
}
and:
#AfterClass
public static void tearDownAfterClass() throws Exception {
}
instead of setUp() and tearDown().
So in your case it would be:
import org.junit.AfterClass;
import org.junit.BeforeClass;
public class BaseTests extends InstrumentationTestCase {
#BeforeClass
protected static void setUp() throws Exception {
//do your setUp
Log.d(TAG, "setUp()");
}
#AfterClass
protected static void tearDown() throws Exception {
//do your tearDown
Log.d(TAG, "tearDown()");
}
public void test_one() {
Log.d(TAG, "test_one()");
}
public void test_two() {
Log.d(TAG, "test_two()");
}
}
The annotations #BeforeClass and #AfterClass assure that it will run only one time before and after the test runs respectively
I ended up following the idea of #beforeClass and #afterClass.
However I couldn't use the annotations itself. Instead, I implemented them (by using counters) on a base class and my test suites inherits from this base class.
Here's the link I based myself to do so:
https://android.googlesource.com/platform/frameworks/base/+/9db3d07b9620b4269ab33f78604a36327e536ce1/test-runner/android/test/PerformanceTestBase.java
I hope this could help someone else!

Android Testing - Issue with ActivityInstrumentationTestCase2?

I am running UIAutomation for android using Robotium and ActivityInstrumentationTestCase2. I have a test suite with 5 tests.
Sometimes my test randomly crash because a test starts, once the previous test has not ended yet.
Is there a way to avoid this? is it possible to manually add a 10 second delay before every test to get away from this horrible annoying bug?
EDIT:
public class MyTest<T extends RoboActivity> extends ActivityInstrumentationTestCase2<T>
{
protected Solo solo;
#Override
protected void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
#Override
protected void tearDown() throws Exception {
solo.finishOpenedActivities();
try {
solo.finalize();
}
catch (Throwable e) {
Assert.fail(e.getMessage()+ e.toString());
e.printStackTrace();
}
super.tearDown();
}
}
Maybe this could work :
mSolo = new Solo(getInstrumentation(), getActivity());
mSolo.waitForActivity(AccountDetail.class);
Seems that waitFor* methods are managing that better than a "sleep"
http://robotium.googlecode.com/svn/doc/com/robotium/solo/Solo.html#waitForActivity(java.lang.Class, int)
My tests's construction, teardown, etc. are slightly different and works well (see below). I had to add a sleep to work around some non-deterministic test-failures.
public class AccountDetailTest extends ActivityInstrumentationTestCase2<AccountDetail> {
private Solo solo;
public AccountDetailTest() {
super("com.bigcorp.breadmaker", AccountDetail.class);
}
#Override
public void setUp() {
solo = new Solo(getInstrumentation(), getActivity());
solo.sleep(500); //wait for activity to initialize
}
#SmallTest
public void testDummy() {
assertNotNull(solo);
}
#Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
}
}

Android Robotium - How to manage the execution order of testcases?

I am trying to use Robotium to automate the testing of an application.
The test cases were documented and they are supposed to be test in specific order. But it seems that
Junit run the tests in alphabetic order.. how do I rearrange the order of execution? Here is the basic structure of my test class:
public class ETTerminalTest extends ActivityInstrumentationTestCase2<IdleActivity> {
private Solo solo;
private static final Logger LOGGER = LoggerFactory.getLogger(ETTerminalTest.class);
public ETTerminalTest() {
super("com.employtouch.etterminal.ui.activity", IdleActivity.class);
}
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
#Smoke
public void testEnterPin() throws Exception {
...
}
#Smoke
public void testWhatEver() throws Exception {
...
}
#Smoke
public void testSomethingElse() throws Exception {
...
}
#Override
public void tearDown() throws Exception {
try {
//Robotium will finish all the activities that have been opened
solo.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}
}
I am not sure for Robotium, but the test order for normal jUnit test cases can be managed by creating a test suite. I guess it should be same in this case as well.(I haven't tried it myself). Some info here.

how to organize class in a robotium project?

I have an activity A that launch an activity B.
I'd like to have a robotium project to test my app so I'v created a first test class for activity A and all goes well.
I'd like now to create another test class for testing Activity B but it require some init from activity A.
I tried this:
BTestClass extends ActivityInstrumentationTestCase2 {
private Solo solo;
private ATestClass testA;
#Override
protected void setUp() throws Exception {
Log.i(TAG, "setUp");
solo = new Solo(getInstrumentation(), getActivity());
testA = new ATestClass();
testA.setUp();
testA.testAddAccount();
solo.clickInList(0);
}
[… more test method]
}
I got a NullPointerException when testA is doing getActivity()
I do it this way:
public class BTest extends ActivityInstrumentationTestCase2<SplashScreenActivity> {
protected static final String TARGET_PACKAGE_ID = "app.under.test";
protected Solo solo;
public BTest() {
super(TARGET_PACKAGE_ID, StartingActivity.class);
}
#Override
public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
// setup stuff
}
#Override
public void tearDown() throws Exception {
// teardown stuff
super.tearDown();
}
}
All testcases just inherit from BTest then
public class OneTest extends BTest {
public void test_OneTest() {
//test stuff
solo.clickOnButton("Ok");
}
}

Categories

Resources