When you run a tSQLt test you get some messages printed to the output window, including how many tests were run successfully which you need to check, rather than relying on there being 0 errors, there must also be 1 success. To help use NUnit I typically write a test helper in C# which creates a Sql connection and calls tSQLt.Run with the name of the test to run and ensure I read in all “InfoMessages”. I guess that I really prefer using NUnit because I have used it a lot in my c# development and the resharper add-in for running unit tests is really nice. You need to have some way to run your tests, some people include a post-deploy script which calls the unit tests but I prefer to spend the extra time writing wrapper methods using NUnit so that I can run the tests either as a whole, as a class or individually. To get the tSQLt objects into an SSDT project I can deploy, I initially deployed them to an empty database and the used the schema compare in SSDT to bring the objects into an SSDT project:
#Visual studio 2017 sql server unit testing code
To be clear I am not saying the test code should be poorly written, just that we can ensure stricter controls over production code and ensure a separation boundary. We then need somewhere we can put our tests, I like to keep them separate from the production code as it means you can validate the production code and run things like the T-SQL Smells tool ( ) against them without worrying about test code. This is simple, create a sql server project and use it to write you schema and T-SQL code! There is a tSQLt plugin for team city but there is a NUnit test runner for pretty much every single build system out there. The NUnit test runner is not strictly necessary but I find it means that I can write and run tests from within visual studio (I would highly recommend resharper to help here) and the tests can be run the same way on the Continuous Integration server.
![visual studio 2017 sql server unit testing visual studio 2017 sql server unit testing](https://dotnetvibes.files.wordpress.com/2017/03/database-project-build-errors.png)
![visual studio 2017 sql server unit testing visual studio 2017 sql server unit testing](https://venturebeat.com/wp-content/uploads/2018/08/polarrheader.jpg)
This doesn’t need to be the case and it is possible to both write and maintain a database schema completely from within Visual Studio which, I think, is one of the best development environments available. I find that there are often parts of a database project which naturally feel like they should be done in Visual Studio SSDT such as creating tables and stored procedures and then some parts which work better in SSMS, particularly running tSQLt unit tests with the Redgate Sql Test tool. In this day and age all production code should be unit and integration tested and tSQLt is a great solution to help unit test T-SQL code and the two technologies together really make an excellent pair and give us all a great way to develop quickly and with the minimum amount of bugs, if your testing is done correctly. Microsoft standard iconography, colors and other visual effectsĬomparison was conducted on August 14, 2018ĭisclaimer: All product and company names are trademarks ™ or registered ® trademarks of their respective holders.Using SSDT to develop T-SQL code is really nice, although in my opinion it isn’t as nice as developing C# yet, it is a great improvement on using Sql Management studio alone. Run SQL unit tests under one class with a single click Multi-selection of unit tests in the "Unit test explorer" tab Manage all SQL unit tests from within a single form Get messages about passed and failed SQL unit tests
![visual studio 2017 sql server unit testing visual studio 2017 sql server unit testing](https://hybriddbablog.files.wordpress.com/2018/10/working.png)
Demo kit database with predefined unit tests