Hands on training on DbFit Part-I

Preview:

Citation preview

1

Training on

Database Unit Testing with

DbFit

Conducted byMd. Babul Mirdha

Senior Software Engineer

Leads Corporation Ltd

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.

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

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

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)

6

Working with Wiki

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 …

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.

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

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

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.

12

Links & References Page links

Format Name Syntax Remark

from root .RootPage[.ChildPage]

Sibling SameLevelPage[.ChildPage]

child or symbolic >ChildPage[.ChildPage]

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

14

Demo Wiki Page

15

Working With Fixtures

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

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|

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 |

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

20

Use Query Fixrue, If your sp produces a ResultSet

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

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|

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|

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

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|

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|

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

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|

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|

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|

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|

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|

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|

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|

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

35

Thank You!

36

Q & A

Recommended