代码测试的立足点是Code,是基于代码基础之上的,而传统的功能测试和接口测试是基于应用的,必须对应的测试系统是在运行中的。
代码测试不会特别注重接口测试的可持续性集成。
代码测试的特点是快捷高效准确的完成测试工作,快速推进产品的迭代。
2. Code Test 的方法:
(1) 代码走读和review
适合场景:逻辑相对简单,有较多的边界值。
方法介绍:直接查看和阅读代码,检验逻辑是否正确。
(2) 代码debug与代码运行中测试
适合场景:数据构造比较困难,特殊的场景覆盖。
方法介绍:1.直接在debug代码过程中查看数据流走向,校验逻辑。
2.在debug过程中直接将变量的值或者对象的值直接改成想要的场景
(3) 私有方法测试
适合场景:需要测试的类的复杂逻辑处理是放在一个特定的方法中,而且该方法中没有使用到其他引用的bean
方法介绍:通过反射的方式调用方法,进行测试。
例子:
假设有一个待测试的类叫MyApp,有一个私有方法叫getSortList, 参数是一个整形List。
/** * Created by yunmu.wgl on 2014/7/16. */ public class MyApp { private List getSortList(List<Integer> srcList){ Collections.sort(srcList); return srcList; } } |
那么测试类的代码如下:
/** * Created by yunmu.wgl on 2014/7/16. */ public class MyAppTest { @Test public void testMyApp() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Class clazz = MyApp.class; Method sortList = clazz.getDeclaredMethod("getSortList",List.class); //获取待测试的方法 sortList.setAccessible(true); //私有方法这个是关键 List<Integer> testList = new ArrayList<Integer>();//构造测试数据 testList.add(1); testList.add(3); testList.add(2); MyApp myApp = new MyApp(); // 新建一个待测试类的对象 sortList.invoke(myApp,testList); //执行测试方法 System.out.println(testList.toString()); //校验测试结果 } } |