How to host VS IDE during integration test via MSTest?

一个人想着一个人 提交于 2020-01-03 02:56:38

问题


I have created an integration test project to test VS Extension. If I run the tests from the Visual Studio IDE, all tests are running just fine and every method spawns a new VS IDE. The test methods are marked with the following attributes:

[HostType("VS IDE")]
[TestMethod]
public void TestWhateverMethod() { ... }

However if I try to automate the tests, and run them from commandline via MSTest (or VSTest) I got the following error message, for the tests that are hosted inside the VS IDE:

The host type 'VS IDE' cannot be loaded for the following reason: The key 'VS IDE' cannot be found. Make sure that the appropriate host adapter is installed on the machine.

Therefore I tried to find the solution at: MSDN - How to: Install a Host Adapter. But it is only documented for VS2005 and 2008.

I would like to ask for directions regarding VS 2013, where can I found out more? Or what am I missing? Which is the proper way to run integration tests from outside the VS IDE? How one can host an IDE programmatically?

Thank you in advance!


回答1:


I realized how to do it, without using the HostType attribute. Hopefully the following code snippet can help out others as well:

Type visualStudioType = Type.GetTypeFromProgID("VisualStudio.DTE.12.0", true);    
DTE env = Activator.CreateInstance(visualStudioType, true) as DTE;

This will get the type of the VS, with the specified version and will throw an exception on error. The DTE will be the interface of the EnvDTE, that one can use.

To get services, do the following, e.g. to get the UI Shell:

ServiceProvider serviceProvider = new ServiceProvider(env as 
Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
IVsUIShell uiShell = (IVsUIShell)serviceProvider.GetService(typeof(SVsUIShell));

One should keep track of the services, get the rcw handle and properly release the COM object afterwards the usage.



来源:https://stackoverflow.com/questions/29510010/how-to-host-vs-ide-during-integration-test-via-mstest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!