转Android工程 单元测试(一)

时间 : 14-09-10 栏目 : Android, 移动开发, 集成测试 作者 : meiw@z3a105.com 评论 : 0 点击 : 306 次

一、创建测试工程

  1. New > Project > Android > Android Test Project.

  1. 添加测试用例类

    添加新类,基类设置为android.test.ActivityInstrumentationTestCase2<HelloAndroid>

  2. 添加构造函数

    添加setUp()方法,这个方法在所有的测试之前进行变量和测试环境的初始化。

    @Override

        protected void setUp() throws Exception {

            super.setUp();

            mActivity = this.getActivity();

            mView = (TextView) mActivity.findViewById(com.example.helloandroid.R.id.textview);

            resourceString = mActivity.getString(com.example.helloandroid.R.string.hello);

        }

  3. 添加testPreconditions()方法,检查初始化环境,只执行一次

    public void testPreconditions() {

          assertNotNull(mView);

        }

  4. 添加单元测试

    public void testText() {

          assertEquals(resourceString,(String)mView.getText());

        }

  5. 测试 Run As... > Android JUnit Test

二、测试类的选择

   1、测Activity中的方法

        写一个测试类,让它继承自ActivityInstrumentationTestCase2<AHttpServerActivity>。

        首先,重写构造函数

1 public ActivityTest2() {
2           super("com.test.testA",AActivity.class);
3     }

      其中super()中两个参数,前者为待测Activity的包名,后者为待测Activity的.class后缀名称。

      其次,实现setup(),在setup中调用getActivity,能启动该Activity,方便后面调Activity的方法。

1 public void setUp() throws Exception {
2         super.setUp();
3         a = getActivity();        
4     }

      再次,写测试方法,测Activity的方法。

1      @MediumTest
2      public void testHandler()  {                
3            AActivity.sendMsgToActivity("http error", false);
4      }

2、测试一般类的方法

写一个测试类,让它继承自AndroidTestCase。

重写setUp() ,在里面进行类环境的初始化 。
重写tearDown(),在里面进行环境的销毁。

写下面的函数,测相应的类的方法:
public void testXxxx() throws Exception(){
...
}
**这里抛出的异常会被测试框架捕获

注:在测试方法里记得让Thread.sleep(),这样有一些不是同步的方法得以执行。

三、覆盖率统计工具

1、下载工具apache-ant-1.8.4,并将它的解压缩后的路径添加到path中去。

2、编写批处理(.bat文件)文件,清除上一次统计的文件

cd D:\test\test\A

del local.properties build.xml
rmdir /q /s libs

cd D:\test\test\ATest
del local.properties build.xml  ant.properties
rmdir /q /s coverage

其中D:\test\test\A为被测工程的目录,D:\test\test\ATest为测试工程的目录。

3、执行下列命令

android update project -p D:\test\test\A

android update test-project -m D:\test\test\A -p D:\test\test\ATest

ant clean emma debug install test

即可在测试工程目录下生成converage文件夹,其中就得到了统计覆盖率的数据。

本文标签 ,

除非注明,文章均为( meiw@z3a105.com )原创,转载请保留链接: http://blog.z3a105.com/?p=123

转Android工程 单元测试(一):等您坐沙发呢!

发表评论





       ==QQ:122320466==

 微信    QQ群

推广

  • 104726drf320ff09r7i62f.jpg
  • 103217vn3333zbk3snwz2u.jpg

0

Aujourd’hui, une partie avec le développement du e-commerce, achats en ligne est devenu une partie de la vie pour beaucoup de gens. La mariage a commencé achats en ligne. Si vous choisissez achats les mariages en ligne, il peut être beaucoup moins cher que le salon de la Robe de mariée pas chermariée local, réduisant le budget de mariage. vous pouvez avoir beaucoup de choix si acheter de mariage en ligne. vous pouvez ramasser une robe de mariée bon marché sur Internet.
Piercing fascinerande figur, och nu tittar vi på 2016 senast brudklänning, kan du vara den vackraste bruden det!2016 senaste Bra brudklänning, söt temperament Bra design, romantiska spetsar blomma kjol, som du lägger till en elegant och charmig temperament.Kvinnan tillbaka mjuka linjer, människor brudklänningofta få en känsla av oändlig frestelse. Fall 2016 mässan, lämnar uppgifter om ditt bröllop charmig.
Yesterday afternoon, the Chinese team was training in the Guangzhou Gymnasium, when the reporter asked Zhao Yunlei the feeling of wearing the new cheap jersey , cheap jerseys online shopshe readily took a shirt from the bag crumpled ball to reporters, and she said with a smile: ” This shirt is light. ”Zhao Yunlei said: “Our material is very light like with the clothes of the tennis King Nadal, Federer, after the sweat, sweat does not drip down to the ground, when we do move, it is easy pace slipping if the sweat drip on the floor.”Tennis players Zhang Yawen, told reporters: “You might think the clothes attached to the body, fearing we swing will be affected, in fact, we do not feel anything, because the clothes are very light, very soft, put on quite comfortable. And it’s particularly good clothes to dry, washing and will dry in 15 minutes. ”
China’s sports enthusiasts NFL sweatshirt with mad love and the pursuit of, and therefore, NFL jerseys have a good market in China and development. China is a populous country, is the consumer, the economic momentum is so good, the sales prospects sportswear is immeasurable. With hot sales sweatshirt, but also to promote the importance of sports fans, on health, on the other hand is a matter of concern for the World Cup, fans wearing NFL jerseys and also can express themselves more fully love and obsession Therefore, NFL jerseys Wholesale jerseys online shopwholesale has good prospects and development in China.
ANTA-ANTA Sports Products Limited, referred to as ANTA Sports, Anta, is China’s leading sporting goods companies, mainly engaged in the design, development, manufacture and marketing of ANTA brand sporting goods, including sports footwear, apparel and accessories. Anta sweatshirt design advantages, warm stretch knit fabric, using Slim version of model, more personal fit, bid farewell to bloated, so wearing more stylish.GUIRENNIAO-This logo is a spiritual totem, smooth graphics implication unstoppable force; flexible deliver an elegant arc Wholesale jerseys china shop movement, strength and speed of the United States, a symbol of passion and rationality publicity “Heart” and “meaning”, “concept” unity; pass the fearless and enterprising mind, showing beyond the realm of self, to unstoppable force to create the future.XTEP-Xtep (China) Co., Ltd. is a comprehensive development wholesale jerseys china shop, production and marketing of Xtep brand (XTEP) sports shoes, clothing, bags, caps, balls, socks mainly large sporting goods industry enterprises.
There are a lot of fans in identifying the authenticity of the above cheap jerseys have great distress, so here to i will show you some methods to definitely affordable inexpensive cheap jerseys : Firstly, we should look at if it is working fine. China has been called the world’s factory, a lot cheap jerseys factories in China have foundries, but our cheap jerseys are all from here! Secondly, should to see whether it is the , we all know that it is difficult to get out of print once a genuine cheap cheap jerseys free shipping jersey was print. and we have all kind of stocka on the whole website, in other words, we have all you want ! Finally, look at the price, our price is not necessarily the lowest in the whole website but it must be most fair on the whole website, we certainly you will not regret later when you buy it. Of course, except that cheap jerseys, we also have the other products, such as socks, leggings and some other related products, everyone can enjoy the best services of here!