




已阅读5页,还剩35页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1,测试驱动的设计和开发(TestDrivenDesignandDevelopment)基础篇,CharlesHuangimportjunit.framework.TestSuite;importjunit.framework.Test;publicclassAccountDAOmySqlTestextendsTestCasepublicAccountDAOmySqlTest(Stringname)super(name);,27,Junit(Asample),2.OverridesetUp()andtearDown()如果需要,可以在setUp()中初始化需要的全局变量,资源等(比如DatabaseConnection,FileI/O或MockObjects等)相应的,可以在tearDown()中释放资源(DatabaseConnection,FileI/O和MockObjects等)publicclassAccountDAOmySqlTestextendsTestCaseprivateMysqlFixturemysqlFixtureIns=newMysqlFixture();publicvoidsetUp()throwsExceptionmysqlFixtureIns.setUp();publicvoidtearDown()throwsExceptionmysqlFixtureIns.tearDown();,28,Junit(Asample),3.为被测试的Method写TestCasepublicvoidtestAddAccountNormal()AccountDAOmySqldao=newAccountDAOmySql();Accountaccount=newAccount(USER_NAME,charles,charles_hhb);trydao.addAccount(account);Accountaccount_new=dao.findByUserName(account.getUserName();assertEquals(Expectingcharles,account.getUserName(),account_new.getUserName();assertEquals(Expectingcharles,account.getPassword(),account_new.getPassword();assertEquals(Expectingcharles,account.getEmail(),account_new.getEmail();dao.removeAccount(account.getUserName();catch(finalExceptione)e.printStackTrace();fail(Unexpectedexception:+e.toString();,29,Junit(Asample),publicvoidtestAddAccountAlreadyExist()AccountDAOmySqldao=null;Accountaccount=null;try/AddanAccountdao.addAccount(account);fail(AccountAlreadyExistExceptionexpected);catch(finalSQLExceptione)e.printStackTrace();fail(Unexpectedexception:+e.toString();catch(finalAccountNotFoundExceptionnotFound)notFound.printStackTrace();fail(Unexpectedexception:+notFound.toString();catch(finalAccountAlreadyExistExceptionex)/Passtrydao.removeAccount(account.getUserName();catch(finalSQLExceptionsql)sql.printStackTrace();fail(Unexpectedexception);,30,Junit(ASample),运行这个UnitTest。Junit提供两种运行界面:Swing(junit.swingui.TestRunner)C:sandboxforumjava-classpath./classes;./lib/junit.jar;./lib/mysql_jdbc.jar;./lib/Tidy.jar;./lib/struts.jarjunit.swingui.TestRunnerorg.redsoft.forum.dao.mysql.AccountDAOmySqlTest,31,Junit(ASample),Text界面(junit.textui.TestRunner),32,JunitTestSuite,TestSuite用来运行所有的UnitTestsTestSuite的数型结构:org.redsoft.forum.AllTest|-org.redsoft.forum.dao.AllTest|-org.redsoft.forum.dao.mysql.AllTest|-org.redsoft.forum.util.AllTest每个PackageLevel都由一个AllTestTestSuite在每个TestSuite中,加入在本packagelevel中的所有单元测试例子(UnitTestCases)加入子Packagelevel中的所有AllTestSuite,33,JunitTestSuite,packageorg.redsoft.forum.dao;publicclassAllTestspublicstaticvoidmain(Stringargs)junit.textui.TestRunner.run(suite();publicstaticTestsuite()TestSuitesuite=newTestSuite();/加入子package中的AllTestsuitesuite.addTest(org.redsoft.forum.dao.mysql.AllTests.suite();/加入本packagelevel中的UnitTestcasesuite.addTestSuite(MysqlDataSourceTest.class);returnsuite;/EOC,34,JFCUnit,一个Junit的Extension,用来测试Swing-based的Application。一个最简单的Sample:测试一个LoginScreen,35,JFCUnit,代码片断:设置测试环境privateLoginScreenloginScreen=null;privateTestHelperhelper=null;publicLoginScreenTest(Stringname)super(name);protectedvoidsetUp()throwsExceptionsuper.setUp();helper=newJFCTestHelper();loginScreen=newLoginScreen(LoginScreenTest:+getName();loginScreen.setVisible(true);protectedvoidtearDown()throwsExceptionloginScreen=null;helper.cleanUp(this);super.tearDown();,代码片断:测试图形界面JDialogdialog;JButtonexitButton=(JButton)helper.findNamedComponent(ExitButton,loginScreen,0);assertNotNull(CouldnotfindtheExitbutton,exitButton);JButtonenterButton=(JButton)helper.findNamedComponent(EnterButton,loginScreen,0);assertNotNull(CouldnotfindtheEnterbutton,enterButton);JTextFielduserNameField=(JTextField)helper.findNamedComponent(LoginNameTextField,loginScreen,0);assertNotNull(CouldnotfindtheuserNameField,userNameField);assertEquals(Usernamefieldisempty,userNameField.getText();JTextFieldpasswordField=(JTextField)helper.findNamedComponent(PasswordTextField,loginScreen,0);assertNotNull(CouldnotfindthepasswordField,passwordField);assertEquals(Passwordfieldisempty,passwordField.getText();,36,利用Ant来做NightlyTest,使用Ant中的两个Tasks来完成自动运行NightlyTest产生一个XML个是的结果报告再利用JunitReport来产生一个可供浏览的结果文件。,37,测试例子覆盖率,利用NOUnit来获得测试例子的覆盖率(,38,UnitTests:100%always,任何时候如果UnitTests的出现错误(Junit的进度指示显示红色),XPTeam的首要工作就是修补UnitTests直至Junit的进度指示为绿色。如果不修复出错的测试例子,就会出现滚雪球效应,在未知质量代码基础上的开发只会导致更多的未知质量的代码。软件质量的基石就
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 自主实习第三方协议书
- 2025购房补贴借款合同协议书
- 酶制剂微生物菌种工岗位职业健康及安全技术规程
- tcp ip协议书原理与应用
- 防护护栏协议书
- 湖北省恩施州巴东县2026届数学九上期末考试试题含解析
- 公司间苯二酚装置操作工工艺技术规程
- 2025沥青采购合同范本
- 天津市武清区名校2026届数学七年级第一学期期末质量检测试题含解析
- 2025企业管理资料范本劳动合同样本
- 2025贵州册亨县招聘教师25人考试参考试题及答案解析
- 煤矿安全规程2025版解读
- 乙型肝炎病毒护理查房
- 高血压与糖尿病防治课件
- 材料进场验收流程标准化管理
- 2025-2030年矿山机械行业市场深度分析及前景趋势与投资研究报告
- 《慢性伤口治疗与护理》课件
- 2024-2025学年劳动五年级上册制作扇子 教学设计+教学设计人教版
- 2025年交管12123学法减分考试题库及答案
- 中国插花撒技艺 课件全套 第0-6章 导论、传统插花定枝技法和工具的演变-占景撒和基本撒的结合
- 北京建工集团合同范本
评论
0/150
提交评论