简体   繁体   English

如何对一个vsix项目进行单元测试?

[英]How to unit test a vsix project?

I have a Visual Studio Extension project. 我有一个Visual Studio Extension项目。 It works fine, builds into a vsix and does its job. 它工作正常,内置到vsix中并完成工作。 I'm using VS2017 我正在使用VS2017

I'd like to unit test it. 我想对其进行单元测试。 I already wrote unit tests, using MSTest v2. 我已经使用MSTest v2编写了单元测试。

Problem is: When I try to build/run the test project, there's an error that it didn't find the myProject.dll, obviously because there isn't one (myProject builds to a .vsix file). 问题是:当我尝试构建/运行测试项目时,出现一个错误,即找不到myProject.dll,这显然是因为没有一个错误(myProject构建为.vsix文件)。

How do I unit test my vsix project? 如何对我的vsix项目进行单元测试?

Edit: Debugging it works fine. 编辑:调试它工作正常。
Edit 2: The whole output is the vsmanifest file and a .vsix file. 编辑2:整个输出是vsmanifest文件和一个.vsix文件。 Installing and running works. 安装并运行作品。

Tl;dr of the answer: Can't test a vsix. 答案的Tl; dr:无法测试vsix。

Your test project should be a DLL type of file. 您的测试项目应为DLL类型的文件。 After you will change it to "CS" type of file for example, the runner will be able to run your test 例如,将其更改为“ CS”类型的文件后,跑步者将能够运行您的测试

So I just stumbled across this post. 所以我偶然发现了这篇文章。 The problem is the .dll and .pdb files are inside the vsix package. 问题是.dll和.pdb文件位于vsix软件包中。 So to Unit Test, they need to be also outside of it. 因此,对于单元测试,它们也必须不在此范围之内。

This indicates a bug in the imported target in the .csproj file: 这表明.csproj文件中导入的目标存在错误:

<Import Project="$(VSToolsPath)\VSSDK\Microsoft.VsSDK.targets" Condition="'$(VSToolsPath)' != ''" />

This target is removing the dll and pdb as it builds the vsix. 此目标是在构建vsix时删除dll和pdb。 It shouldn't do that. 它不应该那样做。 Now, the vsix is simply a zip file with a special name. 现在,vsix只是一个带有特殊名称的zip文件。 So we can unzip it. 因此,我们可以将其解压缩。

I added 7z.exe to my solution in a tools folder, and run this in the post build actions: 我在工具文件夹中的解决方案中添加了7z.exe,并在构建后的操作中运行它:

"$(SolutionDir)Tools\7z.exe" x -aoa "$(TargetDir)$(TargetName).vsix"

Now my unit tests work fine. 现在我的单元测试工作正常。

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

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