question

sqlhungry avatar image
sqlhungry asked

need suggestion on unit testing template in Visual Studio 2010

I got four queries, which are 1) Every table should have a clustered index 2) Every table should have a primary key 3) No tables should exist on the primary filegroup 4) Primary Keys should follow the naming standard, eg “pk_ ” I already got the queries for these test conditions. Now i need to build a Visual Studio Database Project for unit testing in Visual Studio 2010 Ultimate. I get new databases coming in every now and then in my Sql Server Mgmt Studio 2008. I need a suggestion on the best plan to implement so that I can create a unit testing template to test the above 4 test conditions for each new databases coming in. Is it a good idea to create a separate unit testing project for each test condition or should i create one unit testing project for all the conditions. Also Suggest me the best way to do it in Visual Studio 2010 Ultimate. Thanks, Rgds, sqlhungry
visual-studio-2010unit-testing
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Kevin Feasel avatar image
Kevin Feasel answered
Asking for "the best way" may lead to some discord, but here's a way which works, at least... First, create a test project. In that test project, add a Database Unit Test. ![Creating a Database Unit Test][1] After that, you will be able to add database tests. I would create four tests, one for each of your queries. These are simple enough that the Database Unit Test in VS2010 can work. More complex tests would probably require using something a bit stronger, like NUnit, but when I wrote the queries in [my answer to your last question]( http://ask.sqlservercentral.com/questions/33961/sql-server-dmvs-to-identify-test-conditions/33967), I actually had this in mind. ![A basic database unit test][2] You don't need to have different projects for each, nor do you even need a different test file. After you create them, the drop-down list shows each of your tests. ![All of the structural compliance tests together][3] One advantage to doing it this way is that, in your Test View window, you can group by the class name and get all of your structural compliance tests together. Then, you could create, say, read-only tests, write-based tests, authorization/authentication tests, etc. ![Tests grouped by class name][4] Once you get those in, you could copy the .cs and .resx files into other projects and they'll work there as well, so you get the benefits of a template. For that to work, you would need a DatabaseSetup.cs and an app.config, both of which are created automatically when a Database Unit Test is added to a test project. So you would be able to start working on your project-specific tests and drop these files in. [1]: /upfiles/DatabaseUnitTest.png [2]: /upfiles/Test.png [3]: /upfiles/AllTests.png [4]: /upfiles/GroupedTests.png
6 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kevin Feasel avatar image Kevin Feasel commented ·
Thank you both. It's been very interesting so far, and everything I've read about becoming active in trying to answer questions is exactly right: this is a great way to learn and focus your mind. Plus, when you're done, you get to see somebody come up with an even better idea and mercilessly borrow it later. :-)
2 Likes 2 ·
Fatherjack avatar image Fatherjack ♦♦ commented ·
Nice answer and congratulations on getting your first thousand points. It's great to have you contributing to this forum, thanks.
0 Likes 0 ·
WilliamD avatar image WilliamD commented ·
Awesome answer (+1). I also agree with @Fatherjack - great work so far, thanks for contributing.
0 Likes 0 ·
Fatherjack avatar image Fatherjack ♦♦ commented ·
Kevin - do you know of anything that will do this when VS edition doesnt support Database Test objects? Alternative products, 3rd party objects?
0 Likes 0 ·
sqlhungry avatar image sqlhungry commented ·
Thanks Kevin for the solution. I tried it just now and it is helpful indeed. Regards, sqlhungry
0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.