在 sql server 2008 中实现数据库测试驱动开发的最佳方法是什么
我正在开发一个解决方案来实现数据库测试驱动的开发,以测试存储过程的更改。到目前为止,我的想法是执行存储过程并以原始格式存储预期的数据和模式结果集。进行 sp 更改,然后再次执行存储过程并断言架构和数据相等。
首先,我将数据存储在 SQL Server 数据库中,例如:-
testdata.storedproc
- ID - int
- ResultsSet - int
- Data - XML
- Date - DateTime
testschema.storedproc
- ID - int。
- 结果集 - 整数。
- 架构 - XML。
- 日期 - 日期时间。
一切都很顺利,直到我发现一个存储过程让我头疼:-
存储过程:-
- 有多个结果集。
- 具有包含相同列名的列,即。 (人物.姓名,区域.名称)。
- 数据包含非法 xml 字符。
- 结果超过 120000 行。
这打破了我的初心,有没有人有任何关于数据库测试驱动开发的知识想要分享,或者有可以使用的替代解决方案?
I am develeping a solution to achieved database test driven develepment for testing stored procedure changes. My thoughts so far is to execute the stored proc and store the expected data and schema results set in a raw format. Make the sp change then execute the stored proc again and assert that the schema and data are equal.
At 1st I stored the data in a sql server database like:-
testdata.storedproc
- ID - int
- ResultsSet - int
- Data - XML
- Date - DateTime
testschema.storedproc
- ID - int.
- ResultsSet - int.
- Schema - XML.
- Date - DateTime.
All was going well until i found a stored proc that has is causing me headache:-
The stored procedure :-
- has multiple results sets.
- has columns that contains the same column name ie. (person.name, area.name).
- has data contains with illegal xml characters.
- has results in excess of 120000 rows.
This breaks my intital soultion, does anyone have any knowlege on database test driven development that they would like to share, or have alternative solutions that could be of use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用 DbFit 库(与 Fit/Fitnesse 结合)以测试优先的方式开发存储过程,取得了巨大的成功。测试以 HTML 形式编写,可以作为独立文件 (Fit) 或 Wiki 页面 (FitNesse)。 DbFit 管理数据库连接、执行查询和存储过程,并(默认情况下)将每个单独的测试包装在事务中以帮助保持事物的可重复性。
有关 Fit/Fitnesse 的更多信息,请参阅 FitNesse 网站 以及标记为 fitnesse。 DbFit 可以在 GitHub 上找到。
I have had a great deal of success using the DbFit library (in conjunction with Fit/Fitnesse) to develop stored procedures in a test-first manner. Tests are written in HTML, either as standalone files (Fit) or Wiki pages (FitNesse). DbFit manages the database connectivity, executes queries and stored procedures, and (by default) wraps each individual test in a transaction to help keep things repeatable.
For more on Fit/Fitnesse, see the FitNesse site and the questions tagged fitnesse here on StackOverflow. DbFit can be found on GitHub.
测试数据存储解决方案
您将使用一种简单的原始存储格式,正如您所发现的,当您的存储过程可以在整个关系数据库中自由使用时,这种格式并不是很有帮助。
相反,您应该将每个测试的结果集保留在数据库中。然后将该数据库中的数据与您设置的另一个结果数据库或能够表示多个表中的多行和多列数据的简单数据格式进行比较。
您可以将预期的测试结果数据编写为 DML(可能从实际数据库中提取),这是一种可以处理更复杂的数据模式的自定义 Xml 格式,或者只是在自定义结果集数据库上执行数据库备份/恢复。
就我个人而言,我会尝试使用自定义的仅测试实体框架或 NHibernate 模型来导入架构(但没有存储过程,或任何可能使我的测试变得更加困难的东西),并使用
CreateDatabase 功能来部署测试模式。然后,您可以利用 .Net 代码来存储参考数据(对象初始值设定项)、正在使用的 ORM(提取和比较数据)和 NUnit。这与我在下面进行的头脑风暴非常吻合。
我对您的更高级别问题进行了一些头脑风暴
理想情况下,测试用例将单独测试逻辑。由于您无法真正拆分单个存储过程,因此您至少可以单独运行它。
以下是实现存储过程测试所需的一般描述。请注意,我所说的这些都是数据库方面的,但是您可以用任何使您的工具和测试用例更易于编写、理解和维护的内容来替换其中一些。
部件
输入数据
输出数据
工具
其中一些可以在同一存储过程的测试用例之间共享。您可以共享架构 DDL,也许是一些 DML,以及为每个测试设置/执行/比较/清理的逻辑。
执行
安排
执行
断言
出于诊断目的,数据应该相同,或者应该输出一个不错的错误,告诉您差异是什么。
Solutions for test data storage
You are going with a simple raw storage format, which as you've discovered isn't very helpful when your sprocs are free to play around in an entire relational database.
Instead you should leave your result set from each test in a database. Then compare the data in that DB to another result DB you've set up, or a simple data format that is capable of representing multiple rows and columns of data in multiple tables.
You could author your expected test result data as DML (possibly extracted from an actual DB), a custom Xml format that can handle more complex data schemas, or simply do a DB backup/restore on a custom result set DB.
Personally, I'd try to cheat by using a custom test-only Entity Framework or NHibernate model to import the schema (but none of the sprocs, or anything that might make my testing harder), and use the
CreateDatabase
feature to deploy the schema for tests. Then you can take advantage of .Net code to store your reference data (object initializers), the ORM you're using (to extract and compare the data), and NUnit. This meshes well with the brainstorming I've had below.Some brainstorming I've had on your higher level problem
Test cases ideally will test logic in isolation. Since you can't really split up a single stored procedure, you can at least run it in isolation.
Here's a general perscription for the things you would need to implement a stored procedure test. Note that I speak of these all in terms of the DB, but you could replace some of it with whatever makes your tools and test cases easier to write, understand, and maintain.
Parts
Input data
Output data
Tooling
Some of this can be shared between test cases for the same sproc. You could share the schema DDL, maybe some of the DML, and the logic to set up/execute/compare/clean up for each test.
Execution
Arrange
Act
Assert
For diagnostic purposes, the data should be identical, or a nice error should be output, telling you what the differences are.