集成(硒)测试后回滚数据库 [英] Rollback database after integration (Selenium) tests

查看:111
本文介绍了集成(硒)测试后回滚数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人对从集成测试框架(如Selenium)进行回滚数据库事务的最佳实践或首选方式有任何建议吗?

Does anyone have any suggestions for a best practice or preferred way of rolling back database transactions made from an integration test framework such as Selenium?

这是我们目前的情况:我们有一个.net Web项目,其中包含许多在单元测试环境中可以正常工作的单元测试-每个测试都继承一个父类,该父类在[SetUp]中打开一个事务,并在[TearDown]中回滚该事务。每次测试后,我们的单元测试数据库都恢复到原始状态。

Here is our current situation: We have a .net web project with a number of unit tests which work fine in our unit test environment - each test inherits a parent class which opens a transaction in the [SetUp], and rolls back the transaction in the [TearDown]. After each test, our unit test database is restored back to the original state.

但是,一旦进入集成环境,情况就会发生变化。我们的持续集成服务器会自动编译我们的提交并将其提交到测试服务器,以便该服务器始终以最新代码运行。我们还设置了一个Selenium实例来自动化用户与网站的交互。硒测试基本上与现有的Selenium服务器通信,并告诉服务器诸如启动浏览器并转到 http:// testsite / TestPage.aspx -在ID为'def'的表单字段中输入文本'abc'-断言新页面包含文本'xyz'

However, things change once we get to our integration environment. Our continuous integration server automatically compiles our commits and pushes them out to a test server, so that the server always runs on the most up to date code. We've also setup a Selenium instance to automate user interaction with the site. The selenium tests basically communicate with an existing Selenium server, and tell the server things like, "Launch a browser and go to http://testsite/TestPage.aspx - enter text 'abc' into form field with id 'def' - assert new page contains text 'xyz'"

每次测试的运行方式与我们的原始单元测试类似,但是有一个重要的例外:Selenium所做的任何更改都是在完全不同的线程/应用程序中完成的,因此我们不能(我*至少不能这样做) )在测试拆解中将它们回滚。

Each test is run in a similar manner to our vanilla unit tests, but with an important exception: any changes made by Selenium are done in a completely different thread/application, and therefore we can't (I *think we can't, at least) roll them back in the test teardown.

我们还没有一个好的解决方案。现在,我们正在使用SqlCommand执行sql语句来备份数据库的步骤,然后在测试结束时,将数据库设置为单用户,删除当前数据库,并还原旧副本-这不理想,因为这会有效地杀死附加到数据库的应用程序,并要求我们再次重新初始化该应用程序。

We've yet to come to a good solution for this. Right now we're at a point where we're using a SqlCommand to execute a sql statement to backup the database, then at the end of the test, we're setting the database to single user, dropping the current db, and restoring the old copy - this is less than ideal, because that effectively kills the application that was attached to the DB, and requires us to yet again re-initialize the app.

这是以前已经解决的问题吗?

Is this a problem that has been solved before? Any advice would be awesome.

谢谢!

推荐答案

在每次测试之前运行drop / create-table脚本。

We are running a drop/create-table script before every test. This is quite fast and ensures that nothing is left from previous tests.

PS:我们正在使用NHibernate,它可以动态创建此脚本并在Sqlite中运行测试。内存,这是光速。但是,如果我们切换到SqlServer,它仍然非常快。

PS: We are using NHibernate, which creates this script on the fly and run the test on Sqlite in memory, it's lightspeed. But if we switch to SqlServer it's still quite fast.

这篇关于集成(硒)测试后回滚数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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