单元测试:在哪里可以跟踪从TextView的输出结果 [英] Unit testing: where to track output results from TextView

查看:160
本文介绍了单元测试:在哪里可以跟踪从TextView的输出结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有3个单元本次测试活动:TextView的,EditText上,按键。当用户点击按钮,然后活动转变,从文字到的EditText TextView的中一些文本。

问题是:我如何写这样的活动单元测试?

我的问题:我应该在一个线程中的一个按钮点击(.performClick),但在另一个异步等待,但因为它运行的每个测试开始以test$ P $打破单元测试的逻辑PFIX和标记测试为确定如果没有不成功的断言。

code单元测试的:

 公共类ProjectToTestActivityTest扩展ActivityInstrumentationTestCase2< ProjectToTestActivity> {    私人TextView的resultView;
    私人的EditText editInput;
    私人按钮sortButton;    公共ProjectToTestActivityTest(字符串PKG,类activityClass){
        超(com.projet.to.test,ProjectToTestActivity.class);
    }公共无效onTextChanged(字符串str)
{
    Assert.assertTrue(str.equalsIgnoreCase(1234567890));
}
       @覆盖
       保护无效设置()抛出异常{
           super.setUp();           活动活动= getActivity();
           resultView =(TextView中)activity.findViewById(R.id.result);
           editInput =(EditText上)activity.findViewById(R.id.editInput);
           sortButton =(按钮)activity.findViewById(R.id.sortButton);       resultView.addTextChangedListener(新TextWatcher(){        公共无效afterTextChanged(编辑为arg0){
            onTextChanged(arg0.toString());
        }
           }
       }       保护无效testSequenceInputAndSorting()
       {
           editInput.setText(1234567890);
           sortButton.performClick();
       }
}


解决方案

假设业务逻辑下的应用项目中的活动正确实施,在另一个字,点击按钮时,来自的EditText文字复制到TextView的。

如何写这样的活动单元测试?

 公共无效testButtonClick(){  // TextView中应该是空的开始。
  的assertEquals(文本应该是空的,,resultView.getText());  //模拟点击一个按钮,它复制文本从的EditText TextView的到。
  activity.runOnUiThread(新的Runnable(){
    公共无效的run(){
      sortButton.performClick();
    }
  });  //等待几秒钟,这样就可以看到模拟器/设备上的变化。
  尝试{
    视频下载(3000);
  }赶上(InterruptedException的E){
    e.printStackTrace();
  }  // TextView的应该是富,而不是空了。
  的assertEquals(文字应该是富,富,resultView.getText());
}

更新:

如果您没有在主应用程序code使用的线程,存在的主要应用只有UI线程,所有的UI事件(单击按钮,更新的TextView等)在UI线程连续加工,它是不太可能,这种连续的UI事件会卡住/延迟超过几秒钟。如果你还没有十分的把握,使用 waitForIdleSync(),使测试应用程序等待直到没有更多的UI事件对主应用程序的UI线程处理:

  getInstrumentation()waitForIdleSync();
的assertEquals(文字应该是富,富,resultView.getText());

然而, getInstrumentation()waitForIdleSync(); 不会等待线程在你的主应用程序code催生,例如,当点击按钮后,启动AsyncTask的过程耗时的工作,完成后(在3秒内说),它会更新TextView的,在这种情况下,你必须使用视频下载(); 来让你的测试应用程序停止和等待,看看答案<一个href=\"http://stackoverflow.com/questions/10491526/how-can-i-test-the-result-of-a-button-click-that-changes-the-activitys-view-asy/10491965#10491965\">in此链接为code的例子。

So i have this test Activity with 3 elements: TextView, EditText, Button. When user clicks button, Activity then transforms text from EditText to some text in TextView.

Question is: how do i write unit test for such activity?

My problem: i should "click" (.performClick) on a button in one thread, but to wait asynchronously in another but that breaks a logic of a unit test since it runs every test starting with "test" prefix and marks test as "Ok" if there were no unsuccessful assertions.

Code of a unit test:

public class ProjectToTestActivityTest extends ActivityInstrumentationTestCase2<ProjectToTestActivity> {

    private TextView resultView;
    private EditText editInput;
    private Button   sortButton;

    public ProjectToTestActivityTest(String pkg, Class activityClass) {
        super("com.projet.to.test", ProjectToTestActivity.class);
    }

public void onTextChanged(String str)
{
    Assert.assertTrue(str.equalsIgnoreCase("1234567890"));
}


       @Override  
       protected void setUp() throws Exception {  
           super.setUp();  

           Activity activity = getActivity();  
           resultView = (TextView) activity.findViewById(R.id.result);
           editInput = (EditText) activity.findViewById(R.id.editInput);
           sortButton = (Button) activity.findViewById(R.id.sortButton);

       resultView.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable arg0) {
            onTextChanged(arg0.toString());
        }
           }
       }  

       protected void testSequenceInputAndSorting()
       {
           editInput.setText("1234567890");
           sortButton.performClick();   
       }
}

解决方案

Suppose the business logic is properly implemented in your Activity under application project, in another word, when button is clicked, copy text from EditText to TextView.

how do i write unit test for such activity?

public void testButtonClick() {

  // TextView is supposed to be empty initially.
  assertEquals("text should be empty", "", resultView.getText());

  // simulate a button click, which copy text from EditText to TextView.
  activity.runOnUiThread(new Runnable() {
    public void run() {
      sortButton.performClick();
    }
  });

  // wait some seconds so that you can see the change on emulator/device.
  try {
    Thread.sleep(3000);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }

  // TextView is supposed to be "foo" rather than empty now.
  assertEquals("text should be foo", "foo", resultView.getText());
}

Update:

If you don't use thread in your main application code, there is only UI thread in main application, all UI events (button clicked, textView updated and etc.) are processed continuously in UI thread, it is very unlikely that this continuous UI events will stuck/delay more than several seconds. If you are still no very sure, use waitForIdleSync() to make test application wait until no more UI events to process on main application's UI thread:

getInstrumentation().waitForIdleSync();
assertEquals("text should be foo", "foo", resultView.getText());

However, getInstrumentation().waitForIdleSync(); will not wait for the thread spawned in your main application code, for instance, when click button, it starts AsyncTask process time-consuming job and after finish (say in 3 seconds), it updates the TextView, in this case, you have to use Thread.sleep(); to make you test application stop and wait, check out answer in this link for code example.

这篇关于单元测试:在哪里可以跟踪从TextView的输出结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆