问题
I'm writing some tSQLt tests and running them using Visual Studio's Test Explorer via the tSQLt test adapter extension. I'm doing TDD, so I'm writing the test before writing the stored procedure that it tests.
The problem is, when I run the test, it should fail because the stored procedure does not exist yet. When I run the test with tSQLt in Sql Server Management Studio it fails like it should:
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.
-----------------------------------------------------------------------------
But when I run it in Test Explorer, it says the test passes:
Here is the code for the test:
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
And here is the dbo.Customer
table:
CREATE TABLE dbo.Customer
(
CustomerID INT NOT NULL PRIMARY KEY IDENTITY(1, 1)
, Name NVARCHAR(50) NOT NULL
)
EDIT - I've modified the test to just call tSQLt.Fail
:
EXEC tSQLt.NewTestClass 'AddCustomer';
GO
CREATE PROCEDURE AddCustomer.test_ValidCustomerName_CustomerIdIs1
AS
BEGIN
EXEC tSQLt.Fail 'Test should fail'
END
GO
The test still fails in Sql Server Management Studio but it passes in Test Explorer.
回答1:
I have updated the test adapter to visual studio 2017 and fixed a couple of issues. I ran this code here and can't reproduce it (the tests cause a failure in visual studio).
Grab the latest version from:
https://marketplace.visualstudio.com/items?itemName=vs-publisher-263684.tSQLtTestAdapterforVisualStudio2017
If you need vs 2015 support let me know, I am hesitant as it means maintaining two versions.
ed
来源:https://stackoverflow.com/questions/44053420/why-does-a-tsqlt-test-pass-in-visual-studio-test-explorer-when-it-should-fail