简体   繁体   English

如何使用Resharper测试运行器对T-SQL代码进行单元测试?

[英]How to unit test T-SQL code using Resharper test runner?

Basically, the title says it all: I am using the Resharper test runner (my tests are written using NUnit), and now I need to test some T-SQL code. 基本上,标题说明了一切:我正在使用Resharper测试运行程序(我的测试是使用NUnit编写的),现在我需要测试一些T-SQL代码。

How do I do that? 我怎么做?

Any help (links to tutorials & co.) would be appreciated. 任何帮助(链接到tutorials&co。)将不胜感激。

What I do not know is: 我不知道的是:

  • Where do I put the tests? 我应该在哪里进行测试? C#? C#? T-SQL as well? T-SQL也一样? ...? ...?
  • How do I setup the test runner to run these tests? 如何设置测试运行器以运行这些测试?
  • Is this even possible with Resharper? Resharper甚至有可能吗?

I have written some unit tests for T-SQL that tests for syntactical errors in the Sql code. 我已经为T-SQL编写了一些单元测试,用于测试Sql代码中的语法错误。 There tests are executed from C# in NUnit or Microsofts Unit testing Framework and run with the preceding statement: 这些测试是从NUnit或Microsoft的单元测试框架中的C#执行的,并使用前面的语句运行:

SET FMTONLY ON;

Microsoft reference of this statement. Microsoft对此声明的引用。

After you only get back metadata so nothing is really executed after this, so you can even test queries that would usually modify data. 在仅获取元数据之后,此后什么也不会真正执行,因此,您甚至可以测试通常会修改数据的查询。

For example, see this NUnit-test below that will be succesful if the Procedure exists and has no syntactical error in it self or in any of the stored procedures dependencies. 例如,请参见下面的NUnit测试,如果该过程存在并且自身或任何存储过程依赖项中都没有语法错误,则该NUnit测试将成功。

[Test]
public void GivenStoredProcedure_WhenParsedBySqlServer_ThenItsResultShouldBeZero()
{
    const string sqlText = "SET FMTONLY ON; EXEC dbo.MyStoreProc;";

    var sqlConnection = new SqlConnection(connectionString);
    var sqlCommand = new SqlCommand(sqlText, sqlConnection);
    sqlCommand.CommandType = CommandType.Text;

    try
    {
        sqlConnection.Open();
        var result = sqlCommand.ExecuteNonQuery();              
        Assert.That(result, Is.EqualTo(0));
    }
    finally
    {
        if (sqlConnection.State == ConnectionState.Open)
        {
            sqlConnection.Close();
        }
        sqlConnection.Dispose();
    }      
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM