使用 Specflow 进行验收测试中的数据库清理
我是 tdd 的新手。我看过布兰登·萨特罗姆的视频。我正在尝试实现像它们这样的测试,用于验收测试的外循环和用于单元测试的内循环。我认为验收测试也是对数据库不利的。所以我希望在 Specflow 中找到有关用于数据库清理的 [BeginScenario/AfterScenario] 事件的示例。据说它用于数据库清理。但我看到的例子都没有做到这一点。
我是否误解了验收测试的概念?它不也涵盖数据库吗?我们应该像在单元测试中那样使用模拟对象吗?
I am a newbie in tdd. I have watched Brandon Satrom's videos. I am trying to implement tests like them ,outer loop for acceptance tests and inner loop for unit tests. I have thought acceptance test was againist to Database ,too.So i expect to find examples about [BeginScenario/AfterScenario] events for database clean up in Specflow.It is said to be used for database Clean up. But None of the examples i saw do it.
Am i misundestanding the acceptance test concept? Doesn't it cover the database too? Should we use mock objects there like we did in unit tests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在集成单元测试 (MSTest) 中使用真正的 MS SQL Server 数据库,并通过 BDD 工具 SpecFlow 进行验收测试,方法如下:我将测试数据库(MDF/LDF 文件)的转储存储为模板。在测试初始化时,我将它们复制到临时位置,使用 sp_attach_db 存储过程将它们附加到专用 SQL Server(您可以为此使用 Express 版本),然后运行我想要的任何测试代码,并在测试清理时分离测试数据库并删除 MDF/LDF 文件。整个复制/附加/分离/删除周期非常快(至少比我之前想象的要快得多)。
如果你有兴趣,我可以在我的博客上写更多的文字。
I'm using a real MS SQL Server database in my integration unit tests (MSTest) and acceptance testing with BDD tool SpecFlow in this way: I have a dump of my test database (MDF/LDF files) stored as a template. On test initialize I copy them to a temporary location, attach them to a dedicated SQL Server using sp_attach_db stored procedure (you may use an Express edition for this), then I run whatever test code I want and on test cleanup I detach the test database and delete the MDF/LDF files. The whole copy/attach/detach/delete cycle is pretty fast (at least much faster than I thought before).
If you're interested, I could put it into some more words on my blog.
最后我确信我必须在验收测试中使用真实的数据库。在我把它放在心上之前,我必须看到一些例子,并从多种资源中阅读它。
现在我正在使用验收测试来测试用户界面和数据库的流程。
我为我的注册页面编写了一个快乐路径场景来设计页面流程。然后我编写了一些保留在数据库存储过程中的逻辑测试。其他逻辑位于控制器和模型类上。所以对于他们我使用了单元测试。现在它对我来说更有意义,直到我下一次对 tdd 感到困惑:)。
至于清理过程,我使用 [BeginScenario/AfterScenario] 事件。在 BeginScenario 中,我使用全局变量来保留 DateTime.Now.Ticks 值并将其合并到我发送到数据库的值的开头。然后,当我在 AfterScenario 事件中对该场景进行清理时,我找到以此 DateTime.Now.Ticks 值开头的记录。所以它帮助我创建了不干扰其他记录的独特值。现在看来已经起作用了。
At last i am convinced that i must use the real database in my acceptance tests. I have to see some examples, and read it from several resources before i settle it in my mind.
Now i am using acceptence test as supposed for testing the flow of my user interfaces and database.
i wrote a happy path scenerio for my registration page to design page flow. then i wrote some test for logic that kept in my stored procedures in database. Other logic is on controllers and model classes. So for them i used unit tests. Now it makes more sense to me, until my next confusion about tdd :).
As for clean up process, i use [BeginScenario/AfterScenario] events. At BeginScenario i use a global varible to keep a DateTime.Now.Ticks value and merge it in beginnigs of the values that i sent to db. Then i find the records that start with this DateTime.Now.Ticks value when i making the clean up for that scenario at AfterScenario event. So it helped me to make unique values that doesnt interfere with other records. It seemed to work by now.
关于此事,这篇文章,非常有帮助。
它描述了 MSDTC 中事务的使用,从 BeginScenario 开始并在 AfterScenario 回滚。
(本文中未使用 SpecFlow,但其概念相同)
我们目前正在一个中型开发项目中成功使用此技术。
Regarding this matter, this article, is very helpful.
It describes the use of transactions in MSDTC, starting at BeginScenario and rolled back at AfterScenario.
(SpecFlow is not used in the article, but its the same concept)
We are currently using this technique with success in a mid scale development project.