Running tSQLt tests from C/VB.Net/Java/Whatever

When you already have a test framework setup such as MSTest or NUnit and possibly a CI build in place and you want to add Sql Server tests it is often wise to call the tests from the framework you already have setup.

I do this at the moment using C# and I have a helper method that calls tSQLt.Run/RunClass/RunAll passing in the name of the test(s) to run - this means that from within visual studio, I can see and run all of my tSQLt tests alongside my existing NUnit / Moq tests.

One issue is that the response from tSQLt.Run is human readable, it is basically a string and also comes down the TDS channel for messages rather that responses so with the SqlClient you need to use the “InfoMessage” event and then parse the string - this is one of those things that is a bit of a pain so instead what you can do is to run your unit test using tSQLt.Run and then call “EXEC [tSQLt].[XmlResultFormatter]” this returns the result of the last call to RunAll in a nicely, easily passed string from the normal TDS response so you can use use a standard SqlDataReader to read the first column of the first row.

Update: Everytime I look at tSQLt it gets better and better! There is also a “tSQLt.RunWithXmlResults” which will run a test and return the results as Xml which saves the additional step.

Enjoy!