单元测试-存根数据库数据时如何设置测试数据

发布于 2024-08-19 05:25:21 字数 339 浏览 3 评论 0原文

在我们的单元测试中,我有一个存根对象,它在内存中创建一组数据以供单元测试期间使用,以便不使用实时数据库。

我有单元测试,使用被测试的查询以及测试中提供给查询的值来检查从该集合返回的行数。我的第一个问题是,因为我们使用的是 MSTest 并且它不支持参数化测试,所以我们对每组不同的值都有一个测试,并且最终进行了许多测试,仅因提供给一个例程的值而不同。使用不同的测试框架在政治上可能很困难。

此外,处理数据有点笨拙,因为它是通过代码将实体添加到集合中创建的,因此很难轻松查看集合中的数据,如果我们决定将来向该集合添加记录,我们需要更新测试中应返回的记录数,以便我们的测试非常紧密地依赖于这些数据。似乎没有办法自动执行此操作。是这样吗?

In our unit testing, I've got a stub object that is creating a set of data in memory to be used during unit testing so that the live database is not used.

I have unit tests that check the number of rows returned from this set using the query under test and the values supplied to the query in the test. My first issue is that because we are using MSTest and it does not support parametized tests, we have one test for each different set of values and have ended up with many many tests, only differing by values supplied to the one routine. It may be difficult politically to use a different testing framework.

Also working with the data is somewhat unwieldly as it is created by adding entities to a set through code so it's difficult to easily see what data is in the set, and if we decide to add records to this set in the future, we need to update the number of records that should be returned in the tests so our tests depend very tightly on this data. There seems no way to automate this. Is that the case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别忘他 2024-08-26 05:25:21
  1. 既然您已经排除了使用另一个单元测试框架,那么编写您自己的参数化测试怎么样?编写一个循环访问不同数据集的测试,调用具有不同参数的私有帮助器方法。将每个数据集运行的结果收集到“收集参数”。我建议您仅记录错误/失败的数据集以减少噪音。在循环结束时,如果收集参数不为空,则发出等效的 Assert.Fail 并将结果记录到控制台。 (缺点是您无法在 GUI 中看到单独的测试,并且如果组织正在监视测试的数量,则所有这些工作您只能得到 +1。)
  2. 这给您带来了像您一样专业化失败消息的好处。希望 - 您可以在故障跟踪中包含必要的位。这将帮助您“快速查看”哪个场景失败了。
  1. Since you already ruled out using another unit testing framework, how about writing your own take on parameterized tests. Write a test that loops through the different data sets, calling a private helper method with different parameters. Collect the result of each data-set run to a 'collecting parameter'. I'd suggest that you log only errors/failed data-sets to reduce noise. At the end of the loop, if the collecting parameter is not empty, issue the equivalent of Assert.Fail and log the results to the console. (The downside is that you can't see individual tests in the GUI and if the org is monitoring number of tests, you get only +1 for all this work.)
  2. This gives you the benefit of doing as specialized a failure message as you wish - you can include the essential bits in the failure trace. This will help you 'quickly see' which scenario failed.
怀中猫帐中妖 2024-08-26 05:25:21

看看 Visual Studio 2010 终极版如何进行数据库测试(您可以下载完全配置的 VPC)。

一个选项是向您的测试添加“上下文”,因此当您初始化测试时,上下文将使用测试所需的参数进行初始化。您可以通过测试方法中的代码访问参数,也可以将其动态分配给要测试的代码(可能不是最佳选择)。

您还可以添加预期结果或测试应符合的更好条件。这些条件可以从某种数据源(例如数据库)初始化并作为数据集添加。创建一个方法来评估测试方法的条件。

考虑构建特定的类来处理不同的上下文设置或条件,并创建一个测试类可以继承的基测试类(添加功能)。

Have a look at how the Visual Studio 2010 ultimate edition does this for database testing (you can download a fully configured VPC).

A option would be to add "context" to your tests, so when you initialize the test the context gets initialized with the parameters required for the test. You can either access the parameters via code in you test method, or dynamical assign it to the code to be tested (might not be the best option).

Also you can add the expected results or better yet conditions that the test should comply with. These conditions can be initialized from some sort of data source (e.g. database) and added as a dataset. Create a method that will evaluate the conditions for the test method.

Consider building specific classes to handle different context settings or conditions and create a base test class (which adds the functionality) from which you test class can inherit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文