5d5f6157e7e20dcbb6202833206ec09a.ppt
- Количество слайдов: 17
Testing Web Applications with Html. Unit and Dbunit Michael J. Bresnahan Fruition Consulting Inc.
Agenda • • About Me, Mike Bresnahan Brief Overview of Html. Unit and Dbunit Benefits of Automated Testing About Html. Unit About Dbunit Designing Tests Questions and Answers
About Me, Mike Bresnahan • 9 years of software development experience – – – Enterprise applications C, C++, Java Relational Databases Client/Server Web 1 year of Html. Unit and Dbunit experience
A Brief Overview of Html. Unit and Dbunit • Both are Java APIs for automated functional testing. – Html. Unit is a Java API for automated testing of web applications. – Dbunit is a JUnit extension for automated testing of database applications. • Both are Java API’s, but can be used to test applications written in any language. • Both are Open Source.
Benefits of Automated Testing • More efficient than manual testing. • Less error prone than manual testing. • Enables collective code ownership. • Enables refactoring. • Enables frequent integration.
About Html. Unit Test Client Web Server Application Server
Html. Unit Features • • HTTP 1. 0 and 1. 1 HTTPS HTML 4 Cookies Proxy Servers Java. Script Simulated Mouse Clicks DOM Search and Navigation
Simple Html. Unit Test 1 public class Simple. Html. Unit. Test extends junit. framework. Test. Case { 2 3 public void test. Home. Page() throws Exception { 4 Web. Client web. Client = new Web. Client(); 5 java. net. URL url = new 6 java. net. URL("http: //htmlunit. sourceforge. net"); 7 8 Html. Page page = (Html. Page) web. Client. get. Page(url); 9 10 assert. Equals("Welcome to Html. Unit", page. get. Title. Text()); 11 } 12 }
HTML Form Test 1 public class Form. Test extends junit. framework. Test. Case { 2 3 public void test. Form() throws Exception { 4 Web. Client web. Client = new Web. Client(); 5 java. net. URL url = 6 new java. net. URL("http: //htmlunit. sourceforge. net"); 7 8 Html. Page page = (Html. Page)web. Client. get. Page(url); 9 10 Html. Form form = page. get. Form. By. Name("myform"); 11 12 Html. Text. Input text. Input = 13 (Html. Text. Input)form. get. Input. By. Name("myfield"); 14 15 text. Input. set. Value. Attribute("zappa zoo yeah!"); 16 17 form. submit(); 18 } 19 }
Button Click Test 1 public class Button. Click. Test extends junit. framework. Test. Case { 2 3 public void test. Button. Click() throws Exception { 4 Web. Client web. Client = new Web. Client(); 5 java. net. URL url = 6 new java. net. URL("http: //htmlunit. sourceforge. net"); 7 8 Html. Page page = (Html. Page) web. Client. get. Page(url); 9 10 Html. Form form = page. get. Form. By. Name("myform"); 11 12 Html. Button. Input button. Input = 13 (Html. Button. Input) form. get. Input. By. Name("mybutton"); 14 15 Html. Page new. Page = (Html. Page)button. Input. click(); 16 } 17 }
About Dbunit XML Database
Dbunit Features • • • DTD database schemas Import/export XML datasets Manipulate datasets Compare datasets Ant task
Abstract Database Test 1 public abstract class Abstract. Database. Test. Case 2 extends org. dbunit. Database. Test. Case { 3 4 protected IDatabase. Connection get. Connection() throws Exception { 5 java. sql. Connection connection = java. sql. Driver. Manager. 6 get. Connection("jdbc: oracle: thin: @localhost: 1521: foo"); 7 return new Database. Connection(connection); 8 } 9 10 protected Database. Operation get. Set. Up. Operation() throws Exception{ 11 return Database. Operation. CLEAN_INSERT; 12 } 13 14 protected IData. Set get. Data. Set() throws Exception { 15 return new Flat. Xml. Data. Set(new java. io. File("dataset. xml")); 16 } 17 }
Dataset. xml <!DOCTYPE dataset SYSTEM "dataset. dtd"> <dataset> <STUDY_SESSIONS ID="1" AIMR_AREAS_ID="1" STUDY_SESSION_CODE="1“/> <STUDY_SESSIONS ID="2" AIMR_AREAS_ID="1" STUDY_SESSION_CODE="2“/> <READINGS ID="1" STUDY_SESSIONS_ID="1" READING_CODE="1"/> <READINGS ID="2" STUDY_SESSIONS_ID="2" READING_CODE="2"/> </dataset>
Simple Dbunit Test 1 public void test() throws Exception { 2 3 // exercise the application 4 5 IData. Set expected. Data. Set = 6 new Flat. Xml. Data. Set(new java. io. File("expected. xml")); 7 8 ITable expected. Table = expected. Data. Set. get. Table(“readings"); 9 10 ITable database. Table = get. Connection(). 11 create. Query. Table(“readings", "select * from readings"); 12 13 Assertion. assert. Equals(expected. Table, database. Table); 14 }
Designing Tests • • • Keep tests independent Keep tests small and single purpose Pool database connections Load common reference data once Engineer the test suite Decouple tests from the application as much as possible • Use Dbunit to hold the expected data of web pages
Further Reading • http: //htmlunit. sf. net • http: //dbunit. sf. net • http: //junit. org
5d5f6157e7e20dcbb6202833206ec09a.ppt