Hands on training on DbFit Part-I


Published on

  • Be the first to comment

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

No notes for slide
  • Create PROCEDURE [dbo].[ConcatenateStrings](@firststringvarchar(100),@secondstringvarchar(100), @concatenated varchar(200) out)ASBEGINset @concatenated= @firststring+' '+@secondstringENDCreate PROCEDURE [dbo].[concat](@firststringvarchar(100),@secondstringvarchar(100))ASBEGINselect @firststring+' '+@secondstring as fs;END
  • create table testtbl (n int, name varchar(100))create function [dbo].[concat](@firststringvarchar(100),@secondstringvarchar(100))returns varchar(500)ASBEGINreturn @firststring+''+@secondstringEND
  • Create table Test_DBFit(name varchar(50), luckyNumberint)
  • Create table Test_DBFit(name varchar(50), luckyNumberint)
  • ALTER PROCEDURE [dbo].[ConcatenateStrings](@firststringvarchar(100),@secondstringvarchar(100), @concatenated varchar(200) out)ASBEGINset @concatenated= @firststring+' '+@secondstringENDcreate procedure Multiply (@factor int, @valint out)as beginset @val =@val*@factor;end
  • Hands on training on DbFit Part-I

    1. 1. TrainingonDatabase Unit TestingwithDbFitConducted byMd. Babul MirdhaSenior Software EngineerLeads Corporation Ltd1Part- I
    2. 2. What is dbFit?• DbFit is a Poka-Yoke device for Database Development.• DbFit is a set of FIT fixtures which enables FIT/FitNessetests to execute directly against a database.• This enables developers to manipulate databaseobjects in a relational tabular form, making databasetesting and management.2
    3. 3. Features of DbFit• Test Driven development for Database Development• Integration testing, Regression testing for queriesand SQL statements• Functional testing for stored procedures andfunctions• Automatic transaction control and various short-cutsto make writing test scripts easier and more efficient• Support for– DB2, Oracle and SQL Server (java and .net versions)– MySql 5 and Derby (only java).3
    4. 4. Dbfit Picture (How DbFit works)Fit TableDbFit Fixtures(i , o)DataBaseio’Db Developero ≠ o’Test RunnerOutput Tablei= inputo = expected outputo’ = actual outputUser Story & Test ScriptUsing WikiCustomer/Analyst4
    5. 5. Installing DbFit• Install Java JRE 5 or later• Install Microsofts .NET Framework runtime 2 or later• To use DbFit fixtures in your tests, download thedbfit-dotnet-binaries package fromhttp://sourceforge.net/projects/dbfit.• To install dbFit just unpack dbfitcomplete-XXX.zipsomewhere on your disk, and run startFitnesse.bat(or startFitnesse.sh on Linux)5
    6. 6. Working with Wiki6
    7. 7. What is a Wiki?A minimalist (simple) Content ManagementSystem.– Everyone can change every page.– Changes are visible immediately.– There are abbreviations for often used HTMLtags.• Wiki will be used to write Test ScriptWikipedia …7
    8. 8. WikiWord & WikiPage• Wiki word is Pascal Case: an alternating pattern of upper andlower case letters.• A wiki word is a string of two more more capital letters withlower case letters or numbers between them.Word WikiWord?BobMartin YesSalesReport1972 YesUSAforEver NoUsa1776 NoRcM YesItDoesNotMatter Yes• A wiki word helps to create & Link a Wiki Page• A WikiWord like Text in a wiki page is considered as a page link.8
    9. 9. Character FormattingFormat Name Syntax RemarkComment #text Can also be used to comment-out markupItalic ‘’text’’Bold ‘’’text’’’Style !style_(text) The style of text will be the CSS class <someStyle>Strike-through --text--"As-is"/escaping !-text -! Can also be used to insert HTML tags directly"As-is" !<text>! Will escape HTML tags so they display in plain text.Alias Link [[TEXT][PagePATH]] 9
    10. 10. Line & Block FormattingFormat Name Syntax RemarkHorizontal line ---- ThinnestNote !note textHeadings!1 largest heading text!2 middle heading text!3 smaller heading textand so on up to !6Centered !c textFormatted "asis"{{{text}}} note: causes extra line afterward"As-is"/escaping !-text-! Can also be used to insert HTML tags directlyAlias Link [[TEXT][PagePATH]]10
    11. 11. Collapsible BlockFormat Name Syntax RemarkExpanded!* [title]multi-line wiki text*!Collapsed !*> [title]multi-line wiki text*!Invisible !*< [title]multi-line wiki text*!useful for hidding defines, etc.11
    12. 12. Links & ReferencesPage linksFormat Name Syntax Remarkfrom root.RootPage[.ChildPage]SiblingSameLevelPage[.ChildPage]child orsymbolic>ChildPage[.ChildPage]12
    13. 13. Writing Fit table using Wiki!| RowFixtureTest ||name |post code ||John Smith |SW4 66Z ||Michael Jordan | NE1 8AT |Fig: Wiki Fig: Fit tableNB: The exclamation mark before the first cell in the table header tells FitNesse not toconvert it to a page link13
    14. 14. DemoWiki Page14
    15. 15. Working With Fixtures15
    16. 16. What is DbFit Fixture?• A fixture is an interface between the testinstrumentation (the Fit framework), test cases(Fit tables), and the system under test.• Fixtures are procedures/functions/classes.• In general, there is a one-to-one mappingbetween a Fit table and fixture.DbFit FixturesDataBase(System Under Test)Test Runner(Fit/Fitnesse)Test cases16
    17. 17. Demo on Simple Example!define COMMAND_PATTERN {%m -r fitnesse.fitserver.FitServer,dotnet2fit.dll %p}!define TEST_RUNNER {dotnet2Runner.exe}!path dotnet2*.dll!|dbfit.SqlServerTest|!|Connect|pcsw188|sa|leads|TestDb|!|Execute Procedure|ConcatenateStrings||firststring|secondstring|concatenated?||Hello|World|Hello World||Babu|Mirdha|Babu Mirdha|17
    18. 18. Query Fixture• Query tests lists of objects using SQL Queryresults.• Specify query as the first fixture parameter, afterthe Query command.• The second table row contains column names,and all subsequent rows contain data for theexpected results.!|query| SELECT AccountNo , AccountName, OpeningBalance FROM tb_Accounts||AccountNo|AccountName |OpeningBalance||1001 |Imtiaz Ahamed|5000.00 |18
    19. 19. Ordered Query Fixture• Query ignores row order by default. In flow mode, the Ordered Query command provides orderchecking.• This will fail because the order is wrong|Ordered Query|SELECT n FROM ( SELECT 1 as n union select 2 union select 3 ) x ||n||2||1||3|• This will pass because the order is correct|Ordered Query|SELECT n FROM ( SELECT 1 as n union select 2 union select 3 ) x||n||1||2||3|Self Study19
    20. 20. Use Query Fixrue, If your sp produces a ResultSet!|query |sp_CurrentBalance 1001||AccountNo|Name |Amount||1001| Imtiaz Ahamed |4000.00|20
    21. 21. Fixture Symbol• Symbols are global variables that can be accessed using a simple syntax.• Symbols are stored in a static collection inside the Fixture class• To store a value of an output column into a symbol named player, write>>player into the cell.• To read a symbol value, and store it into an input column, use <<player.• Think of << and >> as arrows pointing the way.|Query | select Babu as Name||Name?||>>MyName||Query | select @MyName as Name||Name?||Babu|21
    22. 22. Store Query Fixture• Store Query reads out query results and stores theminto a Fixture symbol for later use.• Specify the full query as the first argument and thesymbol name as the second argument (without >>).• You can then use this stored result set as a parameterof the Query command later:!|Store Query|select n from ( select 1 as n union select 2 ) x|firsttable|!|query|<<firsttable||n||1||2|22
    23. 23. CompareStoredQueries Fixture• CompareStoredQueries compares two previously stored query results!|insert|testtbl||n|name||1|NAME1||3|NAME3||2|NAME2|!|Store Query|select * from testtbl|fromtable|!|Store Query|select n, dbo.concat(NAME,n) as name from ( select 1 as nunion select 3 union select 2) x|fromdual|!|compare stored queries|fromtable|fromdual||name|n?|Self Study23
    24. 24. Insert Fixture• Insert builds an insert command from the parameters in a data table (andexecutes the insert once for each row of the table).• The view or table name is given as the first fixture parameter.• The second row contains column names, and all subsequent rows containdata to be inserted.|Insert|Test_DBFit||name|luckyNumber||pera|1||nuja|2||nnn|3||Query|Select * from Test_DBFit||name|lucky Number||pera|1||nuja|2| |nnn|3|24
    25. 25. Storing auto-generated values!3 Use ? to mark columns that should return values!|Insert|users||username|name|userid?||pera|Petar Detlic|>>peraid||Mika|Mitar Miric|>>mikaid||Zeka|Dusko Dugousko|>>zeka||DevNull|null|>>nllid|!3 Confirm that IDs are the same as in the database!|Ordered Query|Select * from users||username|name|userid||pera|Petar Detlic|<<peraid||Mika|Mitar Miric|<<mikaid||Zeka|Dusko Dugousko|<<zekaid||DevNull|null|<<nllid|!3 Stored values can be used in queries directly|Query|Select * from users where userid=@zekaid||username|name|userid||Zeka|Dusko Dugousko|<<zekaid|25
    26. 26. Update• Update allows you to quickly script dataupdates.• Columns ending with = are used to updaterecords (cell specifies new data value).• Columns without = on the end are used toselect rows (cell specifies expected columnvalue for the select part of update command).Self Study26
    27. 27. UpdateThis example updates the username column where the name matches arthur dent .|insert|users||name|username||arthur dent|adent||ford prefect|fpref||zaphod beeblebrox|zaphod||update|users||username=|name||adent2|arthur dent||query|select * from users||name|username||arthur dent|adent2||ford prefect|fpref||zaphod beeblebrox|zaphod|27
    28. 28. Execute Fixture• Execute executes any SQL statement.• You can use query parameters in the DB-specific syntax (eg. @paramname forSQLServer and MySQL, and :paramname for Oracle).!3 to execute statements, use the execute command!|Execute|Insert into Test_DBFit values (‘Aftab Ahmed,80)||Set parameter|name|Imtiaz Ahmed||Execute|Insert into Test_DBFit values (@name,10)|!|Query|Select * from Test_DBFit||Name|Lucky Number||Darth Maul|10||Obi Wan|80|28
    29. 29. Execute Fixture (Cont)!|Insert|Users||username|name|UserId?||pete|Peter Pan|>>user1||mike|Michael Jordan|>>user2|!|Execute| Update Users set name=Michael Jackson where userid=@user2|!|Query|Select * from users||userid|username?|name?||<<user1|pete|Peter Pan||<<user2|mike|Michael Jackson|29
    30. 30. ExecuteProcedure• It executes a stored procedure or function for each row ofdata table, binding input/output parameters to columns.• !3 execute procedure allows multiple parameters, with blanks in names• !|Execute Procedure|ConcatenateStrings|• |first string|second string|concatenated?|• |Hello|World|Hello World|• |Word|Prefect|Word Prefect|• !3 IN/OUT params need to be specified twice• |execute procedure|Multiply|• |factor|val|val?|• |5|10|50|30
    31. 31. ExecuteProcedure (Cont)• !3 If there are no parameters, ExecuteProcedure needs just one row• !|Execute Procedure|MakeUser|• |query|select * from users|• |name|username|• |user1|fromproc|31
    32. 32. ExecuteProcedure (Cont)• !3 Stored functions are treated like procedures - just put ? in the resultcolumn header• !|Execute Procedure|ConcatenateF|• |first string|second string|?|• |Hello|World|Hello World|• |Ford|Prefect|Ford Prefect|• !3 ? does not have to appear on the end (although it is a good practice toput it there)• !|Execute Procedure|ConcatenateF|• |second string|?|first string|• |World|Hello World|Hello|• |Prefect|Ford Prefect|Ford|32
    33. 33. Inspect Fixture• Inspect extracts meta-data information from the database, and print it outin a form which can be easily converted into a test.• It can work in three modes: Query, Table or Procedure.!3 Inspect Procedure prints procedure arguments to be used for Execute procedure!|Inspect Procedure|ConcatenateStrings|!3 Inspect Table prints table/view columns to be used for Insert/Update/Query Procedure!|Inspect Table|users|!3 Inspect query prints columns and data|Insert|users||name|username||david haselhoff|dhoff||arthur dent|adent|!|Inspect query|select * from users|33
    34. 34. References• Test Driven Database Development With DbFit -Marisa Seal, Gojko Adzic• Test Driven .NET Development with FitNesse-Gojko Adzic• http://fitnesse.info/dbfit• http://www.methodsandtools.com/tools/tools.php?fitnesse• http://working-thought.blogspot.com/2008/10/connecting-to-oracle-grid-with-dbfit.html34
    35. 35. Thank You!35
    36. 36. Q & A36