Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit tests framework for databases [closed]

I´m looking for a unit tests framework for database development. I´m currently developing for SQL Server 2000, 2005 and 2008. Do you know of any good frameworks with similar functionality as JUnit and NUnit?
Perhaps it´s better to ask, what do you use to unit test your stored procedures and user defined functions?

like image 351
bjorsig Avatar asked Oct 14 '08 21:10

bjorsig


People also ask

Should unit tests interact with database?

Unit tests shouldn't depend on infrastructure For example, your database functions with all their SQL and driver libraries should be separate from the code that ensures your user's passwords are secure. There's no way to test this function without having a database connection available at the time of testing.

Can unit tests use databases?

It is meant to make sure that definable modules of code work as expected. To test an application it is not enough to use unit tests. You must also perform functional testing and regression testing. Database access falls outside the scope of unit testing, so you would not write unit tests that include database access.

Should I mock the database in unit tests?

Mocking and stubbing are the cornerstones of having quick and simple unit tests. Mocks are useful if you have a dependency on an external system, file reading takes too long, the database connection is unreliable, or if you don't want to send an email after every test.

How do you write a unit test case for a database connection?

Unit test should test classes without external dependencies (like DB or calls to other remote services). You should always try to separate the business logic (your Domain Model) code from infrastructure code then it will be easy to use unit tests. Be careful with Mocks, it can be a signal of bad design.


4 Answers

There is TSQLUnit... Link here: http://tsqlunit.sourceforge.net/

like image 161
mannu Avatar answered Nov 09 '22 20:11

mannu


I tend to use a data access assembly and test that with NUnit using the idea outlined in http://weblogs.asp.net/rosherove/articles/dbunittesting.aspx it uses transactions to rollback changes. This is not a dedicated database approach but is good if you want to test at a higher level than pure database.

It may be worth having a look at http://weblogs.asp.net/rosherove/archive/2004/07/12/180189.aspx

like image 39
dice Avatar answered Nov 09 '22 19:11

dice


I'm currently using DBUnit in my project. It's quite a good tool to test a database to see if the results you expect are there, but also it can alter the data in a database to predefined values, for example, it will reset a database to the data you were using before the tests, leaving the tables unchanged.

http://dbunit.sourceforge.net/

like image 41
Richard Avatar answered Nov 09 '22 21:11

Richard


I've also come across T.S.T. the T-SQL Test Tool http://tst.codeplex.com/

like image 26
JoshL Avatar answered Nov 09 '22 21:11

JoshL