我有一个 jenkins 构建,它可以清理、恢复,然后构建一个单元测试项目。这个项目以及我的其他项目最近已迁移到使用PackageReference
. 清洁工作,恢复工作。我验证了 NUnit 存在于%userprofile%/.nuget/packages/
. 以下是我的构建脚本中的命令:
msbuild /p:Configuration="Debug" /t:clean OtherProjectName.Test.csproj
msbuild /p:Configuration="Debug" /t:restore OtherProjectName.Test.csproj
msbuild /p:Configuration="Debug" OtherProjectName.Test.csproj
这稍后会产生如下所示的输出:
csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /define:DEBUG;TRACE /highentropyva+
/reference:<one of my references> /reference:<another one> /reference:<and so on>
/out:obj\Debug\OtherProjectName.Test.dll /subsystemversion:6.00 /target:library /utf8output <a list of source files>
我的项目的构建失败,因为它找不到 NUnit(见下文)。我相信它找不到 NUnit,因为编译命令不包含对 NUnit nuget 包的“/reference”。
error CS0246: The type or namespace name 'NUnit' could not be found (are you missing a using directive or an assembly reference?
我的其他项目确实有 NuGet 包的“/reference”。所以我比较了它们,看看是否有什么明显不同。我确实注意到我的大多数项目一开始都包含这一行:
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
我认为这可能是罪魁祸首,基于我对这条线的简短阅读,但是当我在本地机器上尝试相同的步骤时(它有 VS2017 和 MSbuild 15,就像 jenkins 服务器一样)我无法复制这个问题.
我的.csproj
文件有以下内容:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{a guid}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>OtherProjectName.Test</RootNamespace>
<AssemblyName>OtherProjectName.Test</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<!-- a couple entries like this -->
</PropertyGroup>
<ItemGroup>
<Reference Include="log4net">
<HintPath>Z:\path\to\log4net.dll</HintPath>
</Reference>
<!-- a couple references like this -->
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
</ItemGroup>
</When>
<Otherwise />
</Choose>
<ItemGroup>
<!-- all my C# source files -->
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OtherProjectName\OtherProjectName.csproj">
<Project>{a guid}</Project>
<Name>OtherProjectName</Name>
</ProjectReference>
<ProjectReference Include="..\OtherProjectName2\OtherProjectName2.csproj">
<Project>{another guid}</Project>
<Name>OtherProjectName2</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="App.config.1" />
<None Include="Unit Test Playlists\Fast.playlist" />
<!-- all my unit test playlists -->
</ItemGroup>
<ItemGroup>
<PackageReference Include="NUnit">
<Version>3.11.0</Version>
</PackageReference>
<PackageReference Include="NUnit.ConsoleRunner">
<Version>3.9.0</Version>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter">
<Version>3.11.0</Version>
</PackageReference>
<!-- other nuget packages -->
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
</ItemGroup>
</When>
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>