在什么情况下都[ClassCleanup]和[TestCleanup]无法运行 [英] Under what circumstances are [ClassCleanup] and [TestCleanup] not run

查看:671
本文介绍了在什么情况下都[ClassCleanup]和[TestCleanup]无法运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组使用这些属性的方法,从创建的测试数据库表中删除所有行的测试开始后,MSTest的测试案例;每过一段时间,我结束了从单元测试仍然在DB的一个行。

I have a set of MSTest test cases that use methods with those attributes to delete all rows from tables in the test db created after the tests are started; every once in a while I end up with a row from a one of the unit tests still in the DB.

由于我节省了测试开始时间和删除所有时间戳记录比它更近,我可以看到记录幸存的唯一方法是,如果清理代码没有被调用。

Since I'm saving the test start time and deleting all records with timestamps more recent than it, the only way I can see for the records surviving is if the cleanup code wasn't called.

推荐答案

嗯,首先,一个单元测试在技术上不应该接触DB。但是,我用NUnit的为我的集成测试过,所以根本不算什么。

Well, first off, a "unit test" technically shouldn't touch the DB. But, I use NUnit for my integration tests too, so no biggie.

只有在何种情况下清理方法将不能完整运行的是如果有一个在清理功能,或异常错误引起的运行时关闭。唯一的,我知道能做到这一点的堆栈溢出,并超出内存错误。中止从调试器的测试也将导致跳过清除阶段。

The only circumstances in which the cleanup methods wouldn't run in their entirety is if there were an error in the cleanup function, or an exception caused the runtime to shut down. The only ones that I know can do that are stack overflow and out-of-memory errors. Aborting a test from the debugger will also cause the cleanup phase to be skipped.

一个更可能的解释,这取决于你的数据访问层,是你失去了你的数据库连接和清理功能不能得到一个新的。这通常会表明需要让你的DAL更加强劲,也预示着你不期待着什么在测试发生导致DAL打破。

A more likely explanation, depending on your data access layer, is that you lost your DB connection and the cleanup function couldn't get a new one. That would generally indicate a need to make your DAL more robust, and also indicates that something you're not expecting is happening in the test to cause the DAL to break down.

这篇关于在什么情况下都[ClassCleanup]和[TestCleanup]无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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