all groups > c# > april 2008 >
You're in the c# group:
Writing a DAL with TDD
c#:
I've finally gotton board with TDD (test driven development) and wow is it effective! I went from sceptic to True Believer with my first effort. My question: According to the various books and articles I have read about TDD, a good unit test does not rely on the database or other such external/environmental conditions. More generally, a good unit test is atomic and makes as few assumptions about its runtime environment as possible. But what about developing unit tests for a DAL - whose purpose in life is to interact with a database? Your thoughts and opinions are appreciated.
[quoted text, click to view] On Apr 23, 11:16 am, "Cramer" <A...@B.com> wrote: > I've finally gotton board with TDD (test driven development) and wow is it > effective! I went from sceptic to True Believer with my first effort. > > My question: According to the various books and articles I have read about > TDD, a good unit test does not rely on the database or other such > external/environmental conditions. More generally, a good unit test is > atomic and makes as few assumptions about its runtime environment as > possible. But what about developing unit tests for a DAL - whose purpose in > life is to interact with a database? > > Your thoughts and opinions are appreciated.
I would test the behavior in the DAL. I m not very well versed with DB programming but, as far as i know DAL abstracts a bunch of SQL statements or calls to stored procedures in the DB. So I would test for setting up the adapter object that actually tunnels the call to the DB. The rationale is that if you have validated the behavior that sets up the adapter object, then it will work correctly when you connect a actual DB. If the Adapter object cannot be mocked direclty, then I would write a simple wrapper around
[quoted text, click to view] "Cramer" <A@B.com> wrote in message news:eZGYBUVpIHA.4904@TK2MSFTNGP03.phx.gbl... > I've finally gotton board with TDD (test driven development) and wow is it > effective! I went from sceptic to True Believer with my first effort. > > My question: According to the various books and articles I have read about > TDD, a good unit test does not rely on the database or other such > external/environmental conditions. More generally, a good unit test is > atomic and makes as few assumptions about its runtime environment as > possible. But what about developing unit tests for a DAL - whose purpose > in life is to interact with a database? > > Your thoughts and opinions are appreciated.
You could do the unit test of the DAL through the interfaces of the Business layer that interacts with the DAL. This link has shows about TDD and unit testing of a TDD solution. http://www.polymorphicpodcast.com/ There is also this which can show you how to develop your solutions, and how you can test through the interface. You should be able to develop a test harness for the interface or use something line Nunit. MODEL-VIEW-PRESENTER http://www.polymorphicpodcast.com/ click 'Shows' click 'Design Patterns Bootcamp: Model View * Patterns* view parts 1-5
[quoted text, click to view] Cramer wrote: > I've finally gotton board with TDD (test driven development) and wow is it > effective! I went from sceptic to True Believer with my first effort. > > My question: According to the various books and articles I have read about > TDD, a good unit test does not rely on the database or other such > external/environmental conditions. More generally, a good unit test is > atomic and makes as few assumptions about its runtime environment as > possible. But what about developing unit tests for a DAL - whose purpose in > life is to interact with a database? > > Your thoughts and opinions are appreciated. > > >
Similar to what Ben said, We used to have a "test" database (or one of each, mssql, mysql, oracle) that each time would be cleaned out and then repopulated by each test. It might not be very scaleable but worked well for what we were doing at the time which encompassed 500+ tests running in around 20 seconds.
[quoted text, click to view] Cramer wrote: > I've finally gotton board with TDD (test driven development) and wow > is it effective! I went from sceptic to True Believer with my first > effort. > My question: According to the various books and articles I have read > about TDD, a good unit test does not rely on the database or other > such external/environmental conditions. More generally, a good unit > test is atomic and makes as few assumptions about its runtime > environment as possible. But what about developing unit tests for a > DAL - whose purpose in life is to interact with a database?
The unit test needs to create a new database in a known state, open it using the DAL under test, verify the results, clean up the database. [quoted text, click to view] > > Your thoughts and opinions are appreciated.
[quoted text, click to view] On Apr 24, 11:52 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote: > Cramer wrote: > > I've finally gotton board with TDD (test driven development) and wow > > is it effective! I went from sceptic to True Believer with my first > > effort. > > My question: According to the various books and articles I have read > > about TDD, a good unit test does not rely on the database or other > > such external/environmental conditions. More generally, a good unit > > test is atomic and makes as few assumptions about its runtime > > environment as possible. But what about developing unit tests for a > > DAL - whose purpose in life is to interact with a database? > > The unit test needs to create a new database in a known state, open it using > the DAL under test, verify the results, clean up the database. > > > > > Your thoughts and opinions are appreciated.
Ben, Then it wont be a unit test in the TDD sense. In fact i would argue that its not unit test at all and more of Integration testing. The point is your tests would be "lazy" and would not help you drive the
[quoted text, click to view] On Apr 24, 6:07 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote: > mgsram wrote: > > On Apr 24, 11:52 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote: > >> Cramer wrote: > >>> I've finally gotton board with TDD (test driven development) and wow > >>> is it effective! I went from sceptic to True Believer with my first > >>> effort. > >>> My question: According to the various books and articles I have read > >>> about TDD, a good unit test does not rely on the database or other > >>> such external/environmental conditions. More generally, a good unit > >>> test is atomic and makes as few assumptions about its runtime > >>> environment as possible. But what about developing unit tests for a > >>> DAL - whose purpose in life is to interact with a database? > > >> The unit test needs to create a new database in a known state, open > >> it using the DAL under test, verify the results, clean up the > >> database. > > >>> Your thoughts and opinions are appreciated. > > > Ben, > > Then it wont be a unit test in the TDD sense. In fact i would argue > > that its not unit test at all and more of Integration testing. The > > point is your tests would be "lazy" and would not help you drive the > > design of DAL which is what the goal is. > > Well... ok. This might no be considered a unit test anymore, it is quite > heavy. > > But there's no reason that you couldn't still develop the test before the > code. You could even use the test to drive database layout (this might not > be very effective at normalization though).
Absolutely,
[quoted text, click to view] mgsram wrote: > On Apr 24, 11:52 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote: >> Cramer wrote: >>> I've finally gotton board with TDD (test driven development) and wow >>> is it effective! I went from sceptic to True Believer with my first >>> effort. >>> My question: According to the various books and articles I have read >>> about TDD, a good unit test does not rely on the database or other >>> such external/environmental conditions. More generally, a good unit >>> test is atomic and makes as few assumptions about its runtime >>> environment as possible. But what about developing unit tests for a >>> DAL - whose purpose in life is to interact with a database? >> >> The unit test needs to create a new database in a known state, open >> it using the DAL under test, verify the results, clean up the >> database. >> >> >> >>> Your thoughts and opinions are appreciated. > > Ben, > Then it wont be a unit test in the TDD sense. In fact i would argue > that its not unit test at all and more of Integration testing. The > point is your tests would be "lazy" and would not help you drive the > design of DAL which is what the goal is.
Well... ok. This might no be considered a unit test anymore, it is quite heavy. But there's no reason that you couldn't still develop the test before the code. You could even use the test to drive database layout (this might not be very effective at normalization though).
[quoted text, click to view] > Cramer wrote: >> I've finally gotton board with TDD (test driven development) and wow is >> it effective! I went from sceptic to True Believer with my first effort. >> >> My question: According to the various books and articles I have read >> about TDD, a good unit test does not rely on the database or other such >> external/environmental conditions. More generally, a good unit test is >> atomic and makes as few assumptions about its runtime environment as >> possible. But what about developing unit tests for a DAL - whose purpose >> in life is to interact with a database? >> >> Your thoughts and opinions are appreciated. >> >> >> > Similar to what Ben said, > We used to have a "test" database (or one of each, mssql, mysql, oracle) > that each time would be cleaned out and then repopulated by each test. > It might not be very scaleable but worked well for what we were doing at > the time which encompassed 500+ tests running in around 20 seconds.
Thank you John, Along the lines of what you did, I have heard unit tests (against a DB) doing something like this: (1) open a transaction in the test fixture setup; (2) run the tests; (3) roll back the transaction in the test fixture teardown. -Cramer
Don't see what you're looking for? Try a search.
|
|
|