如何在Android JUnit测试中测试文本框值
我刚刚创建了登录屏幕项目。我为此编写了测试用例。我不知道如何在 JUnit 测试用例的文本框中输入文本。我就是这样做的,
public void testlogin() {
final String n1 = "adithi";
final String p1 = "adithi";
String name, pass;
editUname.clearComposingText();
editPswd.clearComposingText();
TouchUtils.tapView(this, editUname);
sendKeys("adithi");
TouchUtils.tapView(this, editPswd);
sendKeys("adithi");
activity.runOnUiThread(new Runnable() {
public void run() {
signinbtn.performClick();
}
});
name = editUname.getText().toString();
pass = editPswd.getText().toString();
Log.e("name",name);
Log.e("Password",pass);
assertEquals(n1, name);
assertEquals(p1, pass);
}
测试用例结果是
junit.framework.ComparisonFailure: expected:<adithi> but was:<>
at com.firstpageTest.Test.testlogin(Test.java:126)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
atandroid.test.ActivityInstrumentationTestCase2.runTest
(ActivityInstrumentationTestCase2.ja
atandroid.test.ActivityInstrumentationTestCase2.runTest
(ActivityInstrumentationTestCase2.java:186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
请指导我。
i have just created login screen project.I wrote the testcase for that.I dont know how to enter the text in textbox in JUnit test case. i just did like this
public void testlogin() {
final String n1 = "adithi";
final String p1 = "adithi";
String name, pass;
editUname.clearComposingText();
editPswd.clearComposingText();
TouchUtils.tapView(this, editUname);
sendKeys("adithi");
TouchUtils.tapView(this, editPswd);
sendKeys("adithi");
activity.runOnUiThread(new Runnable() {
public void run() {
signinbtn.performClick();
}
});
name = editUname.getText().toString();
pass = editPswd.getText().toString();
Log.e("name",name);
Log.e("Password",pass);
assertEquals(n1, name);
assertEquals(p1, pass);
}
the testcase result is
junit.framework.ComparisonFailure: expected:<adithi> but was:<>
at com.firstpageTest.Test.testlogin(Test.java:126)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
atandroid.test.ActivityInstrumentationTestCase2.runTest
(ActivityInstrumentationTestCase2.ja
atandroid.test.ActivityInstrumentationTestCase2.runTest
(ActivityInstrumentationTestCase2.java:186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
please guide me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个简单的问题,我在向 JUnit 提供输入时犯了一个小错误。我们必须提供,因为
它现在可以工作。
Its a simple one and I have done a small mistake in giving input to JUnit.we must give as
It works now.
笔记。我不是 Java 人员,但是..
您的测试失败了,因为它失败了,您的代码没有按您预期运行。它说
预期:但是是:<>
因此,用户界面中的字符串可能为
null
。您将字符串Karthika
放入 UI 中,然后您的测试期望这两个字符串都是adithi
,但它失败并指出它为 null。assertEquals(actualvar, whatyouexpect);
就像assertEquals(name, n1);
Note. I'm not a Java person but..
Your test is failing because well its failing, your code is not running as you expected.It says
expected:<Karthika> but was:<>
So the string from your UI is probably
null
. You're putting the stringKarthika
in the UI, then your test expects both strings to beadithi
, but it fails saying it was null.assertEquals(actualvar, whatyouexpect);
likeassertEquals(name, n1);