Android的:如何测试自定义视图? [英] Android: How to test a custom view?

查看:137
本文介绍了Android的:如何测试自定义视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有在Android的单元测试的几种方法,什么是最好的一个测试,我写了一个自定义视图?

There are several methods of unit testing in Android, what's the best one for testing a custom view I've written?

我目前正在测试它作为我的仪表测试案例活动的一部分,但我宁愿考刚认为,孤立的。

I'm currently testing it as part of my activity in an instrumentation test case, but I'd rather test just the view, isolated.

推荐答案

好了单元测试是通过它的源$ C ​​$ C独立单元进行测试,以确定它们是否适合使用的方法。因此,当你说你想测试你的自定义视图,你可以检查像的onTouchEvent,onDown,onFling,onLong preSS您的自定义视图的各种方法,onScroll,OnShow中preSS,onSingleTapUp,的OnDraw和各种其他依赖于您的业务逻辑。您可以提供模拟值,并对其进行测试。我建议测试您的自定义视图的两种方法。

Well unit testing is a method by which individual units of source code are tested to determine if they are fit for use. So when you say you want to test your custom view, you can check various methods of your custom views like "onTouchEvent", "onDown", "onFling", "onLongPress", "onScroll", "onShowPress", "onSingleTapUp", "onDraw" and various others depending on your business logic. You can provide mock values and test it. I would suggest two methods of testing your custom view.

1)猴子测试 猴子测试是通过自动化的测试工具进行随机测试。猴子测试是没有考虑到具体的测试运行单元测试。在这种情况下,猴子是任何输入的生产者。例如,一只猴子测试可以进入随机字符串到文本框,以确保处理所有可能的用户输入或提供垃圾文件,以检查装载例程在其数据迷信。这是一个黑箱测试技术,它可以检查你的自定义视图在这么多得天独厚的条件,你会得到吃惊:)。

1) Monkey Testing Monkey testing is random testing performed by automated testing tools. A monkey test is a unit test that runs with no specific test in mind. The monkey in this case is the producer of any input. For example, a monkey test can enter random strings into text boxes to ensure handling of all possible user input or provide garbage files to check for loading routines that have blind faith in their data. This is a black box testing technique and it can check your custom view in so many unique conditions that you will get astonished :) .

2)单元测试

2A)使用Robotium单元测试Framwork

转到Robotium.org或 HTTP://$c$c.google.com/p/robotium/ 并下载示例测试项目。 Robotium是一个非常易于使用的框架,使得Android应用程序测试方便,快捷。我创造了它,使先进的Andr​​oid应用程序可以以最小的努力测试。其结合使用ActivityInstrumentationTestCase2

Go to Robotium.org or http://code.google.com/p/robotium/ and download the example test project. Robotium is a really easy to use framework that makes testing of android applications easy and fast. I created it to make testing of advanced android applications possible with minimum effort. Its used in conjunction with ActivityInstrumentationTestCase2.

2B)采用Android测试框架

下面是引用链接: <一href="http://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2.html">http://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2.html 和 <一href="http://developer.android.com/reference/android/test/ActivityUnitTestCase.html">http://developer.android.com/reference/android/test/ActivityUnitTestCase.html

Here are the links to the reference: http://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2.html and http://developer.android.com/reference/android/test/ActivityUnitTestCase.html

对于初学者: <一href="http://developer.android.com/guide/topics/testing/testing_android.html">http://developer.android.com/guide/topics/testing/testing_android.html

据一位用户:除了容易测试非平台   相关的逻辑我还没有找到一个   聪明的方式来运行测试,到目前为止(在   至少对我来说)任何实际的平台   逻辑测试很麻烦。其   几乎不平凡反正因为我   在执行中的差异   仿真器和我的实际之间   设备,我讨厌运行单元测试   实现我的设备上刚   事后删除该应用程序。

According to one user : Aside from easily testing non platform dependent logic I haven't found a clever way to run tests, so far (at least for me) any actual platform logic testing is cumbersome. It's almost non trivial anyway because I've found differences in implementation between the emulator and my actual device and I hate to run a unit test implementation on my device just to remove the application afterwards.

我的策略是:尽量保持   简洁而做出的逻辑以及   想出来,然后测试   实施逐件(少   那么理想)。

My strategy has been: Try to be concise and make the logic well thought out and then test implementation piece by piece (less then desirable).

同时吴天海提供了良好的形式给出了真正的单元测试为Android项目的解决方案:<一href="https://sites.google.com/site/androiddevtesting/">https://sites.google.com/site/androiddevtesting/

一个用户做了一个截屏。

One user has made a screencast.

这里有一个截屏我做的我是怎么单元测试工作。简单的单元   测试和更复杂的单元测试   依靠具有参考   情境或活动的对象。   <一href="http://www.gubatron.com/blog/2010/05/02/how-to-do-unit-testing-on-android-with-eclipse/">http://www.gubatron.com/blog/2010/05/02/how-to-do-unit-testing-on-android-with-eclipse/

Here's a ScreenCast I made on how I got Unit Tests to work. Simple Unit Tests and more complex unit tests that depend on having a reference to Context or Activity objects. http://www.gubatron.com/blog/2010/05/02/how-to-do-unit-testing-on-android-with-eclipse/

希望它可以帮助你测试你的自定义视图中的所有可能的条件:)

Hope it helps you testing your custom view in all possible conditions :)

评论(futlib)所有你的建议似乎涉及测试活动,而我确实想测试刚才的看法。我可能想用这个观点在其他活动,所以也没有太大的意义对我来说,有一个特定的一个测试。 - futlib

答:要实现自定义视图,   你通常会通过提供开始   覆盖的一些标准   该框架调用的方法   所有意见。例如的OnDraw   的onkeydown(INT,KeyEvent的),   的onkeyup(INT,KeyEvent的),   onTrackballEvent(MotionEvent)等   您的自定义视图。所以,当你想   做单元测试为您定制您会   必须测试这些方法,和   提供模拟值,这样你   可以测试所有的自定义视图   可能的情况。这些测试方法   并不意味着你正在测试   活动中,但它意味着测试你的   自定义视图(方法/功能),它   是一个活动范围内。同时你会   必须把你的自定义视图中   活动最终为你的目标   用户体验。一旦   彻底的测试,您的自定义视图   可以放置在许多项目和   许多活动。

Answer: To implement a custom view, you will usually begin by providing overrides for some of the standard methods that the framework calls on all views. For example "onDraw", "onKeyDown(int, KeyEvent)", "onKeyUp(int, KeyEvent)", "onTrackballEvent(MotionEvent)" etc of your custom view. So when you want to do unit testing for your custom you'll have to test these methods, and provide mock values to it so that you can test your custom view on all possible cases. Testing these methods doesn't mean that you are testing your ACTIVITY, but it means testing your custom view (methods/functions) which is within an activity. Also you'll have to put your custom view in an Activity eventually for your target users to experience it. Once thoroughly tested , your custom view can be placed in many projects and many activities.

这篇关于Android的:如何测试自定义视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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