问题
I'm testing my app's Login with Robotium and I'm having some problems. I've got two different user types so I've made 3 tests. One for each type and another failed login. If I execute them individually they all success, but if I run all the test case it runs one, executes the tearDown(solo.finishOpenedActivities) and it doesn't restart the activity to execute the other tests. So in the second test when I'm asking for a EditText it says that is not available.
Here is my code:
public class TestLogin extends ActivityInstrumentationTestCase2<MainActivity> {
private Solo solo;
public TestLogin() {
super("com.truekke4.test", MainActivity.class);
}
@Override
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
super.setUp();
}
@Override
public void tearDown() throws Exception {
getActivity().logout();
solo.finishOpenedActivities();
super.tearDown();
}
public void testUsuarioDesconocido() {
solo.clearEditText(0);
solo.enterText(0, "usuario desconocido");
solo.assertCurrentActivity("Error", MainActivity.class);
solo.clickOnButton("OK");
solo.clickOnButton("OK");
solo.assertCurrentActivity("Error", MainActivity.class);
}
public void testUsuario() {
solo.clearEditText(0);
solo.enterText(0, "usuario");
solo.clickOnButton("OK");
solo.assertCurrentActivity("Error", InicioUsuarioActivity.class);
}
public void testEmpresa() {
solo.clearEditText(0);
solo.enterText(0, "empresa");
solo.clickOnButton("OK");
solo.assertCurrentActivity("Error", InicioPymeActivity.class);
}
}
I've got to restart the activity manually? Create and Intent and startActivity(intent)?
I don't have to finish opened activities? Or I have to finish activities but restart them/it in setUp(). How Can I restart or create activities to make recognizeables for Robotium?
Help!
回答1:
I believe your issue is being caused because you're overriding the setUp()
method from ActivityInstrumentationTestCase2 rather than using the Robotium method. In your test classes, Robotium looks for a method with the signature public void setUp()
to configure its tests, which can get confusing, because it has the same name as the method that can be overridden. I'm not sure of exactly when or how often the protected setUp is called, but I know that the public one is the best place to initialize solo as Robotium is guaranteed to call it before each individual test. Try changing:
@Override
protected void setUp() throws Exception
to:
public void setUp() throws Exception
with the same method body (but without the @Override annotation) and see if that allows you to run all your tests.
回答2:
where you have the code:
@Override
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
super.setUp();
}
make it:
@Override
protected void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
if this does not work for some reason your class isn't getting scrubbed properly in the teardown, you can either call setActivity(null) to make getActicity() launch the activity again or manually call launchActivity yourself.
来源:https://stackoverflow.com/questions/14608353/activity-doesnt-restart-in-different-tests-with-robotium