Your SlideShare is downloading. ×
0
TDD with DbFit and OracleWriting readable,easyto maintain unitand integration testsfordatabasecodeYavor NikolovBGOUG Confe...
Agenda#bgoug2013Concepts (Testing, Test-Driven Development)DbFitDemo2/56
What is a legacy system?#bgoug2013You spot an obvious design problemknow how to improve that,but the thought about consequ...
#bgoug2013source: http://lisacrispin.com/2011/11/08/using-the-agile-testing-quadrants4/56
Why (automated) testing?#bgoug2013Makes application change easierSafety net - provides confidence/removes fearDocumentatio...
Test Fixture#bgoug2013All the things we need to have in place in order torun a test and expect a particular outcomeThe tes...
System Under Test (SUT)The system that is being tested#bgoug2013 7/56
Test execution cycle#bgoug2013Arrange (set up the Fixture)Act (exercise the System Under Test)Assert (verify results are a...
Unit test#bgoug2013Tests small individual unit (module,procedure/function)In isolation (no interaction with other units)Sh...
Integration test#bgoug2013Tests several modules as a groupSlower than unit tests (usually)10/56
Acceptance test#bgoug2013Conducted to determine whether or not a systemsatisfies its acceptance criteriaand to enable the ...
Regression tests(Regress vs progress)Performed to make sure that previously workingfunctionality still works after changes...
RefactorIs the the process of changing a system in such a waythatDoing refactoring without tests is unsafe#bgoug2013doesnt...
TDD Cycle#bgoug2013source: Internet14/56
Tests are not the main product in TDD#bgoug2013TDD is a design techniqueThe design emerges in small steps15/56
Why test first?#bgoug2013Start with end in mind (think from point of view ofcaller)This perspective helps for better desig...
Cost of change (traditional)#bgoug2013 17/56
Cost of change (test early)#bgoug2013 18/56
Why testing the Database?#bgoug2013For lot of businesses, data held in DB are the mostvital commercial asset they haveBusi...
Challenges of Database testing...#bgoug2013Bad toolsInherently hard to test. Isolation is difficultAttitude ("its not my j...
How to isolate db tests?Run tests in one transaction#bgoug2013Makes them repeatable and independentWhen one transaction is...
How to isolate db tests (2)?Dedicated database#bgoug2013One db per contributorSeparate schemasShared Dev db may work tooAs...
Other Tips#bgoug2013Make tests self-sufficientDont count on the order of testsPrepare everything you need for the test in ...
#bgoug2013DbFit24/56
What is DbFit?#bgoug2013Initially created by Gojko Adzic:Enables manipulating database objects and definingtests in tabula...
DbFit, FIT and FitNesse#bgoug2013DbFit is based on FIT+FitNesse+DB Fixtures whichenable FIT/Fitnesse tests to execute dire...
FitNesse architecture#bgoug2013source: Fittnesse User Guilde - One Minute Description27/56
What is DbFit Fixture?#bgoug2013A fixture is interface between:In general there is 1:1 mapping between Fit tableand fixtur...
Why DbFit?#bgoug2013Easy to use (even for non-technical people)Provides all the plumbing:Runs inside FitNesse - already in...
What is Wiki?#bgoug2013The simplest online database that could possiblywork. - Ward CunninghamAllows users to freely creat...
Fitnesse Wiki#bgoug2013Hierarchies - SubWiki, Test SuitesPage types - Suite, Test, StaticSome special pages:http://fitness...
A Unit test with DbFit#bgoug2013Set up the input data (arrange).Execute a function or procedure (act).Run a query and comp...
Basic commands of DbFit#bgoug2013QueryInsertUpdateExecute ProcedureExecute33/56
Advanced features#bgoug2013Inspect queries, tables, procedures to auto-generatetest tables and regression testsStore and c...
Getting started#bgoug2013Needs Java to runDownload: http://benilovj.github.io/dbfitUnzipCopy Oracle JDBC driver (ojdbc6.ja...
Connecting to the databaseInline configuration:Using properties file:#bgoug2013!|Connect|localhost:1521|username|password|...
Query#bgoug2013!|insert|testtbl||n |name ||1 |NAME1 ||3 |NAME3 ||2 |NAME2 |!|query|select*fromtesttbl||n |name ||1 |NAME1 ...
Ordered Query#bgoug2013!|OrderedQuery|select*fromtesttblorderbyn||n |name? ||1 |NAME1 ||3 |NAME3 ||2 |NAME2 |38/56
Insert#bgoug2013!|insert|testtbl||n |name ||1 |NAME1 ||3 |NAME3 ||2 |NAME2 |39/56
Execute Procedure#bgoug2013!2Noparameters!|ExecuteProcedure|do_stuff|!2Functions-returnvalueswith"?"!|ExecuteProcedure|zlp...
Expect exception#bgoug2013!2ExpectORA-20013!|Executeprocedureexpectexception|set_age|20013||p_age ||-5 |41/56
Parameters and fixture symbols#bgoug2013set parameterto set parameter directly>>paramname- store a value<<paramname- read ...
Store Query#bgoug2013!|StoreQuery|select1nfromdualunionselect2nfromdual|firsttable|!|query|<<firsttable||n ||1 ||2 |43/56
Compare Stored Queries#bgoug2013!|insert|testtbl||n |name ||1 |NAME1 ||3 |NAME3 ||2 |NAME2 ||StoreQuery|select*fromtesttbl...
Working Modes of fixtures#bgoug2013FlowStandalone45/56
Working Modes of fixtures (2)Flow mode#bgoug2013A DatabaseTest fixture controls the whole page andcoordinates testingAutom...
Working Modes of fixtures (3)Standalone mode#bgoug2013We are responsible for transaction managementEnables more control ov...
Connecting to databse#bgoug2013!3InFlowmode!|dbfit.OracleTest|!|Connect|ourhost:1521|dbusername|dbpassword|mydb|#Alternati...
Integration test with SQL*Loader#bgoug2013Compile CommandLineFixture (by Bob Martin)Use it to run a shell script!3Loadsome...
Automating tests execution#bgoug2013Running tests from command lineRun test or suite as RESTful servicehttp://fitnesse.org...
How tests are stored?#bgoug2013Simple text filescontent.txt - test definition and other Wiki contentproperties.xml - metad...
#bgoug2013Demo52/56
Summary#bgoug2013Changes of database code and schema are oftenrelatively hardThis makes the systems considered legacyTDD s...
Resources#bgoug2013http://benilovj.github.io/dbfit - with links to:https://github.com/javornikolov/tdd-with-dbfit-bgoug-20...
Thank You!nikolov dot javor at gmail
Q&A#bgoug2013 56/56
Upcoming SlideShare
Loading in...5
×

Test-Driven Development with DbFit and Oracle database, BGOUG Conference, 2013-05-18

6,044

Published on

Test-Driven Development with DbFit and Oracle database, BGOUG Conference, 2013-05-18

Published in: Technology

Transcript of "Test-Driven Development with DbFit and Oracle database, BGOUG Conference, 2013-05-18"

  1. 1. TDD with DbFit and OracleWriting readable,easyto maintain unitand integration testsfordatabasecodeYavor NikolovBGOUG Conference, 2013-05-18
  2. 2. Agenda#bgoug2013Concepts (Testing, Test-Driven Development)DbFitDemo2/56
  3. 3. What is a legacy system?#bgoug2013You spot an obvious design problemknow how to improve that,but the thought about consequences gives you astomach ache.source: Gojko Adzic, "Fighting the monster"3/56
  4. 4. #bgoug2013source: http://lisacrispin.com/2011/11/08/using-the-agile-testing-quadrants4/56
  5. 5. Why (automated) testing?#bgoug2013Makes application change easierSafety net - provides confidence/removes fearDocumentationHelp to localize where exactly a defect is locatedReduce the chance of new bugsAutomation enables earlier feedback, saves time,helps focusing on solving the main problem. (Noteverything is feasible to automate)5/56
  6. 6. Test Fixture#bgoug2013All the things we need to have in place in order torun a test and expect a particular outcomeThe test context6/56
  7. 7. System Under Test (SUT)The system that is being tested#bgoug2013 7/56
  8. 8. Test execution cycle#bgoug2013Arrange (set up the Fixture)Act (exercise the System Under Test)Assert (verify results are as expected)Tear Down the fixture (to isolate other tests fromthis one)1.2.3.4.8/56
  9. 9. Unit test#bgoug2013Tests small individual unit (module,procedure/function)In isolation (no interaction with other units)Should run quickly (otherwise people wont runthem)9/56
  10. 10. Integration test#bgoug2013Tests several modules as a groupSlower than unit tests (usually)10/56
  11. 11. Acceptance test#bgoug2013Conducted to determine whether or not a systemsatisfies its acceptance criteriaand to enable the customer to determine whether ornot to accept the system.At least modeled and possibly even written by thecustomerEnd-to-end (slower than Integration & Unit tests)11/56
  12. 12. Regression tests(Regress vs progress)Performed to make sure that previously workingfunctionality still works after changes elsewhere inthe system#bgoug2013 12/56
  13. 13. RefactorIs the the process of changing a system in such a waythatDoing refactoring without tests is unsafe#bgoug2013doesnt alter external behaviourand improves its internal structure (design)through small steps13/56
  14. 14. TDD Cycle#bgoug2013source: Internet14/56
  15. 15. Tests are not the main product in TDD#bgoug2013TDD is a design techniqueThe design emerges in small steps15/56
  16. 16. Why test first?#bgoug2013Start with end in mind (think from point of view ofcaller)This perspective helps for better designTest coverage is useful byproductGreatly reduces the need of debugging16/56
  17. 17. Cost of change (traditional)#bgoug2013 17/56
  18. 18. Cost of change (test early)#bgoug2013 18/56
  19. 19. Why testing the Database?#bgoug2013For lot of businesses, data held in DB are the mostvital commercial asset they haveBusiness critical functions rely on this dataSo it makes sense to validate that data is stored andprocessed correctly19/56
  20. 20. Challenges of Database testing...#bgoug2013Bad toolsInherently hard to test. Isolation is difficultAttitude ("its not my job")Too much boilerplate codeOO tools not directly applicable for RDBMSChanges are persistentShared environmentTriggers, Constraints20/56
  21. 21. How to isolate db tests?Run tests in one transaction#bgoug2013Makes them repeatable and independentWhen one transaction is not an option - clean upafter tests21/56
  22. 22. How to isolate db tests (2)?Dedicated database#bgoug2013One db per contributorSeparate schemasShared Dev db may work tooAs a rule - avoid running tests on top of production22/56
  23. 23. Other Tips#bgoug2013Make tests self-sufficientDont count on the order of testsPrepare everything you need for the test in its set-up23/56
  24. 24. #bgoug2013DbFit24/56
  25. 25. What is DbFit?#bgoug2013Initially created by Gojko Adzic:Enables manipulating database objects and definingtests in tabular formOpen source https://github.com/benilovj/dbfitto enable efficient database testingmotivate database developers to use anautomated testing framework25/56
  26. 26. DbFit, FIT and FitNesse#bgoug2013DbFit is based on FIT+FitNesse+DB Fixtures whichenable FIT/Fitnesse tests to execute directly against adatabase.FIT is Acceptance testing frameworkFitNesse is Wiki-web based front-end for FITcustomer orientedtests are described as tables26/56
  27. 27. FitNesse architecture#bgoug2013source: Fittnesse User Guilde - One Minute Description27/56
  28. 28. What is DbFit Fixture?#bgoug2013A fixture is interface between:In general there is 1:1 mapping between Fit tableand fixturethe test instrumentation (Fit framework),test cases (Fit tables),and the system under test (e.g. a databasestored procedure)28/56
  29. 29. Why DbFit?#bgoug2013Easy to use (even for non-technical people)Provides all the plumbing:Runs inside FitNesse - already integrated with lots ofother tools/librariesTests expressed and managed as tablesWeb-Wiki front-endTransaction managementFeatures based on meta-dataParameter mapping29/56
  30. 30. What is Wiki?#bgoug2013The simplest online database that could possiblywork. - Ward CunninghamAllows users to freely create and edit Web pagecontent using any Web browserA group communication mechanismsEncourages democratic use of the Web andpromotes content composition by nontechnicaluserssource: http://wiki.org/wiki.cgi?WhatIsWiki30/56
  31. 31. Fitnesse Wiki#bgoug2013Hierarchies - SubWiki, Test SuitesPage types - Suite, Test, StaticSome special pages:http://fitnesse.org/FitNesse.UserGuidePageHeader, PageFooterSetUp, TearDown, SuiteSetUp, SuiteTearDownInherited recurively by default; can be overriden31/56
  32. 32. A Unit test with DbFit#bgoug2013Set up the input data (arrange).Execute a function or procedure (act).Run a query and compare actual vs expected data(assert).1.2.3.32/56
  33. 33. Basic commands of DbFit#bgoug2013QueryInsertUpdateExecute ProcedureExecute33/56
  34. 34. Advanced features#bgoug2013Inspect queries, tables, procedures to auto-generatetest tables and regression testsStore and compare queriesStandalone mode for full control34/56
  35. 35. Getting started#bgoug2013Needs Java to runDownload: http://benilovj.github.io/dbfitUnzipCopy Oracle JDBC driver (ojdbc6.jar) to lib subfolderRun the startup script (startFitnesse.sh orstartFitnesse.bat)Access via web browser - http://localhost:80851.2.3.4.5.6.35/56
  36. 36. Connecting to the databaseInline configuration:Using properties file:#bgoug2013!|Connect|localhost:1521|username|password|dbname|!|ConnectUsingFile|DBConnection.properties|service=localhost:1521username=usernamepassword=passworddatabase=dbname36/56
  37. 37. Query#bgoug2013!|insert|testtbl||n |name ||1 |NAME1 ||3 |NAME3 ||2 |NAME2 |!|query|select*fromtesttbl||n |name ||1 |NAME1 ||3 |NAME3 ||2 |NAME2 |37/56
  38. 38. Ordered Query#bgoug2013!|OrderedQuery|select*fromtesttblorderbyn||n |name? ||1 |NAME1 ||3 |NAME3 ||2 |NAME2 |38/56
  39. 39. Insert#bgoug2013!|insert|testtbl||n |name ||1 |NAME1 ||3 |NAME3 ||2 |NAME2 |39/56
  40. 40. Execute Procedure#bgoug2013!2Noparameters!|ExecuteProcedure|do_stuff|!2Functions-returnvalueswith"?"!|ExecuteProcedure|zlpad_notrunc ||p_str |p_padded_len|? ||12 |5 |00012|!2OUTparameters-"?"suffix!|ExecuteProcedure|split_name||p_fullname|p_first_name?|p_last_name?||MikeyMouse|Mickey |Mouse |!2INOUTparameters-specifytwice!|ExecuteProcedure|make_double||x|x?||3|6|40/56
  41. 41. Expect exception#bgoug2013!2ExpectORA-20013!|Executeprocedureexpectexception|set_age|20013||p_age ||-5 |41/56
  42. 42. Parameters and fixture symbols#bgoug2013set parameterto set parameter directly>>paramname- store a value<<paramname- read the value!|setparameter|ONE|1|!|query|selectsysdatemytimefromdual||mytime? ||>>current_time |!|query|selectcount(*)cntfromdualwheresysdate>=:current_time||cnt ||<<ONE |42/56
  43. 43. Store Query#bgoug2013!|StoreQuery|select1nfromdualunionselect2nfromdual|firsttable|!|query|<<firsttable||n ||1 ||2 |43/56
  44. 44. Compare Stored Queries#bgoug2013!|insert|testtbl||n |name ||1 |NAME1 ||3 |NAME3 ||2 |NAME2 ||StoreQuery|select*fromtesttbl|fromtable||StoreQuery|!-select1n,name1namefromdual|fromdual||comparestoredqueries|fromtable|fromdual||name |n? |Use ? suffix for non-key columns44/56
  45. 45. Working Modes of fixtures#bgoug2013FlowStandalone45/56
  46. 46. Working Modes of fixtures (2)Flow mode#bgoug2013A DatabaseTest fixture controls the whole page andcoordinates testingAutomatic rollback at the end (manual commit orrollback is still possible)Better isolationSome additional features such as inspections ofstored procedure error resultsOracleTest, MysqlTest, DerbyTest, DB2Test, ...46/56
  47. 47. Working Modes of fixtures (3)Standalone mode#bgoug2013We are responsible for transaction managementEnables more control over the database testingprocessAllows using other individual fixturesWe can supply our own database connection tomake sure that (Java) integration tests are running inthe same transaction47/56
  48. 48. Connecting to databse#bgoug2013!3InFlowmode!|dbfit.OracleTest|!|Connect|ourhost:1521|dbusername|dbpassword|mydb|#Alternatively-TNSdescriptorcanbeused:#!|Connect|(DESCRIPTION=(ADDRESS=...))|!3InStandalonemode|importfixture||dbfit.fixture|!|DatabaseEnvironment|oracle||connect|localhost:1521|dbusername|dbpassword|mydb|48/56
  49. 49. Integration test with SQL*Loader#bgoug2013Compile CommandLineFixture (by Bob Martin)Use it to run a shell script!3LoadsomedatawithOracleSQL*Loader|com.objectmentor.fixtures.CommandLineFixture ||command|${PROJECT_ROOT}/loaderdemo/load_employee.sh|!|Query|select*fromemployee ||id |name?|dept? |salary?||100 |Thomas|Sales |5000 ||200 |Jason|Technology|5500 ||300 |Mayla|Technology|7000 ||400 |Nisha|Marketing|9500 ||500 |Randy|Technology|6000 ||501 |Ritu |Accounting|5400 |49/56
  50. 50. Automating tests execution#bgoug2013Running tests from command lineRun test or suite as RESTful servicehttp://fitnesse.org/FitNesse.UserGuide.RestfulServicesJUnitjava-jarfitnesse-standalone.jar-d"${TESTS_DIR}"-c"BgougDemoSuite?suite&format=text"50/56
  51. 51. How tests are stored?#bgoug2013Simple text filescontent.txt - test definition and other Wiki contentproperties.xml - metadata (Test, Sute)Easy to put under version control51/56
  52. 52. #bgoug2013Demo52/56
  53. 53. Summary#bgoug2013Changes of database code and schema are oftenrelatively hardThis makes the systems considered legacyTDD stimulates designing cleaner and easier tochange codeDevelopment of RDBMS artefacts is lagging when itcomes to engineering practices and toolsDbFit can help53/56
  54. 54. Resources#bgoug2013http://benilovj.github.io/dbfit - with links to:https://github.com/javornikolov/tdd-with-dbfit-bgoug-201305http://gojko.net/2007/11/20/fighting-the-monsterhttp://www.agiledata.org/essays/tdd.html, http://www.agiledata.orgTest Driven Development: By Example, Kent BeckRefactoring Databases - Evolutionary Database Design, Scott W. Ambler, Pramodkumar J. Sadalagedownload DbFitdocs, getting started informationmailing list - dont hesitate to participate and ask questionscode repository at github - reports for problems, suggestions and contributions are welcome54/56
  55. 55. Thank You!nikolov dot javor at gmail
  56. 56. Q&A#bgoug2013 56/56
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×