本文介绍了防止 vstest 发现引擎锁定 DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用 DllImport pinvoke 调用 VS2010 c++ DLL 的 VS2012 项目进行了一些 C# 单元测试.

I have some C# unit tests for a VS2012 project which calls a VS2010 c++ DLL using DllImport pinvoke.

作为测试项目的预构建事件,我将最新版本的 DLL 复制到二进制项目进行测试.

As a pre-build event for the test project, I copy the latest version of the DLL to the binary project for the test.

如果 vstest.discoveryengine 正在运行,这会反复失败.似乎发现引擎"正在加载测试并锁定 DLL.

This repeatedly fails if vstest.discoveryengine is running. It appears that the 'discovery engine' is loading the tests and holding the lock on the DLL.

如果我杀死 vstest 发现引擎,那么我可以构建和运行测试.否则构建将失败,并且 VS2012 提供运行以前的版本(带有没有不再显示此消息"选项的模型对话框)

If I kill vstest discovery engine, then I can build and run the tests. otherwise the build fails, and VS2012 offers to run a previous version ( with a model dialog which doesn't have a 'don't show this message again' option)

我可以做些什么来强制测试项目在不实际运行测试时卸载 DLL,或者禁用后台发现可执行文件?

Is there something I can do to either force the test project to unload the DLL when not actually running the tests, or to disable the background discovery executable?

我通过创建一个名为 Kealakekua 的可执行文件来破解一个变通方法,它会杀死 vstest.discoveryengine.x86、vstest.executionengine.x86,并且作为预构建事件的第一部分,它可以复制文件并构建,但不想为我的文件与 Visual Studio 争吵.

I've hacked a workaround by creating an executable called Kealakekua which kills vstest.discoveryengine.x86, vstest.executionengine.x86, and with that as the first part of the pre-build event it can copy the files and build, but would prefer not to be fighting visual studio for my file.

推荐答案

我最近也遇到了这个问题,问题是我自己的用户代码造成的.

I recently also had this issue and the problem was caused by my own user code.

在测试发现期间,所有测试类都被实例化,并且在我们的一个测试类构造函数中,初始化了一个非常复杂的业务类.问题是在它的初始化过程中创建了一个后台线程,执行以下操作:

During test discovery all the test classes are instantiated and in one of our test class constructors, a quite complex business classes was initialized. The problem is that during initialization of it a background thread was created, that did the following:

socket.Read(...)

这个线程一直在运行,等待一些套接字数据到达,结果锁定了我们的程序集.

This thread kept running forever waiting for some socket data to arrive and as a result locked our assembly.

所以我的解决方案是确保在测试发现期间不会调用此代码.

So the solution for me was to make sure this code won't get called during test discovery.

如果您受到此问题的影响,您可以通过在锁定某个程序集时将 Visual Studio 附加到测试发现引擎来检查.按暂停后,您通常会看到,当前执行的行位于您自己的用户代码中(另请检查线程"窗口).

You can check, if you are affected by this issue, by attaching Visual Studio to the test discovery engine when it has locked some assembly. After pressing pause you normally will see, that the current executing line is somewhere in your own user code (also check the Threads window).

这篇关于防止 vstest 发现引擎锁定 DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:58