如何在Android JUnit测试中测试文本框值

发布于 2024-10-27 17:34:04 字数 1693 浏览 2 评论 0原文

我刚刚创建了登录屏幕项目。我为此编写了测试用例。我不知道如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百思不得你姐 2024-11-03 17:34:04

这是一个简单的问题,我在向 JUnit 提供输入时犯了一个小错误。我们必须提供,因为

       sendKeys("A D I T H I");

它现在可以工作。

Its a simple one and I have done a small mistake in giving input to JUnit.we must give as

       sendKeys("A D I T H I");

It works now.

南城旧梦 2024-11-03 17:34:04

笔记。我不是 Java 人员,但是..

您的测试失败了,因为它失败了,您的代码没有按预期运行。它说预期:但是是:<>

因此,用户界面中的字符串可能为 null。您将字符串 Karthika 放入 UI 中,然后您的测试期望这两个字符串都是 adithi,但它失败并指出它为 null。

  1. 用户界面中的某处文本变为空。
  2. 认为它应该是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 string Karthika in the UI, then your test expects both strings to be adithi, but it fails saying it was null.

  1. Somewhere the text in your UI goes null.
  2. I think it should be assertEquals(actualvar, whatyouexpect); like assertEquals(name, n1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文