mardi 24 mars 2015

tSQLt: share data between SetUp and tests

I am using tSQLt to unit test t-sql code.


Quite often the Arrange part of tests is quite extensive and I am trying to push a lot of it out to the SetUp procedure to reuse among the tests within the class.


It would be very useful if Setup and test procedures could "know" the same information, i.e. have some shared data. For example, let's say setup creates a test invoice and sets invoice ID to something known:



CREATE PROCEDURE [InvoiceManager].[SetUp]
AS
DECLARE @TestId INT = 10;

EXEC tsqlt.FakeTable @SchemaName='dbo', @TableName='Invoice';
INSERT INTO dbo.Invoice (Id, Amount) VALUES (@TestId, 20.50);
GO


And then in the test we want to do something to the test invoice, like so:



CREATE PROCEDURE [InvoiceManager].[Test_InvoiceHandler]
AS
DECLARE @TestId INT = 10; -- duplication I would like to eliminate

-- Action
EXEC dbo.InvoiceHandler @InvoiceId = @TestId;

-- Assert
-- ... some assertions
GO


Would be nice to be able to replace duplicating the @TestId's value in both (and more) procedures by just pushing it into some "class variable" in SetUp procedure and then use it from the tests. Any ideas how to achieve it in a compact manner? I could imagine creating a table in [InvoiceManager] schema and reading from it in tests. Any chance something like this exists just that I can't find it in the docu? Thanks!


Aucun commentaire:

Enregistrer un commentaire