Android注解和单元测试 [英] AndroidAnnotations and Unit Testing

查看:108
本文介绍了Android注解和单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在当前正在处理的示例项目中使用AndroidAnnotations(2.5)。



因为您可以使用例如注释类@EActivity @ViewById @Click 都会导致生成编译时间代码,我想知道如何为任何基于Android注释的应用程序创建单元测试/功能测试。



我很想听听对此事的看法。 / p>

干杯

解决方案

我回复了类似的帖子在这里



您可以使用几种选择。当然,您可以用一种更具单元测试风格的方式对代码的预生成进行测试。这应该隔离测试Java代码,最好不要涉及生成的代码。



您还可以在生成后测试代码。由AA生成的MyActivity_类可以在编译后直接实例化并进行相应的测试。我声称这正在朝着集成测试风格的方向发展。



虽然,我认为测试总比不测试好,我认为对于集成测试,您应该测试在类似于生产环境的硬件上。这使您可以全面了解应用程序在实际情况下的行为。因此,对于集成测试,我更喜欢高级的一切都正常工作测试。



Robolectric Robotium 在这两个努力。 Robolectric允许您在单元测试中实例化Activity,而Robotium允许您直接在设备上测试硒样式。



总而言之,我更喜欢对单元进行大量测试。没有生成的代码,然后进行一些轻度集成测试,以确保一切正常。


I am using AndroidAnnotations(2.5) in a sample project I am currently working on.

Since you can annotate your classes with for example @EActivity,@ViewById,@Click which will all lead to generated compile-time code, I was wondering how one would go about creating unit tests / functional tests for any Android Annotations powered application.

I would love to hear some opinions on this matter.

Cheers,

解决方案

I responded to a similar post here.

There are a couple of options available to you. You can, of course, test your code pre-generation in, what I claim, is a more unit testing style. This should test the Java code in isolation, preferably without generated code involved.

You can also test the code post-generation. The MyActivity_ classes generated by AA can be instantiated directly after compile time and test them accordingly. I claim this is edging towards an integration testing style.

While, I think it is always better to test than not to test, I think for integration tests, you should test on the hardware in a situation similar to production. This gives you a total picture of how your application will behave in a real world situation. Thus, for integration tests I prefer high level "is everything working well together" tests.

Robolectric and Robotium can help greatly in these two efforts. Robolectric allows you to instantiate you Activities in a unit test, while Robotium allows you to test selenium style directly on a device.

To recap, I prefer to heavily unit test my code without generation then do some light integration testing to make sure everything is working well together.

这篇关于Android注解和单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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