An Example of Test-Driven Development

  • Comments posted to this topic are about the item An Example of Test-Driven Development

    Andy Leonard, Chief Data Engineer, Enterprise Data & Analytics

  • I like this a lot, it's a good demonstration of what it means to do test driven development. Everyone uses their own techniques, I tend to favor test harnesses that write to log tables/files. But this great; a good many projects fail because nobody can thoroughly test them.

    Signature is NULL

  • Nice well demonstrated article. Still alot of confusions in my mind that WHY to through this long procedure of creating project, adding query files (if talk about the example in the article) . On can manually create a simple script file to accomplish the task.

    But, I would say that I might be too far away from the necessities of the TD3. Once again, Nice Article.

    Sometimes, winning is not an issue but trying.
    You can check my BLOG
    [font="Arial Black"]here[/font][/url][/right]

  • I enjoyed this article; a good introduction to test-driven database development, something I have been looking to implement for quite some time.

    I would be interested to see this expanded with some more practical examples, how you test complex stored procs, or triggers. Also, how do you deal with changing data from one environment to another?

    Looking forward to more in this series.


    Life: it twists and turns like a twisty turny thing

  • I completely agree with the fact of writing sql scripts for deployment including tests and using sqlcmd is a very good practice, I use to do it in a similar way.

    But what is the relation with test driven development? TDD means that you model your application in order to pass the tests you define and refactor it iteratively, that's not the same than making unit tests on your scripts.

  • Loved the article. I really need to get more into Test-Driven Development.

  • Andy: Great article: Simple and effective. If people want to delve deeper, that's what the discussion forum is for!

    Comparing notes:

    Why consider the "test first" technique? 1. I find it helps me focus on one feature at a time, which in turn helps me break down the complexity of a project into many simple parts. So it helps me manage complexity. 2. It keeps testing from becoming a neglected afterthought. I can manage the risks in quality issues up front.

    Why write reproducible tests? I feel more confident about making changes and adjustments if I can rerun the tests and get empirical evidence that things aren't broken. If I can efficiently make changes, I'm more agile.

    I develop mostly in SQL Server 2000 right now as I'm sure others do, which doesn't have the try/catch feature. However, there are tools in 2000 for raising errors: RAISERROR ('User-defined Error in usp_ThisStoredProcedure.', 16, 1). custom code or a framework can offer reporting of test success and failure: EXEC tsu_failure @FailureMessage.

    I've been using the TSQLUnit testing framework: TSQLUnit gives me the test suite logical grouping of tests with a setup and teardown for the group. Test code is written in stored procedures. The framework organizes the stored procedures through simple naming conventions. The framework gives some simple stored procedures to execute to run and manage the tests. Changes are rolled back at the conclusion of the tests.

    I find that I use the unit tests mainly to set up and use test data (insert, update and delete test records) and test the functionality of stored procedures (did the stored procedure delete the test recordset as expected?).

    I tend to not test all DDL (data definition language) statements such as whether or not certain objects exist except when necessary. For most of my DDL test needs, I've got the RedGate tool SQL Compare which compares every aspect of every object between my development and production. So, if the goal is to make sure nothing unexpected has changed, this tool does it for me in my environment where I don't require remote deployment.

    As a developer, I feel more agile when I use test-driven development techniques and I think I produce better quality code. It can be done even in SQL Server 2000.

    Bill Nicolich:
    Daily tweet of what's new and interesting: AppendNow

  • I like the idea of Test Driven Development very much, but I'm not sure this is exactly how I would want it to go. Even in this simple example, the script that is created is actually an application to create the database. Instead I think I would be trying to keep it as lean as possible. Maybe do a check and print a result - and maybe fail. I can see where the script (application) created in the example could get to be bigger than the database project itself. If that permanently solved all the problems, that might work. But for continuous development, it means that new developers have to master not only the database, but the application. They would have to figure out how to modify the application to change the database.

    Still, there is something here in "TD3", and the presentation of SS2005 features employed is interesting.

  • This strikes me as a great approach to use for high-quality, (almost) self-documenting code.

    I think the overhead of time and effort, at least when one is first implementing this technique, seriously compromises it's usefulness. That is to say, i probably don't have the patience to use this approach when i can just write stuff off-the-cuff, and i know for a fact that my boss does not have the patience.

    Thanks for the article, Andy. I just don't think i'll ever be able to really use it. :crying:

  • What a well-written article. I like how you explain things. Also, your writing style is easy to read and the touch of humor keeps me engaged.

    Alas, like others, I'm not convinced of the usefulness of this kind of approach. I'm not a lazy developer. I don't mind extra work for good value. For example, I document extensively even though it is no fun and does not directly affect the product. I just don't see the benefit of doing test driven development -- especially for database development.

    But, at least now I feel I really understand the approach and how to implement it. If one is sold on the concept of test driven development to begin with, your approach and set of scripts makes a lot of sense. I will definitely read any other articles you publish in the future on the subject. Who knows, maybe I'll be converted. Thanks.

  • Wonderful article. I followed you completely up until almost the last sentence - where you used the ambiguous term 'opposite' in the Q&A section:

    'Scripting the database will work wherever restoring a backup will work, but the opposite doesn't always hold.'

    Which logical opposite? Are you saying that scripting will not work wherever a backup will not work, or that scripting will not work wherever a backup will work, or that a backup will not work where scripting does (not) work, or ....

    And, how would you build a (syntactic) test script for that sentence anyway?

    Great job!

  • Great explanation of 'how' but could have done with a bit more info one exactly 'why' to do things like this.

    As far as I can see, writing the test first is a waste of time if it is being done by the same person as is developing the code: if you know what to test for, then you know how to write code that will pass, and will stop improving the code when it passes all of the tests.

    By testing after the code is written, you get a chance to write additional tests when you have seen odd behaviour.

    If you have the luxury of a seperate testing team, then there is no point bringing them in before any code has been written - that is what a design is supposed to be for!

    Throw away your pocket calculators; visit
  • I'd suggest doing more reading on Test Driven Development before passing judgement. If done correctly, TTD is a good development methodology. The problem I see with it is the abuse that is possible, which one problem is writing of tests such that the code will pass.

    The tests are supposed to be written first based on the users requirements. The code is then written and tested using those tests. Of cousre, you may miss some things this way also.

  • I thought it was pretty good. Too bad you didn't see the point.

    I would be more concerned about your thoughts on the testing team. I would be pretty upset if the testing team was allowed to create tests as they go and keep changing the requirement. Is that what you want from a test team, to keep sending your programs back to development?

    I think this is really informal "testing" as a development process. Maybe there could be a better word for it. But, hopefully there are some requirements before you start. One key objective (at least in project management) is NOT to build more than is required. Someone should be responsible for specifying the requirements before development. Yes, it can specify some standard useability in general. But, work shouldn't come back for more and better ideas.

  • @Lynn

    Maybe I should have made it explicit that my comments were based on experience, not on conjecture.

    If the OP thinks it works well, I would be interested to hear what was different about his setup.


    Sorry, I don't follow what you are getting at.

    Throw away your pocket calculators; visit

Viewing 15 posts - 1 through 15 (of 33 total)

You must be logged in to reply to this topic. Login to reply