android studio 如何进行单元测试

                                        android studio 如何单元测试

1 新建一个测试类Test,再写一个demoTest()测试方法

 

public class Test extends InstrumentationTestCase {
    public void demoTest(){
        assertEquals(1,1);
    }
}

 

2 右击鼠标,然后选中运行,点击Test按钮

 

 

 

3 运行结果

 很奇怪运行结果居然没有成功,网上翻了一些资料才查明了原因:测试类中的方法名必须要以test开头才行

 

 

4 把方法名改成test001后,再次运行,查看结果

 

 

5 如何获取context对象进行测试

 

public class Test extends InstrumentationTestCase {
    public void test001(){
        Context context = getInstrumentation().getContext();
        Toast.makeText(context,"测试成功",Toast.LENGTH_LONG);
        Assert.assertEquals(1,1);
    }
}

 

 

 

 

 

猜你喜欢

转载自1029457926.iteye.com/blog/2218590