测试驱动的设计和开发(TestDrivenDesignand

上传人:ldj****22 文档编号:51335515 上传时间:2018-08-13 格式:PPT 页数:40 大小:514.50KB
返回 下载 相关 举报
测试驱动的设计和开发(TestDrivenDesignand_第1页
第1页 / 共40页
测试驱动的设计和开发(TestDrivenDesignand_第2页
第2页 / 共40页
测试驱动的设计和开发(TestDrivenDesignand_第3页
第3页 / 共40页
测试驱动的设计和开发(TestDrivenDesignand_第4页
第4页 / 共40页
测试驱动的设计和开发(TestDrivenDesignand_第5页
第5页 / 共40页
点击查看更多>>
资源描述

《测试驱动的设计和开发(TestDrivenDesignand》由会员分享,可在线阅读,更多相关《测试驱动的设计和开发(TestDrivenDesignand(40页珍藏版)》请在金锄头文库上搜索。

1、 Copyright 2002 Chinaxp. All rights reserved测试驱动的设计和开发 ( Test Driven Design and Development ) 基础篇Charles Huang import junit.framework.TestSuite; import junit.framework.Test;public class AccountDAOmySqlTest extends TestCase public AccountDAOmySqlTest(String name) super(name); 1111126 Copyright 2002 C

2、hinaxp. All rights reservedJunit( A sample)2. Override setUp() and tearDown() 如果需要,可以在setUp()中初始化需要的全局变量,资源等(比如 Database Connection, File I/O或Mock Objects等) 相应的,可以在tearDown()中释放资源(Database Connection,File I/O和Mock Objects等) public class AccountDAOmySqlTest extends TestCase private MysqlFixture mysql

3、FixtureIns = new MysqlFixture(); public void setUp() throws Exception mysqlFixtureIns.setUp(); public void tearDown() throws Exception mysqlFixtureIns.tearDown(); 1111127 Copyright 2002 Chinaxp. All rights reservedJunit( A sample)3.为被测试的Method写Test Case public void testAddAccountNormal() AccountDAOm

4、ySql dao = new AccountDAOmySql(); Account account = new Account(USER_NAME,“charles“,“charles_“); trydao.addAccount( account );Account account_new = dao.findByUserName( account.getUserName() );assertEquals(“Expecting charles“,account.getUserName(),account_new.getUserName() );assertEquals(“Expecting c

5、harles“,account.getPassword(),account_new.getPassword() );assertEquals(“Expecting “,account.getEmail(),account_new.getEmail() );dao.removeAccount( account.getUserName() );catch( final Exception e )e.printStackTrace();fail(“Unexpected exception:“ + e.toString(); 1111128 Copyright 2002 Chinaxp. All ri

6、ghts reservedJunit( A sample)public void testAddAccountAlreadyExist() AccountDAOmySql dao = null; Account account = null; try / Add an Account dao.addAccount( account ); fail(“AccountAlreadyExistException expected“); catch( final SQLException e )e.printStackTrace(); fail(“Unexpected exception:“ + e.

7、toString(); catch( final AccountNotFoundException notFound ) notFound.printStackTrace(); fail(“Unexpected exception: “ + notFound.toString() );catch( final AccountAlreadyExistException ex ) / Pass try dao.removeAccount( account.getUserName() ); catch( final SQLException sql ) sql.printStackTrace();

8、fail(“Unexpected exception “); 1111129 Copyright 2002 Chinaxp. All rights reservedJunit( A Sample) 运行这个Unit Test。 Junit提供两种运行界面:Swing(junit.swingui.TestRunner)C:sandboxforumjava -classpath ./classes;./lib/junit.jar;./lib/mysql_jdbc.jar;./lib/Tidy.jar;./lib/struts.jar junit.swingui.TestRunner org.red

9、soft.forum.dao.mysql.AccountDAOmySqlTest1111130 Copyright 2002 Chinaxp. All rights reservedJunit( A Sample)Text界面( junit.textui.TestRunner )1111131 Copyright 2002 Chinaxp. All rights reservedJunit Test SuiteTest Suite用来运行所有的Unit Tests Test Suite的数型结构: org.redsoft.forum.AllTest | |-org.redsoft.forum.

10、dao.AllTest | |-org.redsoft.forum.dao.mysql.AllTest | |-org.redsoft.forum.util.AllTest每个Package Level都由一个AllTest Test Suite 在每个Test Suite中, 加入在本package level中的所有单元测试例子( Unit Test Cases ) 加入子Package level中的所有AllTest Suite1111132 Copyright 2002 Chinaxp. All rights reservedJunit Test Suitepackage org.r

11、edsoft.forum.dao; public class AllTests public static void main(String args) junit.textui.TestRunner.run(suite(); public static Test suite() TestSuite suite = new TestSuite();/ 加入子package中的AllTest suite suite.addTest(org.redsoft.forum.dao.mysql.AllTests.suite();/ 加入本package level中的Unit Test case sui

12、te.addTestSuite(MysqlDataSourceTest.class);return suite; /EOC1111133 Copyright 2002 Chinaxp. All rights reservedJFC Unit一个Junit的Extension,用来测试Swing-based的Application。 一个最简单的Sample:测试一个Login Screen1111134 Copyright 2002 Chinaxp. All rights reservedJFC Unit代码片断:设置测试环境private LoginScreen loginScreen =

13、null; private TestHelper helper = null; public LoginScreenTest( String name ) super( name ); protected void setUp( ) throws Exception super.setUp( ); helper = new JFCTestHelper( );loginScreen = new LoginScreen( “LoginScreenTest: “ + getName( ) ); loginScreen.setVisible( true ); protected void tearDo

14、wn( ) throws Exception loginScreen = null; helper.cleanUp( this ); super.tearDown( ); 代码片断:测试图形界面 JDialog dialog; JButton exitButton = ( JButton ) helper.findNamedComponent( “ExitButton“, loginScreen, 0 ); assertNotNull( “Could not find the Exit button“, exitButton ); JButton enterButton = ( JButton

15、 ) helper.findNamedComponent( “EnterButton“, loginScreen, 0 ); assertNotNull( “Could not find the Enter button“, enterButton ); JTextField userNameField = ( JTextField ) helper.findNamedComponent( “LoginNameTextField“,loginScreen, 0 ); assertNotNull( “Could not find the userNameField“, userNameField

16、 ); assertEquals( “Username field is empty“, “, userNameField.getText( ) ); JTextField passwordField = ( JTextField ) helper.findNamedComponent( “PasswordTextField“, loginScreen, 0 ); assertNotNull( “Could not find the passwordField“, passwordField ); assertEquals( “Password field is empty“, “, passwordField.getText( )

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号