gpt4 book ai didi

android - EditText View 的单元测试。我该怎么做?

转载 作者:太空宇宙 更新时间:2023-11-03 12:11:04 24 4
gpt4 key购买 nike

public class LoginActivityTest extends ActivityInstrumentationTestCase2<Login> {


Login mActivity;
private EditText username;
private EditText password;

@SuppressWarnings("deprecation")
public LoginActivityTest()
{
super("com.main.account.Login",Login.class);
}


@Override
protected void setUp() throws Exception {
super.setUp();
mActivity = this.getActivity();

username = (EditText) mActivity.findViewById(R.id.username);
password = (EditText) mActivity.findViewById(R.id.password);
}

public void testPreconditions() {
assertNotNull(username);
assertNotNull(password);
}


public void testText() {
assertEquals("hello",username.getText());
assertEquals("hello123", password.getText());
}

}

我得到的错误是:

04-18 16:03:47.132: I/TestRunner(12376): junit.framework.AssertionFailedError: expected:<hello> but was:<>
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.Assert.fail(Assert.java:47)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.Assert.failNotEquals(Assert.java:282)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.Assert.assertEquals(Assert.java:64)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.Assert.assertEquals(Assert.java:71)
04-18 16:03:47.132: I/TestRunner(12376): at com.crumbs.main.test.LoginActivityTest.testText(LoginActivityTest.java:46)
04-18 16:03:47.132: I/TestRunner(12376): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 16:03:47.132: I/TestRunner(12376): at java.lang.reflect.Method.invoke(Method.java:507)
04-18 16:03:47.132: I/TestRunner(12376): at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
04-18 16:03:47.132: I/TestRunner(12376): at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
04-18 16:03:47.132: I/TestRunner(12376): at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.TestCase.runBare(TestCase.java:127)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.TestResult$1.protect(TestResult.java:106)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.TestResult.runProtected(TestResult.java:124)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.TestResult.run(TestResult.java:109)
04-18 16:03:47.132: I/TestRunner(12376): at junit.framework.TestCase.run(TestCase.java:118)
04-18 16:03:47.132: I/TestRunner(12376): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
04-18 16:03:47.132: I/TestRunner(12376): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
04-18 16:03:47.132: I/TestRunner(12376): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
04-18 16:03:47.132: I/TestRunner(12376): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

那我该如何测试呢?如题,如何设置 EditText 的值以便我可以在此处对其进行测试?

文档有点分散。

最佳答案

How do I test it then? As in, how do I set the values for the EditText so that I can test it here?

虚拟测试 EditText.setText() 方法没有多大意义,但是,如果您坚持,请执行以下操作:

public void testText() {
// simulate user action to input some value into EditText:
final EditText mUsername = (EditText) mActivity.findViewById(R.id.username);
final EditText mPassword = (EditText) mActivity.findViewById(R.id.password);
mActivity.runOnUiThread(new Runnable() {
public void run() {
mUsername.setText("hello");
mPassword.setText("hello123");
}
});

// Check if the EditText is properly set:
assertEquals("hello", mUsername.getText());
assertEquals("hello123", mPassword.getText());
}

希望这对您有所帮助。

关于android - EditText View 的单元测试。我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10207759/

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