测试驱动开发TDD培训义PPT课件_第1页
测试驱动开发TDD培训义PPT课件_第2页
测试驱动开发TDD培训义PPT课件_第3页
测试驱动开发TDD培训义PPT课件_第4页
测试驱动开发TDD培训义PPT课件_第5页
已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论