有没有办法让 Visual Studio 2010 在同一线程上运行所有测试?
即使 Visual Studio 2010 不在多个并行线程中运行测试,它仍然使用不同的线程来运行不同的测试方法。它使用一个线程运行一个测试,然后切换到另一个线程来运行其他测试。它继续在每个测试方法的线程之间切换。您可以通过查询不同测试中的线程 ID 来轻松测试它。
我正在尝试编写集成测试来初始化使用 COM 对象的实际应用程序。这些 COM 对象必须仅在 STA 内存模型中使用,并且没有可用于从其他线程调用它们的代理/存根封送拆收器。
应用程序的 COM 对象在第一次测试期间对第一次测试使用的线程进行初始化。然后,从其他测试对它们的任何调用都会失败,因为它们是从不同的线程调用它们。它抛出 InvalidComObjectException 并显示“无法使用已与其底层 RCW 分离的 COM 对象”,因为它无法到达其他 STA 单元中且没有代理/存根封送拆收器的 COM 对象。
让 Visual Studio 在同一线程上运行所有测试将解决问题,因为所有 COM 对象都将在同一 STA 单元内的同一线程上初始化和使用。
Even when Visual Studio 2010 does not run tests in multiple parallel threads, it still uses different threads to run different test methods. It uses one thread to run one test, and then switches to other thread to run other test. It continues switching between threads for every test method. You can easily test it by querying thread IDs within different tests.
I’m trying to write integration tests that initialize an actual application that uses COM objects. Those COM objects has to be using only in STA memory model and don’t have proxy/stub marshalers that can be used to call them from other thread.
An application's COM objects get initialized during the first test on the thread that was used by the first test. Then any call to them from other tests fails because they are calling them from different threads. It throws InvalidComObjectException with the "COM object that has been separated from its underlying RCW can not be used", because it cannot reach a COM object that is in other STA apartment and does not have proxy/stub marshaler.
Making Visual Studio to run all tests on the same thread will solve a problem, because all COM objects will be initialized and used on the same thread from within the same STA apartment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用 NUnit 进行此特定测试,因为它 确实运行所有测试在同一个线程上。
You could try using NUnit for this specific test, as it does run all tests on the same thread.