从命令行执行msbuild:引用的命名空间'Microsoft.VisualStudio.TestTools'出现错误

7
我用以下代码行使用MSBuild编译一个.NET 3.5解决方案:
msbuild.exe n:\temp\Thisnight.sln /p:Configuration=Debug /v:diag

并且它给了我这个错误:

 error CS0234: The type or namespace name 'UnitTesting' does not exist in the namespace 'Microsoft.VisualStudio.TestTools' (are you missing an assembly reference?)

我已经获取了最新的源代码,当然可以在VS2010中构建。
似乎我无法弄清楚我缺少什么...
编辑
在命令窗口中向上查看,看到以下消息:
  C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Common.targets : warning MSB3245: Could not resolve this reference.
 Could not locate the assembly "Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutra
l, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.

我认为我不需要将这个DLL包含在我的项目中(因为它是一个微软的DLL?)

编辑 现在我看到的是:所有的项目都是针对.Net 3.5框架的,但测试项目(给我带来问题的那个)是针对.Net 4.0框架的。 而奇怪的是:我无法更改它。

这是一个使用vs2010解决方案的Web服务、WinForms、DLL、安装和测试项目。


你能详细说明一下你的解决方案吗?目前的信息有点难以尝试复现。 - Sayed Ibrahim Hashimi
这是一个包含16个项目的vs2010解决方案,其中包括web、webservice、winforms和dll。 - Michel
3个回答

2

你在命令提示符中运行了vcvars32.bat脚本吗?


顺便说一下:这是在开发机上,所以我已经安装了VS2010。 - Michel
不太确定。尝试在该机器上使用VS打开解决方案,看看IDE是否能够识别引用选项卡中的引用。您是如何引用此DLL的?您可以尝试删除引用并将其重新添加到项目中。VS运行msbuild来构建项目,因此它必须是进程启动环境中微妙的差异。另外,您确定它是相同类型的VS2010安装吗?测试环境在基本版本中不可用,仅在某些版本(如专业版和测试版)中可用。 - Marco
它在我的开发机器上,所以我正在运行msbuild的机器是我开发解决方案的同一台机器。并且dll是通过简单的文件、新项目、测试项目创建后被项目引用的。但我会尝试删除并重新添加它们,也许这有帮助。 - Michel
我尝试使用 /p:AddModules 和 /p:AssemblySearchPaths 参数将其添加到 msbuild 中,但不幸的是,没有起作用。 - Michel
我无法确定在编译我的解决方案时,VS执行的MSbuild命令在哪里查找。 - Michel
显示剩余4条评论

1
如果你查看Microsoft.Common.Targets文件,你会看到以下内容:
<!--
    The SearchPaths property is set to find assemblies in the following order:

        (1) Files from current project - indicated by {CandidateAssemblyFiles}
        (2) $(ReferencePath) - the reference path property, which comes from the .USER file.
        (3) The hintpath from the referenced item itself, indicated by {HintPathFromItem}.
        (4) The directory of MSBuild's "target" runtime from GetFrameworkPath.
            The "target" runtime folder is the folder of the runtime that MSBuild is a part of.
        (5) Registered assembly folders, indicated by {Registry:*,*,*}
        (6) Legacy registered assembly folders, indicated by {AssemblyFolders}
        (7) Resolve to the GAC.
        (8) Treat the reference's Include as if it were a real file name.
        (9) Look in the application's output folder (like bin\debug)
    -->

在VS中,它在自己的环境中运行MSBuild,我也不知道其中的区别,但您是否尝试通过修改AssemblyFolders项在命令行中添加缺少的程序集路径。您可以在这里找到教程:http://www.beefycode.com/post/Resolving-Binary-References-in-MSBuild.aspx

希望这可以帮助到您。


不确定您是否可以在命令行中覆盖AssemblySearchPaths。你试过像教程里展示的那样添加一个自定义目标吗?否则你可以直接修改Microsoft.Common.Targets,但我不认为这是一个好的解决方案... - Benjamin Baumann
没能让它正常工作,希望有一天有人能读到这篇文章并给我一个解决方案。之前没有想到它会如此困难。已经花了超过6个小时在上面,但它并不重要,所以不打算继续了。还是将BB的回答标记为最佳答案,感谢他的努力。 - Michel
我忘记了这个问题。我目前也面临着同样的问题,我认为你不能在VS之外(即使用MsBuild)运行VS Unit测试。缺少的dll位于VS10\Common7\IDE目录中,即使我引用正确,我也无法在VS之外使其工作。也许它使用了MsTest或需要一些VS的东西才能工作。我将此问题标记为收藏,以查看是否有更好的答案。 - Benjamin Baumann

1

尝试运行:

msbuild /version

确保它是3.5版本,而不是旧版本。

确保MS dll在这里或类似位置:

C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接