36
Training on Database Unit Testing with DbFit Conducted by Md. Babul Mirdha Senior Software Engineer Leads Corporation Ltd 1 Part - I

Hands on training on DbFit Part-I

Embed Size (px)

Citation preview

Page 1: Hands on training on DbFit Part-I

1

Training on

Database Unit Testing with

DbFit

Conducted byMd. Babul Mirdha

Senior Software Engineer

Leads Corporation Ltd

Part- I

Page 2: Hands on training on DbFit Part-I

2

What is dbFit?• DbFit is a Poka-Yoke device for Database Development. • DbFit is a set of FIT fixtures which enables FIT/FitNesse

tests to execute directly against a database. • This enables developers to manipulate database

objects in a relational tabular form, making database testing and management.

Page 3: Hands on training on DbFit Part-I

3

Features of DbFit

• Test Driven development for Database Development• Integration testing, Regression testing for queries

and SQL statements• Functional testing for stored procedures and

functions• Automatic transaction control and various short-cuts

to 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).

Page 4: Hands on training on DbFit Part-I

Dbfit Picture (How DbFit works)

Fit Table

DbFit Fixtures

(i , o)

DataBase

i

o’Db Developer

o ≠ o’

Test Runner

Output Table

i= inputo = expected outputo’ = actual output

User Story & Test ScriptUsing Wiki

Customer/Analyst

4

Page 5: Hands on training on DbFit Part-I

5

Installing DbFit

• Install Java JRE 5 or later • Install Microsoft's .NET Framework runtime 2 or later• To use DbFit fixtures in your tests, download the

dbfit-dotnet-binaries package from http://sourceforge.net/projects/dbfit.

• To install dbFit just unpack dbfitcomplete-XXX.zip somewhere on your disk, and run startFitnesse.bat (or startFitnesse.sh on Linux)

Page 6: Hands on training on DbFit Part-I

6

Working with Wiki

Page 7: Hands on training on DbFit Part-I

7

What is a Wiki?

A minimalist (simple) Content Management System.– Everyone can change every page.– Changes are visible immediately.– There are abbreviations for often used HTML

tags.

• Wiki will be used to write Test Script

Wikipedia …

Page 8: Hands on training on DbFit Part-I

8

WikiWord & WikiPage• Wiki word is Pascal Case: an alternating pattern of upper and

lower case letters. • A wiki word is a string of two more more capital letters with

lower case letters or numbers between them.Word WikiWord?

BobMartin Yes

SalesReport1972 Yes

USAforEver No

Usa1776 No

RcM Yes

ItDoesNotMatter 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.

Page 9: Hands on training on DbFit Part-I

9

Character FormattingFormat Name Syntax Remark

Comment #text Can also be used to comment-out markup

Italic ‘’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]]

Page 10: Hands on training on DbFit Part-I

10

Line & Block FormattingFormat Name Syntax Remark

Horizontal line ---- Thinnest

Note !note text

Headings

!1 largest heading text!2 middle heading text!3 smaller heading textand so on up to !6

Centered !c text

Formatted "as is" {{{text}}} note: causes extra line afterward

"As-is"/escaping !-text-! Can also be used to insert HTML tags directly

Alias Link [[TEXT][PagePATH]]

Page 11: Hands on training on DbFit Part-I

11

Collapsible BlockFormat Name Syntax Remark

Expanded!* [title]   multi-line wiki text *!

Collapsed !*> [title]   multi-line wiki text *!

Invisible !*< [title]   multi-line wiki text *!

useful for hidding define's, etc.

Page 12: Hands on training on DbFit Part-I

12

Links & References Page links

Format Name Syntax Remark

from root .RootPage[.ChildPage]

Sibling SameLevelPage[.ChildPage]

child or symbolic >ChildPage[.ChildPage]

Page 13: Hands on training on DbFit Part-I

13

Writing Fit table using Wiki

!| RowFixtureTest |

|name |post code |

|John Smith |SW4 66Z |

|Michael Jordan | NE1 8AT |

Fig: Wiki Fig: Fit table

NB: The exclamation mark before the first cell in the table header tells FitNesse not to convert it to a page link

Page 14: Hands on training on DbFit Part-I

14

Demo Wiki Page

Page 15: Hands on training on DbFit Part-I

15

Working With Fixtures

Page 16: Hands on training on DbFit Part-I

16

What is DbFit Fixture?

• A fixture is an interface between the test instrumentation (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 mapping between

a Fit table and fixture.

DbFit Fixtures

DataBase(System Under Test)

Test Runner(Fit/Fitnesse)Test cases

Page 17: Hands on training on DbFit Part-I

17

Demo on Simple Example!define COMMAND_PATTERN {%m -r fitnesse.fitserver.FitServer,dotnet2\fit.dll %p}!define TEST_RUNNER {dotnet2\Runner.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|

Page 18: Hands on training on DbFit Part-I

18

Query Fixture

• Query tests lists of objects using SQL Query results. • Specify query as the first fixture parameter, after

the Query command. • The second table row contains column names, and

all subsequent rows contain data for the expected results.

!|query| SELECT AccountNo , AccountName, OpeningBalance FROM tb_Accounts||AccountNo|AccountName |OpeningBalance| |1001 |Imtiaz Ahamed|5000.00 |

Page 19: Hands on training on DbFit Part-I

19

Ordered Query Fixture• Query ignores row order by default. In flow mode, the Ordered Query command provides order

checking.

• 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 Study

Page 20: Hands on training on DbFit Part-I

20

Use Query Fixrue, If your sp produces a ResultSet

!|query |sp_CurrentBalance '1001'||AccountNo|Name |Amount| |1001| Imtiaz Ahamed |4000.00|

Page 21: Hands on training on DbFit Part-I

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|

Page 22: Hands on training on DbFit Part-I

22

Store Query Fixture

• Store Query reads out query results and stores them into a Fixture symbol for later use.

• Specify the full query as the first argument and the symbol name as the second argument (without >>).

• You can then use this stored result set as a parameter of the Query command later:

!|Store Query|select n from ( select 1 as n union select 2 ) x|firsttable|

!|query|<<firsttable| |n| |1||2|

Page 23: Hands on training on DbFit Part-I

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 n union select 3 union select 2) x|fromdual|

!|compare stored queries|fromtable|fromdual| |name|n?|

Self Study

Page 24: Hands on training on DbFit Part-I

24

Insert Fixture• Insert builds an insert command from the parameters in a data table (and

executes 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 contain data 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|

Page 25: Hands on training on DbFit Part-I

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|

Page 26: Hands on training on DbFit Part-I

26

Update

• Update allows you to quickly script data updates.

• Columns ending with = are used to update records (cell specifies new data value).

• Columns without = on the end are used to select rows (cell specifies expected column value for the select part of update command).

Self Study

Page 27: Hands on training on DbFit Part-I

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|

Page 28: Hands on training on DbFit Part-I

28

Execute Fixture• Execute executes any SQL statement. • You can use query parameters in the DB-specific syntax (eg. @paramname for

SQLServer 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|

Page 29: Hands on training on DbFit Part-I

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|

Page 30: Hands on training on DbFit Part-I

30

ExecuteProcedure

• It executes a stored procedure or function for each row of data 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|

Page 31: Hands on training on DbFit Part-I

31

ExecuteProcedure (Cont)

• !3 If there are no parameters, Execute Procedure needs just one row

• !|Execute Procedure|MakeUser|

• |query|select * from users|• |name|username|• |user1|fromproc|

Page 32: Hands on training on DbFit Part-I

32

ExecuteProcedure (Cont)• !3 Stored functions are treated like procedures - just put ? in the result column

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 to put it there)

• !|Execute Procedure|ConcatenateF|• |second string|?|first string|• |World|Hello World|Hello|• |Prefect|Ford Prefect|Ford|

Page 33: Hands on training on DbFit Part-I

33

Inspect Fixture• Inspect extracts meta-data information from the database, and print it

out in 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|

Page 34: Hands on training on DbFit Part-I

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/tool

s.php?fitnesse

• http://working-thought.blogspot.com/2008/10/connecting-to-oracle-grid-with-dbfit.html

Page 35: Hands on training on DbFit Part-I

35

Thank You!

Page 36: Hands on training on DbFit Part-I

36

Q & A