问题
I have created a C# test project in VS2015 RC. it builds locally but when i attempt to build on our CI build server (TeamCity) it fails with errors:
UnitTest1.cs(2,17): error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) [D:\BuildAgent\work\e486bf18e454d0c2\dh.PSP.Coordinator.Api.Tests\dh.PSP.MetadataService.Api.Tests.csproj] UnitTest1.cs(9,10): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [D:\BuildAgent\work\e486bf18e454d0c2\dh.PSP.Coordinator.Api.Tests\dh.PSP.MetadataService.Api.Tests.csproj] UnitTest1.cs(9,10): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [D:\BuildAgent\work\e486bf18e454d0c2\dh.PSP.Coordinator.Api.Tests\dh.PSP.MetadataService.Api.Tests.csproj] UnitTest1.cs(6,6): error CS0246: The type or namespace name 'TestClass' could not be found (are you missing a using directive or an assembly reference?) [D:\BuildAgent\work\e486bf18e454d0c2\dh.PSP.Coordinator.Api.Tests\dh.PSP.MetadataService.Api.Tests.csproj] UnitTest1.cs(6,6): error CS0246: The type or namespace name 'TestClassAttribute' could not be found (are you missing a using directive or an assembly reference?) [D:\BuildAgent\work\e486bf18e454d0c2\dh.PSP.Coordinator.Api.Tests\dh.PSP.MetadataService.Api.Tests.csproj]
Clearly this is because the assembly containing these namespaces (Microsoft.VisualStudio.QualityTools.UnitTestFramework) is not on the build server, on my local machine it resides at C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll. I guess I could copy the assembly into my solution so that it becomes part of the codebase but manually moving files feels like a bit of an inelegant hack. I searched around on nuget and found http://www.nuget.org/packages/Microsoft.VisualStudio.QualityTools.UnitTestFramework/ which I figured would do the trick, but installing that package failed with:
Install-Package : Could not install package 'Microsoft.VisualStudio.QualityTools.UnitTestFramework 11.0.50727.1'. You are trying to install this package into a project that targets '.NETFramework, Version=v4.5.2', but the package does not contain any assembly references or content files that are compatible with that framework
What's my best option of solving this? I'm surprised that creating a test project in VS2015 does not automatically include all the dependencies that I need, though perhaps I'm being naive (I'm something of a fledgling dot netter).
回答1:
Hmm I have some ideas, so choose the one that best fits your needs
- A simple answer should be mark the DLL to copy local and use a folder like Assemblies in the same folder of the solution and references "Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll"
- Install Visual Studio in your build server. Sounds nuts but it's the closest to "developer machine" that you have.
- Install the DLL in the GAC so you don't have to bother with this.
- Fix the NuGet package (Adding a reference for the .NET Framework version) and use it.
- Downgrade your .NET Framework version so you can use the NuGet package.
- Create your own NuGet server! (and add the reference of the DLLs you need).
IMHO I'd choose the first answer, because it seems to be the "best way" to use NuGet to resolve all your packages problems but you are using a DLL that you don't know if it should be trusted.
In system used in "old" languages like C, or C++ it's common you download the source code and the libraries needed for the code to run so I do not think the NuGet package it's the best solution.
Using the first option you always have the same version and could check the MD5 of the file and know exactly what is running in your build server.
Maybe the real best option should be 6. When you use your own NuGet server to handle your DLLs making your live more awesome and trustable.
回答2:
The answer is similar to option 1 in eng.augusto's answer.
Microsoft doesn't provide NuGet for the latest version of Microsoft.VisualStudio.QualityTools.UnitTestFramework,
but supply it as a part of Visual Studio
(normally at C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)
I created the folder Microsoft.VisualStudio.QualityTools as a subfolder of my solution and copied
Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll Microsoft.VisualStudio.QualityTools.UnitTestFramework.xml
The files should be added to source control(even if DLLs are usually ignored).
Then I changed references in my Test.csproj to refer to a new location.
回答3:
For projects created in VS 2017. Adding Nuget package Microsoft.VisualStudio.QualityTools.UnitTestFramework.Updated allows to build unit test projects on CI without VS installed on build server:
回答4:
I was having this issue when trying to use MSBuild on our dev server via our CI/CD process after I was asked to uninstall VS2013 from our dev server by our IT team.
In my case in my build output there were a few lines with the word Considered. What this means is that the build is considering those folders for locations where the file may be located. One of those lines was as follows:
Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll", but it didn't exist.
I copied Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll from my local machine to that folder on the dev server and the error went away.
来源:https://stackoverflow.com/questions/30595283/reference-microsoft-visualstudio-qualitytools-unittestframework-for-ci-build