I need good tool for database unit testing - sql-server

I've tried to test SQL Server database stored procedures (write tests like unit) using Visual Studio 2010 testing tool but it is very inconvenient.
Is there any another more convenient tool for testing database stored procedures?

Check out TSQLUnit for an open source, SQL Server unit testing app. And there are several more SQL Server-specific testing apps listed on this page.

We are using NUnit, and wrote our own library in C#. Works really well for us, and here are some articles where we have described the approach: Close these Loopholes - Reproduce Database Errors

Related

EF database / model first unit (integration) tests

I want each of my unit (integration) test methods to use a clean and consistent database and specific test data for each test.
Could you please provide me some code samples/snippets and tell me what are best practices for the following questions for both scenarios EF 5 database first and model first.
How to create the database for each test method?
How to set up the test data for each test method?
How to delete the database for each test method?
SSDT project is used to handle database schema, how to use the current SSDT schema for each test run? So that the tests are always executed against the current development version of the database.
Please consider the following assumptions for above questions:
The unit tests shall be executed locally on dev machine and on server CI builds.
Each test may have different test data.
Manually defined .mdf test files should be avoided because several developers are working on the product and there is a potential risk that one deleoper overwrites the changes of the .mdf file which another developer may have checked in previously -> development process should be as simpel as possible.
SSDT is used, so maybe this an option to create the database (probably not a good one because I want the database to be created for each test) and I have no deep knowledge yet about SSDT possiblities.
A good performance of test execution time would be nice to have.
VS/TFS 2012 is used.
SQL Server 2012 is used.
Application is a C# desktop application.
Mocking EF context etc. is not an option.
I hope you can guide me into the right direction how to solve the 4 questions from above. I donĀ“t know if EF provides some functionality (I think only for code first) for my challenges or if this all must be solved by executing SQL scripts or something like that.
Thanks!

How to bundle SQL Server Database with my VS2010 solution

I am currently working on a sample website proof of concept and planning to provide the entire VS2010 (ASP.NET and C#) solution to the company. I also use SQL Server and need to provide the database (tables(including some records) and stored procedures). What is the easiest way to ensure that I can bundle the database along with my VS2010 solution? Please provide some steps if possible.
At my company, we actually take the same approach that you're taking, and just do everything with scripts by hand for deployment. However, we do this mostly because we have a large legacy database, and we do incremental updates for a system that has to always be online.
If you're starting a new website from scratch, you might look into Database Projects inside of Visual Studio. It also has some functionality for unit testing your database built in that might be beneficial.
http://www.visualstudiotutor.com/2010/08/manage-database-projects-with-visual-studio-2010/

SQL Server: pushing development to production?

I'm currently using SQL Server Management Studio.
I have a development database, and access to a live database.
I just want to able to migrate my development tables to the live server.
I'm consulting stack flow first, since I don't want to blow anything up.
If you know what tables you want to migrate and they don't yet exist on the prod server, it's as simple as scripting the table definition, then running the script on the server. If it's more complex than that, I would recommend any number of tools which compare database schemas and automatically generate scripts you can run on the destination server to add/alter/remove what you need. A good freeware one for simple jobs is DBComparer. It's pretty basic. An excellent commercial one are the Sql Tools by Red Gate
We are using SQL Examiner Suite for this, but there are a lot of similar tools.
This is worth a look at - I've used it before and it works well and is very easy: Microsoft SQL Server Database Publishing Wizard http://www.microsoft.com/downloads/en/details.aspx?FamilyId=56E5B1C5-BF17-42E0-A410-371A838E570A&displaylang=en

Test-First development tool for SQL Server 2005?

For several years I have been using a testing tool called qmTest that allows me to do test-driven database development for some Firebird databases. I write a test for a new feature (table, trigger, stored procedure, etc.) until it fails, then modify the database until the test passes. If necessary, I do more work on the test until it fails again, then modify the database until the test passes. Once the test for the feature is complete and passes 100% of the time, I save it in a suite of other tests for the database. Before moving on to another test or a deployment, I run all the tests as a suite to make sure nothing is broken. Tests can have dependencies on other tests, and the results are recorded and displayed in a browser.
Nothing new here, I am sure.
Our shop is aiming toward standardizing on MSSQLServer and I want to use the same procedure for developing our databases. Does anyone know of tools that allow or encourage this kind of development? I believe the Team System does, but we do not own that at this point, and probably will not for some time.
I am not opposed to scripting, but would welcome a more graphical environment.
Any suggestions?
Team System is probably the best-known solution, but you could also try TSQLUnit (SourceForge).
I haven't used it myself, but this article does a decent job of introducing it.
Checkout http://www.sqlservercentral.com/articles/Testing/66553/
and http://www.sqlservercentral.com/articles/Database+Design/66845/
This is a fairly crude article about doing everything within T-SQL.
Have you thought about using NHibernate and using TestDriven or similar just for the tests?
On projects where I didn't have access to team system for db pro's, I have used sql scripts combined with msbuild and the sdc tasks library for msbuild (http://www.codeplex.com/sdctasks). The msbuild script calls on an sdc task to run my sql scripts in a particular order (e.g. create db, create tables etc...) and on a particular connection string. The scripts always check if an object exists and do teardown first and build it back up.
The sql and msbuild scripts I place in a regular visual studio database project (which does nothing special, so you could choose to use a simple empty project), so everything is source-controlled.
with such a set of scripts , you can setup a new database for each test run. You can then use insert scripts to populate it with data and run unit tests against it.
These scripts are also useful for setting up databases from scratch in different environments (DEV/TST/QUA/...)
I was able to adequately apply a test driven development style against SQL Server databases using TSQLUnit. I followed the same flow as you described with writing a unit test sproc first that fails and then making the changes necessary for the test to pass. Over time, I also built up a suite of tests when executed validated that nothing broke while making any new changes.
There was some tough spots (including extreme difficulties in writing tests for existing sprocs) but it worked especially for schema changes. However, I would recommend looking at T.S.T. the T-SQL Test Too1 which unlike TSQLUnit (I had to roll my own) has built-in support for assertions.

Stored Procedure Versioning

How do you manage revisions of stored procedures?
We have a BI solution on SQL Server 2005 with hundreds of stored procedures.
What would be a good way to get these into Subversion? What are your recommended tools to script stored procedures to files?
There are doubtless a bunch of off-the-shelf products you could buy (I think a few RedGate tools might come in handy here), as well as Visual Studio Team Suite - Database Edition.
In light of purchasing something, why not consider using SQL Management Objects (SMO)?
I've written a couple of utilities which generate T-SQL scripts (using the Scripter class) which produces the same scripts you get from generating scripts through the SQL Server Management Studio (it uses the same functionality).
You could integrate such a utility into a build script/build process which would allow you to generate scripts and then version & check them into a source repository. Plus, you can batch the scripts into a single file (if desired) which beats maintaining hundreds of individual files.
I wrote a blog entry about this approach a while back.
Check out more on the SMO class Scripter
Here's a few more entries which might be useful:
http://www.sqlteam.com/article/scripting-database-objects-using-smo-updated
http://sqlblog.com/blogs/ben_miller/archive/2007/10/03/table-scripting-with-smo-part-1.aspx
See here and here for a start.
Please check out here What is the best way to version control my SQL server stored procedures?. Might help you identify couple of solutions to this issue.
I have previously used a Visual Studio Database Project to manage create table scripts, stored procedure scripts etc. I'm fairly sure you could then use subversion to manage these files in the same way as any Visual Studio project.
I used the built in functionality for scripting the procs, but i'm sure Redgate would have some tasty tools for that.

Resources