取消测试后清理 [英] Clean Up after Canceling tests

查看:115
本文介绍了取消测试后清理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过Visual Studio运行测试.在运行所有测试之前,我会自动创建一组具有已知凭据的用户,并在运行结束时删除这些用户.但是,有时我需要中途取消测试.在这些情况下,测试永远没有机会进行清理,这意味着测试运行中会遗留伪造的用户信息,并且可能导致下一次测试运行崩溃(当尝试将用户信息添加到数据库中时).无论如何,即使取消了测试,是否也可以强制Visual Studio/mstest运行清理方法?

I'm currently running tests through visual studio. Before all the tests are run, I automatically create a set number of users with know credentials, and at the end of the run I delete those users. However, sometimes I need to cancel my tests midway. In these cases the test never gets the chance to clean up, this means that there is left over fake user info from the test run and may causes the next test run to crash (when it attempts to add user info into the DB). Is there anyway to force visual studio/mstest to run a clean up method even if the test is canceled?

我知道一种选择是进行测试检查,并确保用户信息尚不存在,以及是否确实在创建新用户之前将其删除.但这仍然无法解决取消测试运行并留下不需要的测试数据的问题.

I know one option is to have the test check and make sure that the user info doesn't already exist, and if it does remove it before creating the new users. But this still wouldn't solve the issue of the canceled test run leaving unwanted test data.

更新:

对不起,沟通不畅,但是在测试开始时清除数据是不可行的.我给出的问题非常简单,但简单地说,我没有一种简单的方法来确保在测试开始时不存在任何测试数据.所有清理必须在测试结束时进行.

Sorry for the miscommunication, however cleaning up the data at the start of the test is not an option. I'm giving a very simplistic view of the issue, but put simply, I have no easy way of making sure that no test data exists at the start of the test. All clean up must occur at the end of the test.

推荐答案

那是不可能的.您最好找到一个替代解决方案,例如使用单独的数据库进行测试并在每次测试运行之前清除所有数据,使用一组固定的测试用户或用一些标记标记测试数据.检查在集成测试中隔离数据库数据 Jimmy Bogard的文章.

That is impossible. You better find an alternative solution like using separate database for testing and clean all data before each test run, using fixed set of test users or mark test data with some flag. Check Isolating database data in integration tests article by Jimmy Bogard.

没有内置方法可以更改MSTest默认行为.从理论上讲,您可以编写利用的MSTest扩展. TestExecution.OnTestStopping 事件,但这不是一个容易的过程,它需要更改注册表.而且,很多人抱怨它不起作用.

There is no built-in way to change MSTest default behavior. In theory you can write MSTest extension that utilizes TestExecution.OnTestStopping event, but that is not an easy process and it requires registry change. Moreover, a lot of people complain that it is not working.

还有MSTest V2,这是MSTest的新版本,具有新的可扩展性点.但是看来您不能用这一点来更改取消行为,只能编写属性装饰器.请参阅扩展MSTest V2 .

There also MSTest V2, a new version of MSTest with new extensibility points. But it looks like you can't alter cancel behavior with this points, only write attribute decorators. See Extending MSTest V2.

您不能使用AppDomain.CurrentDomain.ProcessExitProcess.GetCurrentProcess().Exited事件,因为取消似乎终止了测试运行过程.

You can't use AppDomain.CurrentDomain.ProcessExit and Process.GetCurrentProcess().Exited events because cancel seems to kill test run process.

NUnit目前还不支持此功能.请参见相关的NUnit测试适配器在VS上运行拆解取消测试运行问题.

NUnit also doesn't support this at the moment. See related NUnit test adapter Run TearDowns on VS Cancel Test Run issue.

这篇关于取消测试后清理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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