单元测试技术讲解
}
单元测试
• JUnit实践-查看测试结果
单元测试
• 测试技巧:异常测试
• public void testEncoding() {
– String str = "太阳"; – try {
• String utf8Str = Base.encoding(str, "GBK", "ErrorChar"); • fail();
ቤተ መጻሕፍቲ ባይዱ元测试
XUnit模式
• 全部测试(All Tests) 一次性执行所有的测试。 public class AllTests { public static void main() { run(AllTests.class); } public static Test suite() { TestSuite result = new TestSuite(“P tests”); result.addTestSuite(ATest.class); result.addTestSuite(BTest.class); result.addTestSuite(CTest.class); return result; } }
– assertEquals(ac[i], by[i]);
• } }
• }
单元测试
• • • • • • • JUnit实践-测试类中一次运行多个测试 public static void main(String[] args) {
– junit.textui.TestRunner.run(BaseTest.class);
单元测试
• 1. 2. 3. 4. 5. 单元测试的误区 编写单元测试太花时间了 运行测试代码的时间太长 测试代码并不是我的工作 我不清楚代码的行为,所以无从测试 单元测试代码不规范
单元测试
• XUnit模式-结构
TestSuites
TestCase
Test [Java method] Fixtures: common setup/teardown code
– assertEquals(ac[i], by[i]);
} String gbkStr = Base.utf82gbk(utf8Str); by = gbkStr.getBytes("GBK"); ac = new byte[] { -97, -10 }; for (int i = 0; i < 2; i++) {
单元测试
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 业务对象模拟<1> public void testLoginByServiceOK() { final HttpServletRequest httpServletRequest = context .mock(HttpServletRequest.class); final HttpSession httpSession = context.mock(HttpSession.class); final ILoginService loginService = context.mock(ILoginService.class); final String userID = "wangwu"; final String password = "*******"; final String checkCode = "1234"; LoginBean loginBean = new LoginBean(httpServletRequest); loginBean.setLoginService(loginService); // expectations context.checking(new Expectations() { { atMost(4).of(httpServletRequest).getSession(); will(returnValue(httpSession)); } }); context.checking(new Expectations() { { one(httpSession).getAttribute("checkCode"); will(returnValue("1234")); } }); context.checking(new Expectations() { { one(httpSession).setAttribute("isLogin", "true"); ; } }); context.checking(new Expectations() {
单元测试
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • HttpRequest模拟 public void testLogin() { final HttpServletRequest httpServletRequest = context .mock(HttpServletRequest.class); final HttpSession httpSession = context.mock(HttpSession.class); String userID = "wangwu"; String password = "*******"; String checkCode = "1234"; LoginBean loginBean = new LoginBean(httpServletRequest); // expectations context.checking(new Expectations() { { atMost(3).of(httpServletRequest).getSession(); will(returnValue(httpSession)); } }); context.checking(new Expectations() { { one(httpSession).getAttribute("checkCode"); will(returnValue("1234")); } }); context.checking(new Expectations() { { one(httpSession).setAttribute("isLogin", "true"); ; } }); assertTrue("login success", loginBean .login(userID, password, checkCode)); }
3. 4. 5.
单元测试
• 对项目的意义 1. 模块更加可控 2. 及早发现问题,提高模块质量,降低系统 测试成本 3. 发现模块构架调整中潜在Bug 4. 加速开发周期,单元测试是最容易自动化 测试的,成本最低 单元测试不应该仅是程序员的个人行为,而 与项目进度控制、质量控制息息相关
单元测试
• 单元测试的范畴 1. 它的行为和我期望的一致吗? 单元测试的 根本目的 2. 它的行为一直和我期望的一致吗? 考虑异 常情况 3. 我可以依赖单元测试吗? 一定要值得依赖 4. 单元测试说明我的意图了吗? 能充分了解 代码用法
单元测试技术讲解
讲解人:王铁吾
内容
• • • • • 软件开发的困难 测试技术简单介绍 单元测试 测试驱动开发 问题及解答
软件开发的困难
• 1. 2. 3. 4. 软件总是在不断修改 添加新特性 修改BUG 优化 重构
软件开发的困难
• 1. 2. 3. 4. 5. 6. 一般我们如何测试修改 编译 启动服务器 启动客户端(或者浏览器) 鼠标或键盘操作 查看结果 重复这些操作
} public BaseTest(String arg0) {
– super(arg0);
} public static Test suite() { TestSuite lSuite = new TestSuite();
– – – – lSuite.addTest(new BaseTest("testGbk2utf8")); lSuite.addTest(new BaseTest("testWideAngle2HaleAngle")); lSuite.addTest(new BaseTest("testIsAllHalfAngle")); return lSuite;
– public void testGbk2utf8() throws UnsupportedEncodingException {
• • • • • • • • • • String str = "燊"; String utf8Str = Base.gbk2utf8(str); byte[] by = utf8Str.getBytes("utf-8"); byte[] ac = new byte[] { -25, -121, -118 }; for (int i = 0; i < 3; i++) {
•
}
单元测试
• • • JUnit实践-整个Package的测试类 public class AllTests { public static Test suite() {
– – – – – – – – – – – – – – – } TestSuite suite = new TestSuite("Test for eet.evar.tool"); //$JUnit-BEGIN$ suite.addTest(PseuRandomTest.suite()); suite.addTestSuite(IniPropertiesTest.class); suite.addTest(LoggerTest.suite()); suite.addTestSuite(ConfigurationTest.class); suite.addTest(DateFormatTest.suite()); suite.addTestSuite(ConsistentHashTest.class); suite.addTestSuite(NetworkUtilsTest.class); suite.addTestSuite(MD5Test.class); suite.addTestSuite(IdGeneratorTest.class); suite.addTestSuite(FileDealTest.class); suite.addTestSuite(HMACSHA1Test.class); //$JUnit-END$ return suite;