为什么tSQLt测试在Visual Studio Test Explorer失败时会通过?

时间:2017-05-18 16:36:33

标签: sql-server unit-testing visual-studio-2015 sql-server-data-tools tsqlt

我正在编写一些tSQLt测试并使用Visual Studio的Test Explorer通过tSQLt test adapter扩展来运行它们。我正在做TDD,所以我在编写它测试的存储过程之前编写测试。

问题是,当我运行测试时,它应该失败,因为存储过程还不存在。当我在Sql Server Management Studio中使用tSQLt运行测试时它会失败:

The module 'test_ValidCustomerName_CustomerIdIs1' depends on the missing object 'dbo.AddCustomer'. The module will still be created; however, it cannot run successfully until the object exists.

(0 row(s) affected)
[AddCustomer].[test_ValidCustomerName_CustomerIdIs1] failed: (Error) Could not find stored procedure 'dbo.AddCustomer'.[16,62]{test_ValidCustomerName_CustomerIdIs1,7}

+----------------------+
|Test Execution Summary|
+----------------------+

|No|Test Case Name                                      |Dur(ms)|Result|
+--+----------------------------------------------------+-------+------+
|1 |[AddCustomer].[test_ValidCustomerName_CustomerIdIs1]|      0|Error |
-----------------------------------------------------------------------------
Msg 50000, Level 16, State 10, Line 1
Test Case Summary: 1 test case(s) executed, 0 succeeded, 0 failed, 1 errored.
-----------------------------------------------------------------------------

但是当我在Test Explorer中运行它时,它说测试通过了:

test explorer screenshot

以下是测试的代码:

EXEC tSQLt.NewTestClass 'AddCustomer';
GO

CREATE PROCEDURE AddCustomer.test_ValidCustomerName_CustomerIdIs1
AS
BEGIN
    DECLARE @customerName   NVARCHAR(40)    = 'John Doe'

    EXEC dbo.AddCustomer @customerName

    DECLARE @expected   INT     = 1
          , @actual     INT     = (SELECT CustomerID
                                   FROM dbo.Customer
                                   WHERE Name = @customerName)

    EXEC tSQLt.AssertEquals @expected, @actual
END
GO

这是dbo.Customer表:

CREATE TABLE dbo.Customer
(
    CustomerID  INT             NOT NULL    PRIMARY KEY IDENTITY(1, 1)
  , Name        NVARCHAR(50)    NOT NULL
)

编辑 - 我已将测试修改为只调用tSQLt.Fail

EXEC tSQLt.NewTestClass 'AddCustomer';
GO

CREATE PROCEDURE AddCustomer.test_ValidCustomerName_CustomerIdIs1
AS
BEGIN   
    EXEC tSQLt.Fail 'Test should fail'
END
GO

测试在Sql Server Management Studio中仍然失败,但在测试资源管理器中传递

1 个答案:

答案 0 :(得分:0)

我已将测试适配器更新到Visual Studio 2017并修复了一些问题。我在这里运行此代码并且无法重现它(测试导致visual studio失败)。

从以下网站获取最新版本:

https://marketplace.visualstudio.com/items?itemName=vs-publisher-263684.tSQLtTestAdapterforVisualStudio2017

如果您需要vs 2015支持让我知道,我犹豫不决,因为它意味着维护两个版本。