Upload
yavor-nikolov
View
5.950
Download
5
Tags:
Embed Size (px)
DESCRIPTION
Test-Driven Development with DbFit and Oracle database, BGOUG Conference, 2013-05-18
Citation preview
TDD with DbFit and OracleWriting readable, easy to maintain unit and integration tests for
database code
Yavor Nikolov
BGOUG Conference, 2013-05-18
Agenda
#bgoug2013
Concepts (Testing, Test-Driven Development)
DbFit
Demo
2/56
What is a legacy system?
#bgoug2013
You spot an obvious design problem
know how to improve that,
but the thought about consequences gives you astomach ache.
source: Gojko Adzic, "Fighting the monster"
3/56
#bgoug2013
source: http://lisacrispin.com/2011/11/08/using-the-agile-testing-quadrants
4/56
Why (automated) testing?
#bgoug2013
Makes application change easier
Safety net - provides confidence/removes fear
Documentation
Help to localize where exactly a defect is located
Reduce the chance of new bugs
Automation enables earlier feedback, saves time,helps focusing on solving the main problem. (Noteverything is feasible to automate)
5/56
Test Fixture
#bgoug2013
All the things we need to have in place in order torun a test and expect a particular outcome
The test context
6/56
System Under Test (SUT)
The system that is being tested
#bgoug2013 7/56
Test execution cycle
#bgoug2013
Arrange (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
Unit test
#bgoug2013
Tests small individual unit (module,procedure/function)
In isolation (no interaction with other units)
Should run quickly (otherwise people won't runthem)
9/56
Integration test
#bgoug2013
Tests several modules as a group
Slower than unit tests (usually)
10/56
Acceptance test
#bgoug2013
Conducted to determine whether or not a systemsatisfies its acceptance criteria
and to enable the customer to determine whether ornot to accept the system.
At least modeled and possibly even written by thecustomer
End-to-end (slower than Integration & Unit tests)
11/56
Regression tests(Regress vs progress)
Performed to make sure that previously workingfunctionality still works after changes elsewhere inthe system
#bgoug2013 12/56
Refactor
Is the the process of changing a system in such a waythat
Doing refactoring without tests is unsafe
#bgoug2013
doesn't alter external behaviour
and improves it's internal structure (design)
through small steps
13/56
TDD Cycle
#bgoug2013
source: Internet
14/56
Tests are not the main product in TDD
#bgoug2013
TDD is a design technique
The design emerges in small steps
15/56
Why test first?
#bgoug2013
Start with end in mind (think from point of view ofcaller)
This perspective helps for better design
Test coverage is useful byproduct
Greatly reduces the need of debugging
16/56
Cost of change (traditional)
#bgoug2013 17/56
Cost of change (test early)
#bgoug2013 18/56
Why testing the Database?
#bgoug2013
For lot of businesses, data held in DB are the mostvital commercial asset they have
Business critical functions rely on this data
So it makes sense to validate that data is stored andprocessed correctly
19/56
Challenges of Database testing...
#bgoug2013
Bad tools
Inherently hard to test. Isolation is difficult
Attitude ("it's not my job")
Too much boilerplate code
OO tools not directly applicable for RDBMS
Changes are persistent
Shared environment
Triggers, Constraints
20/56
How to isolate db tests?Run tests in one transaction
#bgoug2013
Makes them repeatable and independent
When one transaction is not an option - clean upafter tests
21/56
How to isolate db tests (2)?Dedicated database
#bgoug2013
One db per contributor
Separate schemas
Shared Dev db may work too
As a rule - avoid running tests on top of production
22/56
Other Tips
#bgoug2013
Make tests self-sufficient
Don't count on the order of tests
Prepare everything you need for the test in its set-up
23/56
#bgoug2013
DbFit
24/56
What is DbFit?
#bgoug2013
Initially created by Gojko Adzic:
Enables manipulating database objects and definingtests in tabular form
Open source https://github.com/benilovj/dbfit
to enable efficient database testing
motivate database developers to use anautomated testing framework
25/56
DbFit, FIT and FitNesse
#bgoug2013
DbFit is based on FIT+FitNesse+DB Fixtures whichenable FIT/Fitnesse tests to execute directly against adatabase.
FIT is Acceptance testing framework
FitNesse is Wiki-web based front-end for FIT
customer oriented
tests are described as tables
26/56
FitNesse architecture
#bgoug2013
source: Fittnesse User Guilde - One Minute Description
27/56
What is DbFit Fixture?
#bgoug2013
A fixture is interface between:
In general there is 1:1 mapping between Fit tableand fixture
the test instrumentation (Fit framework),
test cases (Fit tables),
and the system under test (e.g. a databasestored procedure)
28/56
Why DbFit?
#bgoug2013
Easy to use (even for non-technical people)
Provides all the plumbing:
Runs inside FitNesse - already integrated with lots ofother tools/libraries
Tests expressed and managed as tables
Web-Wiki front-end
Transaction management
Features based on meta-data
Parameter mapping
29/56
What is Wiki?
#bgoug2013
The simplest online database that could possiblywork. - Ward Cunningham
Allows users to freely create and edit Web pagecontent using any Web browser
A group communication mechanisms
Encourages democratic use of the Web andpromotes content composition by nontechnicalusers
source: http://wiki.org/wiki.cgi?WhatIsWiki
30/56
Fitnesse Wiki
#bgoug2013
Hierarchies - SubWiki, Test Suites
Page types - Suite, Test, Static
Some special pages:
http://fitnesse.org/FitNesse.UserGuide
PageHeader, PageFooter
SetUp, TearDown, SuiteSetUp, SuiteTearDown
Inherited recurively by default; can be overriden
31/56
A Unit test with DbFit
#bgoug2013
Set 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
Basic commands of DbFit
#bgoug2013
Query
Insert
Update
Execute Procedure
Execute
33/56
Advanced features
#bgoug2013
Inspect queries, tables, procedures to auto-generatetest tables and regression tests
Store and compare queries
Standalone mode for full control
34/56
Getting started
#bgoug2013
Needs Java to run
Download: http://benilovj.github.io/dbfit
Unzip
Copy Oracle JDBC driver (ojdbc6.jar) to lib subfolder
Run the startup script (startFitnesse.sh orstartFitnesse.bat)
Access via web browser - http://localhost:8085
1.
2.
3.
4.
5.
6.
35/56
Connecting to the database
Inline configuration:
Using properties file:
#bgoug2013
!|Connect|localhost:1521|username|password|dbname|
!|ConnectUsingFile|DBConnection.properties|
service=localhost:1521username=usernamepassword=passworddatabase=dbname
36/56
Query
#bgoug2013
!|insert|testtbl||n |name ||1 |NAME1 ||3 |NAME3 ||2 |NAME2 |
!|query|select * from testtbl||n |name ||1 |NAME1 ||3 |NAME3 ||2 |NAME2 |
37/56
Ordered Query
#bgoug2013
!|Ordered Query|select * from testtbl order by n||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
!2 No parameters!|Execute Procedure|do_stuff|
!2 Functions - return values with "?"!|Execute Procedure|zlpad_notrunc ||p_str |p_padded_len|? ||'12' |5 |'00012' |
!2 OUT parameters - "?" suffix!|Execute Procedure|split_name||p_fullname |p_first_name?|p_last_name?||Mikey Mouse|Mickey |Mouse |
!2 IN OUT parameters - specify twice!|Execute Procedure|make_double||x|x?||3|6 |
40/56
Expect exception
#bgoug2013
!2 Expect ORA-20013!|Execute procedure expect exception|set_age|20013||p_age ||-5 |
41/56
Parameters and fixture symbols
#bgoug2013
set parameter to set parameter directly
>>paramname - store a value
<<paramname - read the value
!|set parameter|ONE|1|
!|query|select sysdate mytime from dual||mytime? ||>>current_time |
!|query|select count(*) cnt from dual where sysdate >= :current_time||cnt ||<<ONE |
42/56
Store Query
#bgoug2013
!|Store Query|select 1 n from dual union select 2 n from dual|firsttable|
!|query|<<firsttable||n ||1 ||2 |
43/56
Compare Stored Queries
#bgoug2013
!|insert|testtbl||n |name ||1 |NAME1 ||3 |NAME3 ||2 |NAME2 |
|Store Query|select * from testtbl|fromtable|
|Store Query|!- select 1 n, 'name1' name from dual|fromdual|
|compare stored queries|fromtable|fromdual||name |n? |
Use ? suffix for non-key columns
44/56
Working Modes of fixtures
#bgoug2013
Flow
Standalone
45/56
Working Modes of fixtures (2)Flow mode
#bgoug2013
A DatabaseTest fixture controls the whole page andcoordinates testing
Automatic rollback at the end (manual commit orrollback is still possible)
Better isolation
Some additional features such as inspections ofstored procedure error results
OracleTest, MysqlTest, DerbyTest, DB2Test, ...
46/56
Working Modes of fixtures (3)Standalone mode
#bgoug2013
We are responsible for transaction management
Enables more control over the database testingprocess
Allows using other individual fixtures
We can supply our own database connection tomake sure that (Java) integration tests are running inthe same transaction
47/56
Connecting to databse
#bgoug2013
!3 In Flow mode!|dbfit.OracleTest|!|Connect|ourhost:1521|dbusername|dbpassword|mydb|
# Alternatively - TNS descriptor can be used:#!|Connect|(DESCRIPTION=(ADDRESS=...))|
!3 In Standalone mode|import fixture||dbfit.fixture|!|DatabaseEnvironment|oracle||connect|localhost:1521|dbusername|dbpassword|mydb|
48/56
Integration test with SQL*Loader
#bgoug2013
Compile CommandLineFixture (by Bob Martin)
Use it to run a shell script
!3 Load some data with Oracle SQL*Loader|com.objectmentor.fixtures.CommandLineFixture ||command|${PROJECT_ROOT}/loaderdemo/load_employee.sh|
!|Query|select * from employee ||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
Automating tests execution
#bgoug2013
Running tests from command line
Run test or suite as RESTful servicehttp://fitnesse.org/FitNesse.UserGuide.RestfulServices
JUnit
java -jar fitnesse-standalone.jar \ -d "${TESTS_DIR}" \ -c "BgougDemoSuite?suite&format=text"
50/56
How tests are stored?
#bgoug2013
Simple text files
content.txt - test definition and other Wiki content
properties.xml - metadata (Test, Sute)
Easy to put under version control
51/56
#bgoug2013
Demo
52/56
Summary
#bgoug2013
Changes of database code and schema are oftenrelatively hard
This makes the systems considered legacy
TDD stimulates designing cleaner and easier tochange code
Development of RDBMS artefacts is lagging when itcomes to engineering practices and tools
DbFit can help
53/56
Resources
#bgoug2013
http://benilovj.github.io/dbfit - with links to:
https://github.com/javornikolov/tdd-with-dbfit-bgoug-201305
http://gojko.net/2007/11/20/fighting-the-monster
http://www.agiledata.org/essays/tdd.html, http://www.agiledata.org
Test Driven Development: By Example, Kent Beck
Refactoring Databases - Evolutionary Database Design, Scott W. Ambler, Pramodkumar J. Sadalage
download DbFit
docs, getting started information
mailing list - don't hesitate to participate and ask questions
code repository at github - reports for problems, suggestions and contributions are welcome
54/56
Thank You!nikolov dot javor at gmail
Q&A
#bgoug2013 56/56