在SQL Server中测试存储过程

时间:2018-08-26 11:52:49

标签: unit-testing testing sql-server-2012

我编写了一个存储过程,通过传递不同的参数并将执行该过程所需的计数和时间存储在临时表中,以测试另一个存储过程。过程比功能好吗?

CREATE PROCEDURE [dbo].[SPTEST_NEW] 
AS 
BEGIN
    DECLARE @COUNT INT;
    DECLARE @STARTDATETIME DATETIME;
    DECLARE @ENDDATETIME DATETIME;
    DECLARE @TimeDiff NVARCHAR(50);
    DECLARE @SPNAME VARCHAR(50);

    DECLARE @Result TABLE
                    (
                        Cases NVARCHAR(100),
                        RecordCount INT,
                        ExecutionTime NVARCHAR(50)
                    )
    DECLARE @Search TABLE
                    (
                        ID NVARCHAR(50),
                        NAME NVARCHAR(200),
                        FMT NVARCHAR(100)
                    )

     SET @SPNAME = 'dbo.[SEARCH_VALUE]'; 
     SET @STARTDATETIME = GETDATE();

     --Case#1: Search by All
     INSERT INTO @Search
         EXEC @SPNAME @PRODUCTID = NULL,
                      @PRODUCTNAME = NULL,
                      @FORMAT = NULL

     SET @ENDDATETIME = GETDATE();  
     SELECT @COUNT= @@ROWCOUNT

     SET @TimeDiff = CONVERT(TIME, @ENDDATETIME - @STARTDATETIME);

     INSERT INTO @Result 
     VALUES ('ALL', @COUNT, @TimeDiff)

     -- Case#2 : Search by Product-ID
     INSERT INTO @Search
         EXEC @SPNAME
                   @PRODUCTID = 'ABPROD-BLACK',
                   @PRODUCTNAME = NULL,
                   @FORMAT = NULL,

      SET @ENDDATETIME = GETDATE();
      SELECT @COUNT = @@ROWCOUNT

      SET @TimeDiff = CONVERT(TIME, @ENDDATETIME - @STARTDATETIME);

      INSERT INTO @Result 
      VALUES ('PROD-ID:ABPROD-BLACK', @COUNT, @TimeDiff)

      --Case#3 : Search by FORMAT
      INSERT INTO @Search
          EXEC @SPNAME
                   @PRODUCTID = NULL,
                   @PRODUCTNAME = NULL,
                   @FORMAT = 'SCHT',

      SET @ENDDATETIME = GETDATE();
      SELECT @COUNT= @@ROWCOUNT

      SET @TimeDiff = CONVERT(TIME, @ENDDATETIME - @STARTDATETIME);

      INSERT INTO @Result 
      VALUES ('Format:SCHT', @COUNT, @TimeDiff)

      SELECT * 
      FROM @Result
END

此方法正确吗?还是其他方法?请提出方法。

0 个答案:

没有答案