简体   繁体   English

带有数据库和ADO.NET实体框架的.NET TDD - 集成测试

[英].NET TDD with a Database and ADO.NET Entity Framework - Integration Tests

I'm using ADO.NET entity framework, and am using an AdventureWorks database attached to my local database server. 我正在使用ADO.NET实体框架,并使用附加到我的本地数据库服务器的AdventureWorks数据库。 For unit testing, what approaches have people taken to work with a database? 对于单元测试,人们采用了哪些方法来处理数据库?

Obviously, the database has to be in a pre-defined state of change so that the tests can have some isolation from each other... so I need to be able to run through the inserts and updates, then rollback either between tests or after the batch of tests are done. 显然,数据库必须处于预定义的更改状态,以便测试可以相互隔离...所以我需要能够运行插入和更新,然后在测试之间或之后回滚完成了一批测试。

Any advice? 有什么建议?

Thanks. 谢谢。

I have experienced a similar kind of situation. 我遇到过类似的情况。

Storing data to DB and retaining them to previous state are not ideally unit tests. 将数据存储到DB并将其保留到先前状态不是理想的单元测试。 They are Integration test cases. 它们是集成测试用例。

In order to perform the integration tests (insert / update operations ) without affecting the actual data ,I would suggest the following 为了在不影响实际数据的情况下执行集成测试(插入/更新操作),我建议如下

1. Use different database(change the name of the actual DB) , and let the schema remain the same as the actual DB. 1.使用不同的数据库(更改实际DB的名称),并使架构保持与实际DB相同。

2. Point the unit tests to the newly created DB. 2.将单元测试指向新创建的DB。

3. Now how do we create a new DB , for running the tests alone in an automated way ? 3.现在我们如何创建一个新的数据库,以自动方式单独运行测试?

Ans : Have the SQL scripts listed in a batch file , such as this.(here i presume you are using SQL SERVER) Ans:在批处理文件中列出SQL脚本,例如这个。(这里我假设你使用的是SQL SERVER)

Eg, sqlcmd -ic:\\data\\runscripts\\InventoryMonthEnd.sql 例如,sqlcmd -ic:\\ data \\ runscripts \\ InventoryMonthEnd.sql

Specify the batch file , which lodges this sql statement(s) to be executed in MSBuild process. 指定批处理文件,该文件将在MSBuild进程中执行此sql语句。

Sample MSBuild Task . 示例MSBuild任务。

<ItemGroup>
<StoredProcScripts Include="$(RelativeSPDir)/*.sql" />
</ItemGroup>

<Target>
<Exec Command="isql -E -n -b -d DBName -i %(StoredProcScripts.Identity)" Condition="'%(StoredProcScripts.Identity)' != ''"/>
</Target>

Personally I have each test create it's own data and delete it when the test completes. 我个人每次测试都会创建自己的数据,并在测试完成后将其删除。 This way, the data a test requires and the test itself are kept closely together and can be maintained as one unit. 这样,测试所需的数据和测试本身紧密地保持在一起,并且可以作为一个单元进行维护。 For data shared by a set of related tests (in the same *Test.cs file), I use [TestInitialize] Start() and [TestCleanup] Finish() . 对于由一组相关测试共享的数据(在相同的* Test.cs文件中),我使用[TestInitialize] Start()[TestCleanup] Finish()

I also have a utility program that re-creates an empty test database, as the unit test's cleanup code will not run if I'm debugging a test and terminate it in the debugger. 我还有一个实用程序,它重新创建一个空的测试数据库,因为如果我正在调试测试并在调试器中终止它,单元测试的清理代码将不会运行。 To re-create the database, I use EF 4's ObjectContext.CreateDatabase() method. 要重新创建数据库,我使用EF 4的ObjectContext.CreateDatabase()方法。

You can use MbUnit which has a rollback attribute. 您可以使用具有rollback属性的MbUnit。 This will make sure that all the changes made in the Test will be rolled back. 这将确保将回滚测试中所做的所有更改。

http://weblogs.asp.net/astopford/archive/2006/07/25/MbUnit-database-testing_2C00_-and-thanks-to-Roy.aspx http://weblogs.asp.net/astopford/archive/2006/07/25/MbUnit-database-testing_2C00_-and-thanks-to-Roy.aspx

This is how we solved it: 这就是我们解决它的方式:

  1. Use a Sandbox database in first place for development/testing 首先使用Sandbox数据库进行开发/测试
  2. Create all necessary data in the tests 在测试中创建所有必要的数据
  3. Delete all of the test data created by the tests after they pass/fail. 删除测试通过/失败后创建的所有测试数据。 Something like a try...finally 有点像try...finally

A really effective way to manage data in your tests is to use a transaction. 在测试中管理数据的一种非常有效的方法是使用事务。 A colleague introduced this to me and I later saw it used in a number of places (GOOS) 一位同事向我介绍了这个,我后来看到它在很多地方使用过(GOOS)

In C# if you use a TransactionScope and dont comit the transaction you have a really effective way of keeping things clean. 在C#中,如果您使用TransactionScope并且不进行交易,那么您将拥有一种非常有效的方法来保持清洁。

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

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