[英]Can I force ClassCleanup to execute in same thread as ClassInitialize?
In the code snippet below I'm using ClassInitialize and Cleanup to wrap transactions around a series of tests I'm running. 在下面的代码片段中,我正在使用ClassInitialize和Cleanup将事务包装在正在运行的一系列测试周围。 The code works in my GUI test runner but fails in mstest command line with the below error. 该代码在我的GUI测试运行程序中有效,但在mstest命令行中失败,并显示以下错误。 Am I able to force Class cleanup and initialize to be in same thread? 我可以强制进行类清理并将其初始化为同一线程吗? Prefer not to use Test initialize and cleanup as it will slow the test significantly. 最好不要使用测试初始化和清理,因为这会大大降低测试速度。
CODE 码
private static TransactionScope _trans;
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
_trans = new TransactionScope();
//...do work
}
[ClassCleanup]
public static void ClassCleanup()
{
_trans.Dispose();
}
ERROR 错误
Run has the following issue(s): Class Cleanup method GeoMetExplainSheetTests.TestCleanup failed. 运行存在以下问题:类清除方法GeoMetExplainSheetTests.TestCleanup失败。 Error Message: System.InvalidOperationException: A TransactionScope must be disposed on the same thread that it was created.. 错误消息:System.InvalidOperationException:必须将TransactionScope放在创建它的同一线程上。
Stack Trace: 堆栈跟踪:
at System.Transactions.TransactionScope.Dispose() 在System.Transactions.TransactionScope.Dispose()
我建议您通过删除static modifier
来替换
private TransactionScope _trans;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.