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

查看:36
本文介绍了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.

推荐答案

Well 单元测试是一种测试单个源代码单元以确定它们是否适合使用的方法.因此,当您说要测试自定义视图时,可以检查自定义视图的各种方法,例如onTouchEvent"、onDown"、onFling"、onLongPress"、onScroll"、onShowPress"、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) 猴子测试Monkey 测试是由自动化测试工具执行的随机测试.猴子测试是一种单元测试,它在运行时没有考虑特定的测试.在这种情况下,猴子是任何输入的生产者.例如,猴子测试可以在文本框中输入随机字符串以确保处理所有可能的用户输入或提供垃圾文件以检查对其数据有盲目信心的加载例程.这是一种黑盒测试技术,它可以在许多独特的条件下检查您的自定义视图,您会感到惊讶:).

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 单元测试框架

转到 Robotium.org 或 http://code.google.com/p/robotium/ 并下载示例测试项目.Robotium 是一个非常易于使用的框架,它使安卓应用程序的测试变得容易和快速.我创建它是为了以最少的努力测试高级 android 应用程序.它与 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 测试框架

以下是参考链接:http://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2.htmlhttp://developer.android.com/reference/android/test/ActivityUnitTestCase.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).

Stephen Ng 还为 Android 项目解决方案的真正单元测试提供了很好的方法:https://sites.google.com/site/androiddevtesting/

一位用户进行了截屏.

这是我制作的关于如何让单元测试工作的 ScreenCast.简单单元测试和更复杂的单元测试取决于有参考上下文或活动对象.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) 您的所有建议似乎都涉及测试 ACTIVITY,而我真的只想测试 VIEW.我可能想在其他活动中使用此视图,因此用特定的活动对其进行测试对我来说没有多大意义.– 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天全站免登陆