gpt4 book ai didi

Robotium:测试运行未能完成。预期 N 次测试,已收到 (N-1)

转载 作者:行者123 更新时间:2023-12-03 01:26:55 24 4
gpt4 key购买 nike

Android 测试仍然是我头疼的问题。我创建了最简单的应用程序只是为了弄清楚 Robotium 的工作原理,每次测试失败时都会出现错误:

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

有一次我“预期进行 3 次测试,但收到了 2 次”。满足使用无参数构造函数的条件。如何解决这个问题?

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());
}
}

此默认测试是绿色的:

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

最佳答案

我建议从 testRun() 方法中删除以下几行

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

改为添加以下方法

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

另外请添加tearDown方法

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

关于Robotium:测试运行未能完成。预期 N 次测试,已收到 (N-1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31454131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com