简体   繁体   English

如何为Visual Studio插件编写单元测试?

[英]how to write a unit test for a Visual Studio plugin?

Some of the plugin's methods for which I'd like to write unit tests accept COM objects passed directly by Visual Studio. 我想编写单元测试的一些插件的方法接受Visual Studio直接传递的COM对象。 For example: 例如:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
   DTE2 d = (DTE2)application;
   ...
}

Because these objects are not serializable, I can't have unit test code read them from disk. 因为这些对象不可序列化,所以我不能让单元测试代码从磁盘读取它们。

Other than creating instances of custom classes that implement the same COM-related interfaces and that have properties that return meaningless/dubious data, is there a way to test this can kind of method without actually launching Visual Studio? 除了创建实现相同的COM相关接口并且具有返回无意义/可疑数据的属性的自定义类的实例之外,有没有办法在不实际启动Visual Studio的情况下测试这种方法?

I usually find harder to Unit Test these type of code. 我通常发现单元测试这些类型的代码更难。 This is mainly because, as you said, hard dependencies with COM-related interfaces. 这主要是因为,正如您所说,与COM相关的接口的硬依赖性。

You might be able to abstract away some of the dependencies, and test those types in isolation. 您可能能够抽象出一些依赖项,并单独测试这些类型。 See some samples see here . 看一些样品见这里

Personally I would not recommend writing Unit Tests against these types of methods. 就个人而言,我不建议针对这些类型的方法编写单元测试。 It is more of an entry point to the Plugin and it basically orchestrate calls. 它更像是插件的入口点,它基本上编排了调用。 You probably don't get much value Unit Testing this method. 您可能没有太多价值单元测试此方法。

Below are some that might help you in the direction of writing tests using DTE instance 下面是一些可能帮助您使用DTE实例编写测试的方法

DTE and Unit Testing DTE和单元测试

How to Unit Test Visual Studio AddIn interacting with VS DOM 如何单元测试Visual Studio AddIn与VS DOM交互

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM