简体   繁体   English

我可以强制ClassCleanup与ClassInitialize在同一线程中执行吗?

[英]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.

相关问题 我怎么能强制等待继续在同一个线程? - how can i force await to continue on the same thread? 在等待在同一个线程上执行之后如何获得延续? - How can I get the continuation after an await to execute on the same thread? 无法使用存在的ClassInitialize方法执行类 - Fails to execute classes with ClassInitialize method present MSTest中的ClassCleanup是静态的,但是构建服务器使用nunit来运行单元测试。 我该如何调整? - ClassCleanup in MSTest is static, but the build server uses nunit to run the unit tests. How can i adjust? 如何让C#计时器在创建它的同一个线程上执行? - How can I get a C# timer to execute on the same thread that created it? 如何在WCF线程中强制未处理的异常使进程崩溃? - How can I force unhandled exceptions in a WCF thread to crash the process? 如何强制Resharper将花括号放在同一行上? - How can I force Resharper to place the curly brace on the same line? 如何获取工作线程以在主UI线程上执行回调? - How can I get a worker thread to execute a callback on the main UI thread? 我应该在每个TestMethod中还是在ClassInitialize中设置模拟方法? - Should I setup mock methods inside each TestMethod or inside ClassInitialize? 我有一个类实例,如何强制执行将对象本身作为参数的构造函数 - I have a class instance, how can I force execute its constructor which take object itself as a parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM