简体   繁体   English

如何在C#中为数据库数据进行集成测试?

[英]How to seed database data for integration testing in C#?

I've been writing some integration tests recently against ASP.Net MVC controller actions and have been frustrated by the difficulty in setting up the test data that needs to be present in order to run the test. 我最近一直在针对ASP.Net MVC控制器操作编写一些集成测试,并且由于难以设置为了运行测试而需要存在的测试数据而感到沮丧。

For example, I want to test the "add", "edit" and "delete" actions of a controller. 例如,我想测试控制器的“添加”,“编辑”和“删除”操作。 I can write the "add" test fine, but then find that to write the "edit" test I was am either going to have to call the code of the "add" test to create a record so that I can edit it, or do a lot of setup in the test class, neither of which are particularly appealing. 我可以编写“添加”测试,但后来发现编写“编辑”测试我要么必须调用“添加”测试的代码来创建一个记录,以便我可以编辑它,或者在测试类中做了很多设置,这些都没有特别吸引人。

Ideally I want to use or develop an integration test framework to make it easier to add seed data in a reusable way for integration tests so that the arrange aspect of an arrange/act/assert test can focus on arranging what I specifically need to arrange for my test rather than concerning itself with arranging a load of reference data only indirectly related to the code under the test. 理想情况下,我想使用或开发集成测试框架,以便以可重用的方式为集成测试添加种子数据,以便安排/行为/断言测试的排列方面可以集中精力安排我特别需要安排的内容我的测试而不是关注自己安排一些参考数据,这些参考数据只与测试下的代码间接相关。

I happen to be using NHibernate but I believe any data seeding functionality should be oblivious to that and be able to manipulate the database directly; 我碰巧使用NHibernate,但我相信任何数据播种功能都应该忽略它,并且能够直接操作数据库; the ORM may change, but I will allways be using a SQL database. ORM可能会改变,但我总是会使用SQL数据库。

I'm using NUnit so envisage hooking into the test/testfixture setup/teardown (but I think a good solution would potentially transferable to other test frameworks). 我正在使用NUnit,因此设想挂钩测试/ testfixture setup / teardown(但我认为一个好的解决方案可能会转移到其他测试框架)。

I'm using FluentMigrator in my main project to manage schema and seeding of reference data so it would be nice, but not essential to be able to use the FluentMigrator framework for a consistent approach across the solution. 我在我的主项目中使用FluentMigrator来管理参考数据的模式和种子,因此它很好,但不是必须能够使用FluentMigrator框架在整个解决方案中采用一致的方法。

So my question is, "How do you seed your database data for integration testing in C#?" 所以我的问题是,“你如何为C#中的集成测试种下数据库数据?” Do you execute the SQL directly? 你直接执行SQL吗? Do you use a framework? 你使用框架吗?

You can make your integration testing on Sql Server Compact, you will have a .sdf file and you can connect to it giving the file's path as connection string. 您可以在Sql Server Compact上进行集成测试,您将有一个.sdf文件,您可以连接到它,将文件的路径作为连接字符串。 That would be faster and easier to setup and work with. 这将更快,更容易设置和使用。

Your integration test would not probably need millions of rows of data. 您的集成测试可能不需要数百万行数据。 You can insert your test data into your database and save it as TestDbOriginal.sdf . 您可以将测试数据插入数据库并将其另存为TestDbOriginal.sdf

When you are running your tests, just make a copy of this ' TestDbOriginal.sdf ' and work on that copy, which is already seeded with data. 当您运行测试时,只需复制一个' TestDbOriginal.sdf '并处理该副本,该副本已经有数据。 If you want to test a specific scenario, you will need to prepare your data by calling some methods like add, remove, edit . 如果要测试特定方案,则需要通过调用添加,删除,编辑等方法来准备数据。

When you go production or performance testing, switch back to your original server version, be it Sql Server 2008 or whatever. 当您进行生产或性能测试时,切换回原始服务器版本,无论是Sql Server 2008还是其他任何版本。

我不知道它是否一定是'正确'的事情,但我总是使用我的add / create方法播种。

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

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