测试与测试应用程序域 [英] MSTest & AppDomains

查看:76
本文介绍了测试与测试应用程序域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的某些项目中,我注意到在VSTS2008下执行单元测试期间,其VSTestHost的内存消耗增加了.由于我的解决方案中有很多测试,因此最终会导致OutOfMemroyException. 这对我来说似乎很奇怪,因为我确信MSTest会为每个单元测试创​​建一个新的AppDomain.否则它将如何重置静态字段? 但是,如果为每个测试都创建AppDomain,则不应泄漏内存.但确实如此.

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.

所以问题是:VS是否应该为每个测试类创建AppDomain?如果是,那我该如何检查呢. 我尝试通过ProcessExpolorer和Performance管理单元进行跟踪.在测试运行期间,总的应用域已卸载"的值始终为0.

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.

推荐答案

我不认为单元测试引擎会为每个测试创建一个新的AppDomain.由于创建AppDomain是一项相对昂贵的操作,因此对每个测试这样做都会大大减慢单元测试的执行速度!

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使用一个名为vstesthost.exe的单独可执行文件来运行单元测试. VS与vstesthost.exe通讯(我不知道该怎么做),以告诉它要运行哪些测试. vstesthost.exe将执行结果返回给VS,VS显示这些结果.

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.

如果在运行单元测试时遇到OutOfMemoryExceptions的问题,那可以说是一个有力的指标,表明您的被测试代码实际上并未清除所有内容.您确定不保留非托管对象/内存的句柄吗?我建议您在性能分析下运行单元测试(您可以通过在测试视图"下找到单元测试,右键单击它,然后选择创建性能会话"来进行此操作).至少可以对您的对象分配有所了解.

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.

这篇关于测试与测试应用程序域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆