简体   繁体   English

SQL测试最佳实践

[英]SQL testing best practice

What is the best practice for SQL testing (stored procedures, views, queries, triggers...) excluding automated testing? 除了自动测试之外,SQL测试(存储过程,视图,查询,触发器...)的最佳实践是什么? Most of the time a query works or returns thousands of rows, so it is a bit difficult to check them. 大多数情况下,查询有效或返回数千行,因此检查它们有点困难。 I want to know how you usually do the testing of a SQL query. 我想知道您通常如何测试SQL查询。 Can you point me to a good (online) reference? 您能指出我一个很好的(在线)参考资料吗?

Try to eliminate extra variables. 尝试消除额外的变量。

Ways to do this: 方法:

  • Each test should test only one feature (a view, a stored procedure, etc.) 每次测试应仅测试一个功能(视图,存储过程等)
  • Use known test data (it should be from a real environment (but not actually the real environment) 使用已知的测试数据(它应该来自真实环境(而不是真实环境)
  • Use the smallest amount of test data that adequately tests the feature. 使用最少的测试数据即可充分测试该功能。

For inserts, updates, deletes I check the state of the table before and after the proc is run for records which meet the conditions of the insert update or delete. 对于插入,更新,删除,我在运行proc之前和之后检查表的状态,以查找满足插入更新或删除条件的记录。 So if I am adding 7 records, they shouldn't be in the table beforehand and should be there after. 因此,如果我要添加7条记录,则它们不应该事先在表中,而应该在表后。

Selects with thousands of records can be harder. 具有数千条记录的选择可能会更加困难。 There is no substitute for actually knowing your data and what you would expect to get. 真正了解您的数据以及您期望获得的结果无可替代。 For instance, I know that a certain client has around 2000 sales reps. 例如,我知道某个客户大约有2000名销售代表。 If I run a query that should have all the reps on it, and there are only about 1000 of them, I know something is wrong. 如果我运行一个查询,其中应该包含所有代表,而其中只有大约1000个,那么我知道有些问题。 Sometimes I will place there results of a query into a temp table, so I can run statistics on it. 有时,我会将查询结果放在临时表中,这样就可以对其进行统计。 If I'm doing an attendee report I can then see that there are 200 distinct meetings in the report in the time period according to my query. 如果我正在做一个与会者报告,那么根据我的查询,可以看到该报告期内该报告中有200个不同的会议。 If I look at just that table and see there are 350 meetings in the same time period, I go looking to see what is excluding meetings and usually look at the details of one or more of the excluded meetings and it's related tables to see why it isn't showing up. 如果我仅查看该表并看到在同一时间段内有350个会议,那么我会去查看什么是排除会议,通常会查看一个或多个被排除的会议的详细信息以及与之相关的表,以了解其原因没有出现。 Usually you will find a status that needs to accounted for, or bad data of some sort. 通常,您会发现需要说明的状态或某种错误数据。

I also look for record that are duplicated. 我还寻找重复的记录。 If I expect one record per meeting and the same meeting is in there twice, I know that one of the join tables has more records than I was expecting for the conditions of the query. 如果我希望每个会议有一个记录,而同一个会议有两次记录,那么我知道联接表之一的记录比查询条件要多。

I often ask some of our operations people to look at the results of a query for a report as well. 我经常要求我们的一些运营人员也查看报表查询的结果。 Because they are closer to the use of the data than I am, they will often spot things I don't. 因为它们比我更接近使用数据,所以它们经常会发现我没有的东西。

Another technique is to deliberately limit the where clause for testing to a smaller subset of data that you can actually manually examine to see if what you would expect is what you got. 另一种技术是将测试的where子句故意限制为较小的数据子集,您可以手动检查这些数据子集,以查看是否期望得到的结果。 This is particularly helpful if you have lots of calculations or complex calculations. 如果您有大量计算或复杂计算,这将特别有用。 Anytime I do a complicated calculation I look at the raw data for one typical case and manually calculate the formula from the raw data, then I can check to see if it is correct in my query. 每当我进行复杂的计算时,我都会查看一种典型情况下的原始数据并根据原始数据手动计算公式,然后可以检查查询中的公式是否正确。

Triggers I test by writing them as regular queries first (after first creating and populating the temp tables for #inserted and #deleted). 我首先通过将触发器编写为常规查询进行测试(首先创建并填充#inserted和#deleted的临时表之后)。 I make sure to add multiple records to my temp tables because every trigger must be able to correctly handle multiple record inserts/updates or deletes. 我确保将多个记录添加到我的临时表中,因为每个触发器都必须能够正确处理多个记录的插入/更新或删除。 Then I write code to show the before state and the after state and put it all in a transaction so I can roll it back while testing. 然后,我编写代码以显示before状态和after状态,并将其全部放入事务中,以便在测试时将其回滚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM