Question

Is there any way to expose tSQLt tests inside Visual Studio, in a similar way to NUnit?

Even better, so all the tests in a solution can be run using Resharper

... I'm aware of Red Gates SQL Test product, but it only seems to target SSMS

Was it helpful?

Solution

Yes this is actually very easy, but does require writing some plumbing code. The TestClasses, Tests and TestResults are all available in tSQLt. We have simply written a t4 template that wraps our tSQLt tests and we use the template to generate the nunit tests. Our tSQLt workflow is all within our Visual Studio projects and we use FluentMigrator to do our database migrations.

I have posted a tSQLt demo workflow solution on github (tsqlt-demo-workflow) if you are interested. This workflow is similar to the one that we have been working with.

Hope this helps.

OTHER TIPS

It sounds like you've already added tSQLt and tests to your database and you'd like to run them. If so, it's pretty easy to expose them as NUnit tests which Resharper can run.

In this example I'm using NUnit's [TestCaseSource] to call to the database and retrieve the names of the tSQLt test classes. You could also do this at the individual test level but that's more fine grained than I needed. Returning the names within TestCaseData objects means that NUnit runners, like ReSharper, will list each tSQLt test class as an individual test.

    [TestCaseSource(nameof(GetSqlTests))]
    public void SqlTests()
    {
        try
        {
            // tSQLt.Run @testName
            var message = new Database().RunTest(TestContext.CurrentContext.Test.Name);
            Assert.Pass(message);
        }
        catch (SqlException ex)
        {
            Assert.Fail(ex.Message);
        }
    }

    public IEnumerable<TestCaseData> GetSqlTests()
    {
        // SELECT Name from [tSQLt].[TestClasses]
        var names = new Database().GetTestClasses();
        return names.Select(name => new TestCaseData().SetName(name));
    }

I have written a visual studio test adapted that searches for tSQLt tests in .sql files and can be run using the test explorer window:

https://the.agilesql.club/blogs/Ed-Elliott/tSQLt-Visual-Studio-Test-Adapter

I am planning on writing a resharper plugin to let it run them as well but I haven't done that yet.

Ed

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top