这种对存储过程进行单元测试的方法可以改进吗?

8kb*_*8kb 5 sql-server testing stored-procedures

当我为存储过程编写单元测试时,它们通常如下所示:

/* Unit test for stored procedure which runs a query */

/* Define #temp table with structure of query results */

/* Define and set variables for stored procedure parameters */ 

/* If stored procedure queries from a particular table...*/ 

/* Truncate table and populate with sample data */ 

/* Call stored procedure using variables but insert results into table */ 

/* INSERT #temp EXEC usp_TestProc @P1=1, @P2 = 'X' */

/* Query #temp table to confirm that all fields have certain values */

/* Query #temp table to confirm that certain number of rows exist */ 

/* If results are incorrect, display error message */ 
Run Code Online (Sandbox Code Playgroud)

虽然这会奏效,但不知何故感觉很笨重,并且在需求不断变化的长期项目过程中很难维护。

  1. 有没有更好的方法(或标准方法)来测试这样的存储过程?
  2. 维护的难度是否仅仅是工作的一部分,是否值得为了进行单元测试而付出努力?我不得不说,在我的大部分工作中,DBA 一开始很少做单元测试。

小智 4

我写了一系列关于使用DBTestUnit进行单元测试选择存储过程的博客,这些博客可能有用。