Effective Test Driven Database Development


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Effective Test Driven Database Development

  1. 1. Effective Test Driven Database Development <ul><ul><li>Gojko Adzic </li></ul></ul><ul><ul><li>http://gojko.net </li></ul></ul><ul><ul><li>[email_address] </li></ul></ul>
  2. 2. What we'll talk about: <ul><li>Unit testing in the database </li></ul><ul><ul><li>Stored procedures </li></ul></ul><ul><ul><li>Data management </li></ul></ul><ul><li>Integration testing with Java/.NET </li></ul><ul><ul><li>xUnit and FIT/FitNesse </li></ul></ul><ul><ul><li>Preparing and verifying data </li></ul></ul><ul><ul><li>Key considerations with ORMs </li></ul></ul>
  3. 3. Lots of teams struggle with Database testing... <ul><li>Bad tools </li></ul><ul><li>Inherently hard to test </li></ul><ul><li>O/R Mismatch </li></ul><ul><li>Changes are persistent </li></ul><ul><li>Attitude of DB Specialists </li></ul>
  4. 4. But I use an ORM tool... <ul><li>You will still have integration issues </li></ul><ul><li>Session management and caching can hide serious problems </li></ul>
  5. 5. If it does not fit, look for a better solution <ul><li>Instead of fighting against database features, use them in your favour! </li></ul>
  6. 6. Run tests in a transaction <ul><li>This makes them instantly repeatable and isolated. </li></ul><ul><li>Alternatively – clean up after, but prefer transactions. </li></ul>
  7. 7. Running integration tests inside a transaction <ul><li>Often easier than you think... </li></ul><ul><li>Make sure that everything goes through the same DB connection </li></ul><ul><li>Set up the testing framework so that a transaction is started in set-up and rolled back on the end. </li></ul>
  8. 8. ...Spring, Hibernate, FitNesse... <ul><li>Declarative transactions, ORM controls the database... </li></ul><ul><li>So change the test runner and use the automation to your advantage... </li></ul><ul><li>!define TEST_RUNNER {test.RollbackServer} </li></ul><ul><li>http://gojko.net/2008/01/22/spring-rollback/ </li></ul>
  9. 9. ...Spring, Hibernate, FitNesse... <ul><li>public void process() { </li></ul><ul><li>ApplicationContext ctx = new </li></ul><ul><ul><ul><ul><li>FileSystemXmlApplicationContext(&quot;lib/test.xml&quot;); </li></ul></ul></ul></ul><ul><li>RollbackBean rollbackProcessingBean = (RollbackBean) </li></ul><ul><ul><ul><ul><li>ctx.getBean(&quot;rollback&quot;); </li></ul></ul></ul></ul><ul><li>try { </li></ul><ul><li>while ((size = FitProtocol.readSize(socketReader)) != 0) { </li></ul><ul><li>try { </li></ul><ul><li>rollbackProcessingBean.process( </li></ul><ul><ul><ul><ul><ul><li>new DocumentRunner(size)); </li></ul></ul></ul></ul></ul><ul><li>} catch (RollbackNow rn) { </li></ul><ul><li>print(&quot;rolling back now&quot; + &quot; &quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} catch (Exception e) { </li></ul><ul><li>exception(e); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  10. 10. ...Spring, Hibernate, FitNesse <ul><li>public class RollbackNow extends RuntimeException </li></ul><ul><li>{ } </li></ul><ul><li>public class RollbackBean{ </li></ul><ul><li>@Transactional </li></ul><ul><li>public void process(Runnable r){ </li></ul><ul><li>r.run(); </li></ul><ul><li>throw new RollbackNow(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  11. 11. If transactions are not possible... <ul><li>Eg build tests or integrated web tests </li></ul><ul><li>Preferably have a separate database instance for each developer and one for the build server. </li></ul><ul><li>Or a dev, build and integration db </li></ul><ul><li>Run these tests overnight </li></ul>
  12. 12. Count on stuff being in the database, but not the things that you need <ul><li>Make tests self-sufficient. </li></ul><ul><li>Don't count on the order of tests </li></ul><ul><li>Prepare everything you need for the test in the set-up. </li></ul><ul><li>Or restore a known state (DbUnit, custom loaders, base db)‏ </li></ul>
  13. 13. Unit tests have to run quickly ...Or people simply will not run them
  14. 14. Full build is the only thing you can really trust So have run full builds on a base DB and all the tests overnight
  15. 15. Reducing replication <ul><li>Maybe use “create” scripts and generate “update” scripts </li></ul><ul><li>Generate Java/.NET wrappers for stored procs </li></ul><ul><li>Generate object definitions and loaders </li></ul>
  16. 16. If you use an ORM tool... <ul><li>Flush on the end to make sure that DB and OO models are consistent </li></ul><ul><li>Have tests that commit and rehydrate objects in a different transaction to make sure that mappings are complete </li></ul>
  17. 17. DBFIT: Test Driven DB Development Made Easy FIT+FitNesse+DB Fixtures http://fitnesse.info/dbfit http://sourceforge.net/projects/dbfit
  18. 18. Why DbFit? <ul><li>Manipulate data in a relational model </li></ul><ul><li>Provides all the plumbing </li></ul><ul><ul><li>Transaction management </li></ul></ul><ul><ul><li>Smart features based on meta-data </li></ul></ul><ul><ul><li>Parameter mapping </li></ul></ul><ul><ul><li>“ wizards” for regression tests </li></ul></ul><ul><li>Because it runs inside FitNesse, already integrated with a lot of other tools/libraries </li></ul>
  19. 19. Use DbFit to: <ul><li>Write and execute DB Unit tests </li></ul><ul><li>Prepare/verify Java or .NET integration tests </li></ul><ul><ul><li>.NET: Sql Server, Oracle, (DB2)‏ </li></ul></ul><ul><ul><li>Java: Mysql, Oracle, (DB2, SQL Server, Derby)‏ </li></ul></ul>
  20. 20. Simple commands <ul><li>Execute procedure </li></ul><ul><li>Query </li></ul><ul><li>Execute </li></ul><ul><li>Insert </li></ul><ul><li>Update </li></ul>
  21. 21. FitNesse symbols directly mapped to bind variables <ul><li>Retrieve auto-generated keys and use them directly </li></ul><ul><li><< and >> available in Java as well </li></ul><ul><li>Already mapped to bind variables </li></ul>
  22. 22. Advanced features <ul><li>Inspect queries, tables or procs to automatically generate test tables and regression tests </li></ul><ul><li>Store and compare queries </li></ul><ul><li>Standalone mode for full control </li></ul>
  23. 23. Where next <ul><li>Beers at the Crown </li></ul><ul><li>ALT.NET community talk 31 st July </li></ul><ul><li>Agile 2008 in August </li></ul><ul><li>Next talk about testing: Selenium 28 th Sept </li></ul><ul><li>http://gojko.net </li></ul>
  24. 24. Image credits <ul><li>http://www.flickr.com/photos/seantubridy/ </li></ul><ul><li>http://www.flickr.com/photos/aasta/ </li></ul><ul><li>http://www.flickr.com/photos/guiniveve/ </li></ul>