本文是小编为大家收集整理的关于MSTest & AppDomains的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
在我的某些项目中,我注意到在VSTS2008执行单元测试期间,其Vstesthost的内存消耗增长.由于我的解决方案中有很多测试,因此最终导致了OutofMemroyException. 这对我来说看起来很奇怪,因为我确定MSTest为每个单元测试创建一个新的应用程序.否则,它将如何重置静态字段? 但是,如果为每个测试创建AppDomain,则不应泄漏.但这确实如此.
所以问题是:VS是否应该为每个测试类创建AppDomain?如果是的,那么我该如何检查. 我尝试通过Process Expolorer和Performance Snap-In进行追踪.在测试运行期间,"总应用程序卸载"的值始终为0.
推荐答案
我认为单位测试引擎不会为每个测试创建一个新的应用程序.由于创建AppDomain是一个相对昂贵的操作,因此对于每个测试,都会大大减慢单位测试的执行!
Visual Studio 2008使用称为vstesthost.exe的单独可执行文件来运行单元测试. VS与vstesthost.exe通信(我不知道它是如何做到的),以告诉它要运行什么测试. vstesthost.exe将执行结果返回到显示这些结果的VS.
如果您在运行单元测试时要获得OutofMemoryExceptions,我会说这是一个有力的指标,即您正在测试的代码实际上没有清理所有内容.您确定自己不会保留对未托管的对象/内存的手柄吗?我建议在性能分析下运行单元测试(您可以通过在"测试视图"下找到单元测试,右键单击它,然后选择"创建性能会话"来做到这一点).这可能至少在您的对象分配上散发出一些灯光.
其他推荐答案
mstest会创建每个测试的单应用域汇编,除非您使用噪声,在这种情况下,没有AppDomain隔离.
如果您看到泄漏,则可能是一个,但在您的测试代码或产品代码中.确保您不会把东西塞进字典中,然后将它们留在那里.
其他推荐答案
我对每个Unitest的单独应用程序都是错误的.
这里的证据: 单身
public class Singleton { public static Singleton Instance = new Singleton(); private Guid _token; private Singleton() { _token = Guid.NewGuid(); } public Guid Token { get { return _token; } } }
和两个测试:
[TestClass] public class UnitTest2 { [TestMethod] public void TestMethod1() { Console.WriteLine(Singleton.Instance.Token); } } [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { Console.WriteLine(Singleton.Instance.Token); } }
执行两个测试期间输出相同的GUID.
问题描述
In some my project I notice that during executing unit tests under VSTS2008 its VSTestHost's memory consuming grows. As I have very many tests in my solution it leads to OutOfMemroyException eventually. That looks very strange for me as I was sure that MSTest creates a new AppDomain for each unit test. Otherwise how would it reset static fields? But if AppDomain is being created for each test than memory shouldn't leak. But it does.
So the question is: Should VS create AppDomain for each test class or not? If yes than how can I check that it does it. I tried tracing through ProcessExpolorer and Performance snap-in. A value of "Total appdomain unloaded" is always 0 during test run.
推荐答案
I don't think the unit test engine creates a new AppDomain for each test. Since creating an AppDomain is a relatively expensive operation, doing so for each test would slow down execution of unit tests considerably!
Visual Studio 2008 uses a seperate executable called vstesthost.exe to run unit tests. VS communicates with vstesthost.exe (how it does this I don't know) to tell it what tests to run. vstesthost.exe returns the execution results to VS which displays those results.
If you are getting OutOfMemoryExceptions when running your unit tests I would say that's a strong indicator that your code under test is actually not cleaning things up. Are you sure that you aren't retaining handles to unmanaged objects/memory? I would recommend running your unit tests under a Performance Analysis (you can do that by finding the unit test under the "Test View", right-clicking on it, and selecting "Create Performance Session"). This might shed some light at least on your object allocations.
其他推荐答案
MsTest creates one-app domain per Test assembly, unless you are using noisolation, in which case there is no AppDomain Isolation.
If you are seeing leaks, its probably a but in either your test code, or your product code. Make sure you aren't stuffing things into dictionaries and leaving them there.
其他推荐答案
I was wrong about having separate AppDomains for each unittest.
Here's evidence: a singleton
public class Singleton { public static Singleton Instance = new Singleton(); private Guid _token; private Singleton() { _token = Guid.NewGuid(); } public Guid Token { get { return _token; } } }
and two tests:
[TestClass] public class UnitTest2 { [TestMethod] public void TestMethod1() { Console.WriteLine(Singleton.Instance.Token); } } [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { Console.WriteLine(Singleton.Instance.Token); } }
During executing both tests output the same guid.