1 / 24

Effective Test Driven Database Development

Gojko Adzic http://gojko.net gojko@gojko.com. Effective Test Driven Database Development. Database somehow always sticks out. Lots of teams struggle with Database testing. Bad tools Inherently hard to test O/R Mismatch Changes are persistent Attitude of DB Specialists.

lynn
Télécharger la présentation

Effective Test Driven Database Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Gojko Adzic http://gojko.net gojko@gojko.com Effective Test Driven Database Development

  2. Database somehow always sticks out

  3. Lots of teams struggle with Database testing... • Bad tools • Inherently hard to test • O/R Mismatch • Changes are persistent • Attitude of DB Specialists

  4. Full build with all integration tests is the only thing you can really trust ...but it is not the most practical thing to run all the time

  5. Dedicated instances for developers are best One development DB can also work ok Build + n*integrations must always be separate DB management

  6. Don't count on the DB being empty Tests will be much more effective if they can ignore trash

  7. Generate the glue code and bring the two worlds closer together

  8. Don't fight against database features, use them! Run tests in transactions!

  9. ...Spring, Hibernate, FitNesse... • Declarative transactions, ORM controls the database... • So change the test runner and use the automation to your advantage... • !define TEST_RUNNER {test.RollbackServer} • http://gojko.net/2008/01/22/spring-rollback/

  10. ...Spring, Hibernate, FitNesse... public void process() { ApplicationContext ctx = new FileSystemXmlApplicationContext("lib/test.xml"); RollbackBean rollbackProcessingBean = (RollbackBean) ctx.getBean("rollback"); try { while ((size = FitProtocol.readSize(socketReader)) != 0) { try { rollbackProcessingBean.process( new DocumentRunner(size)); } catch (RollbackNow rn) { print("rolling back now" + "\n"); } } } catch (Exception e) { exception(e); } }

  11. ...Spring, Hibernate, FitNesse public class RollbackNow extends RuntimeException { } public class RollbackBean{ @Transactional public void process(Runnable r){ r.run(); throw new RollbackNow(); } }

  12. Isolate non-transactional tests and run them overnight

  13. Do not depend on the order of test execution

  14. Prepare everything you need for an isolated test

  15. Prepare everything you need... • Known state of small development databases • Link to files • Trash and restore DB • Start with a clean DB and Load data with DbUnit • Set up data for a specific test and roll back later • Ideally don't suppose that the DB is empty

  16. ORM is not magic!

  17. FIT+FitNesse+DB Fixtures http://fitnesse.info/dbfit http://sourceforge.net/projects/dbfit DBFIT: Test Driven DB Development Made Easy

  18. Why DbFit? • Manipulate data in a relational model • Provides all the plumbing • Transaction management • Smart features based on meta-data • Parameter mapping • “wizards” for regression tests • Because it runs inside FitNesse, already integrated with a lot of other tools/libraries

  19. Use DbFit to: • Write and execute DB Unit tests • Prepare/verify Java or .NET integration tests • .NET: Sql Server, Oracle, (DB2)‏ • Java: Mysql, Oracle, (DB2, SQL Server, Derby)‏

  20. Simple commands • Execute procedure • Query • Execute • Insert • Update

  21. FitNesse symbols directly mapped to bind variables • Retrieve auto-generated keys and use them directly • << and >> available in Java as well • Already mapped to bind variables

  22. Advanced features • Inspect queries, tables or procs to automatically generate test tables and regression tests • Store and compare queries • Standalone mode for full control

  23. Image credits • http://www.flickr.com/photos/seantubridy/ • http://www.flickr.com/photos/aasta/ • http://www.flickr.com/photos/guiniveve/ • http://flickr.com/photos/annevoi/ • http://www.flickr.com/photos/chaspope • http://www.flickr.com/photos/massenpunkt • http://www.flickr.com/photos/night_heron • http://www.flickr.com/photos/80336234@N00/476588081/

  24. Q & A • ... or just bug me later • gojko@gojko.com • http://gojko.net • http://twitter.com/gojkoadzic

More Related