Robotium: Test run failed to complete. Expected N tests, received (N-1)

半腔热情 提交于 2019-12-22 10:06:21

问题


Android testing still remains my headache. I created the most simple app just to clear up how Robotium works, and each time tests fail with an error:

Running tests
Test running started
Test failed to run to completion. Reason: 'Test run failed to complete. Expected 1 tests, received 0'. Check device logcat for details
Test running failed: Test run failed to complete. Expected 1 tests, received 0

Once I had "Expected 3 tests, received 2" instead. The condition of using no-args constructor is met. How can this issue be solved?

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText editText = (EditText)findViewById(R.id.input);

        final TextView textView = (TextView)findViewById(R.id.output);
        Button button = (Button) findViewById(R.id.enter);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText(String.valueOf(editText.getText()));
            }
        });
    }
}

MainActivityTest.java

@SuppressWarnings("unchecked")
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

    private Solo solo;

    public MainActivityTest() {
        super(MainActivity.class);
    }

    public void testRun() throws Throwable {
        super.runTest();
        solo = new Solo(getInstrumentation(), getActivity());
        solo.waitForActivity(MainActivity.class);
        solo.assertCurrentActivity("error", MainActivity.class);
        solo.typeText((EditText) solo.getView(R.id.input), "alice");
        solo.clickOnView(solo.getView(R.id.enter));
        assertNotEquals("cooper", ((TextView) solo.getView(R.id.output)).getText());
        assertEquals("alice", ((TextView) solo.getView(R.id.output)).getText());
    }
}

This default test is green:

    public class ApplicationTest extends ApplicationTestCase<Application> {
    public ApplicationTest() {
        super(Application.class);
    }
}

回答1:


I suggest to remove the following lines from testRun() method

super.runTest();
solo = new Solo(getInstrumentation(), getActivity());

Instead add the following method

public void setUp() throws Exception {
        solo = new Solo(getInstrumentation(), getActivity());
    }

Also please add the tearDown method

@Override
public void tearDown() throws Exception {
    solo.finishOpenedActivities();
}


来源:https://stackoverflow.com/questions/31454131/robotium-test-run-failed-to-complete-expected-n-tests-received-n-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!