简体   繁体   English

对选择查询进行单元测试

[英]Unit testing a select query

I am returning a set of rows, each representing a desktop machine. 我将返回一组行,每个行代表一台台式机。

I am stumped on finding a way to unit test this. 我为找到一种可以对此进行单元测试的方法而感到困惑。 There's not really any edge cases or criteria I can think of, to test. 我真的没有什么可以想到的极端条件或标准可以测试。 It's not like share prices where I might want to check I am getting data which is indeed 5 months old. 我可能不希望检查股价,因为我得到的数据确实有5个月了。 It's not like storing person details where you could check that a certain length always works, or special characters, etc. Or currency and different currencies (£, $, etc) as strings. 这不像是存储人员详细信息,您可以在其中检查一定长度的字符是否始终有效,或特殊字符等。或者货币和其他货币(£,$等)作为字符串。

How would I test this sort of resultset? 我将如何测试这种结果集?

Also, in testing the returnset of a query, there are a few problems: 另外,在测试查询的返回集时,还有一些问题:

1) Testing you have the same number of rows as when you run the query on the server is brittle because someone might change the table data. 1)测试行数与在服务器上运行查询时的行数相同,这很脆弱,因为有人可能会更改表数据。 Is this when you have a test server, which nobody changes unless they upload change scripts? 如果您有一个测试服务器,这是什么,除非上载更改脚本,否则没有人更改?

2) Do you test the dataset object is not null? 2)您是否测试数据集对象不为空? So if it's instantiated as null, but is not after the query's executed, it's holding value (this doesn't prove the data is correct, just that data has been retrieved). 因此,如果将其实例化为null,但不是在执行查询后将其保留,则该值将保持不变(这并不证明数据正确无误,只是表明已检索到数据)。

Thanks 谢谢

You can use a component like NBuilder that will simulate your database. 您可以使用类似NBuilder的组件来模拟数据库。 And as you can manage all the aspects of your dataset you can test several aspects of the database interaction: the number of records your query returns, the range of values in some field. 并且,您可以管理数据集的所有方面时,可以测试数据库交互的多个方面:查询返回的记录数,某个字段中的值范围。 And, because the dataset is always created with the arguments you choose the data are always the same so you can reproduce your tests completly decoupled from your database. 而且,由于数据集总是使用参数创建的,因此您选择的数据总是相同的,因此您可以重现与数据库完全脱钩的测试。

1 - 1-

a) Never ever test against the production server, for any reason. a)决不以任何理由针对生产服务器进行测试。

b) Tests should start from a known configuration, which you can achieve either with mock objects or with a test database (some might argue that unit tests should use mock objects and integration tests should use a test database). b)测试应该从已知的配置开始,您可以使用模拟对象或测试数据库来实现(有些人可能会认为单元测试应该使用模拟对象,而集成测试应该使用测试数据库)。

As for test cases, start with a basic functionality test - put a "normal" row in and make sure you get it back. 至于测试用例,请从基本功能测试开始-在“常规”行中插入并确保将其恢复。 You'll appreciate having that test if you later refactor. 如果您以后进行重构,将不胜感激。 Make sure the program responds correctly to columns being null, or blank. 确保程序正确响应列为空或为空的列。 Put the maximum and minimum values in all the DB fields and make sure the object fields you're storing them in can fit that resolution. 在所有数据库字段中输入最大值和最小值,并确保要存储它们的对象字段适合该分辨率。 Check duplicate records in the DB, or missing records. 检查数据库中的重复记录或丢失的记录。 If you have production data, grab a snapshot of it to put in your test DB and make sure that loads correctly. 如果您有生产数据,请抓取其快照以放入测试数据库中,并确保正确加载。 Is there a value that chronically causes difficulties in other parts of the program? 是否有一个值会长期导致程序其他部分出现问题? Check it here too. 也在这里检查。 And once you release the code, add to the test list any values you find in production that break the system (Regression testing). 释放代码后,将在生产中发现的破坏系统的所有值添加到测试列表中(回归测试)。

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

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