实体框架代码优先-相互重叠的测试 [英] Entity Framework Code First - Tests Overlapping Each Other

查看:90
本文介绍了实体框架代码优先-相互重叠的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的集成测试使用的是使用EF初始化程序生成的实时数据库.当我分别运行测试时,它们将按预期运行.但是,当我一次运行它们时,会遇到很多失败的测试.

My integration tests are use a live DB that's generated using the EF initalizers. When I run the tests individually they run as expected. However when I run them all at once, I get a lot of failed tests.

我似乎有一些重叠之处.例如,我有两个使用相同设置方法的测试.此设置方法可构建&填充数据库.这两个测试执行相同的测试ACT,从而向数据库添加了少量项目(相同的项目),但是唯一的区别是每个测试都在寻找不同的计算(而不是一个大型的测试,它执行很多工作).

I appear to have some overlapping going on. For example, I have two tests that use the same setup method. This setup method builds & populates the DB. Both tests perform the same test ACT which adds a handful of items to the DB (the same items), but what's unique is each test is looking for different calculations (instead of one big test that does a lot of things).

我可以解决此问题的一种方法是在设置中做一些技巧,为每个运行的测试创建一个唯一的数据库,以使所有内容保持隔离.但是,当我这样做时,EF初始化工作不起作用,因为它正在创建新的数据库而不是删除&用一个新的替换它(后者触发种子).

One way I could solve this is to do some trickery in the setup that creates a unique DB for each test that's run, that way everything stays isolated. However the EF initilization stuff isn't working when I do that because it is creating a new DB rather than dropping & replacing it iwth a new one (the latter triggers the seeding).

关于如何解决此问题的想法?似乎是我的测试的组织……只是没有显示如何最好地进行测试,而是在寻找输入.真的不需要手动运行每个测试.

Ideas on how to address this? Seems like an organization of my tests... just not show how to best go about it and was looking for input. Really don't want to have to manually run each test.

推荐答案

使用测试框架提供的测试设置和拆卸方法,在测试设置中启动事务,并在测试拆卸中回滚事务(

Use test setup and tear down methods provided by your test framework and start transaction in test setup and rollback the transaction in test tear down (example for NUnit). You can even put setup and tear down method to the base class for all tests and each test will after that run in its own transaction which will rollback at the end of the test and put the database to its initial state.

这篇关于实体框架代码优先-相互重叠的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆