我正在将 NUnit 测试项目添加到现有解决方案中。测试项目 .csproj 文件如下所示:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyProject_VM\MyProject_VM.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="CommonLibrary">
<HintPath>..\..\CommonLibrary\bin\Debug\CommonLibrary.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
</Project>
我希望它能够测试
MyProject_VM
上的类和方法,定义如下:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{5FE6CC92-763A-4FEA-9389-BB4432C9ACEA}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MyProject_VM</RootNamespace>
<AssemblyName>MyProject_VM</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="CommonLibrary">
<HintPath>..\..\CommonLibrary\bin\Debug\CommonLibrary.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
</ItemGroup>
<ItemGroup>
<Compile Include="CoreClass.cs" />
<Compile Include="MyNeatClass.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
(为了简洁起见,我删除了
<Reference/>
和 <Compile/>
元素。)
到目前为止,我的测试项目只有两个类。
TestsArchitecture
进行一般设置/拆卸:
using MyProject_VM;
namespace TestMyProject
{
[SetUpFixture]
public class TestsArchitecture
{
[OneTimeSetUp]
public void SetsUpStuff()
{
CoreClass.Instance = new CoreClass();
}
[OneTimeTearDown]
public void TearsDownStuff()
{
// Something, at some point
}
}
}
CoreClass
使用 CommonLibrary
。
并且
TestMyNeatClass
测试MyNeatClass
中的方法:
namespace TestMyProject
{
[TestFixture]
public class TestMyNeatClass
{
[OneTimeSetUp]
public void SetsThingsUp()
{
}
[SetUp]
public void ResetsThings()
{
}
[Test]
public void TestsMyNeatMethod()
{
}
}
}
没有做太多事情,但已经有错误了。当我运行
TestsMyNeatMethod()
时,我收到以下错误消息:
OneTimeSetUp:System.IO.FileNotFoundException:无法加载文件或程序集“CommonLibrary,版本= 1.3.5.0,文化=中性,PublicKeyToken = null”。系统找不到指定的文件。
我做错了什么?我尝试将
<Private>
设置为 False
,然后设置为 True
,但无济于事。
在 .NET 中,您无法从 .NET Core 或 .NET 5-8 项目引用 .NET Framework 项目或程序集。当运行时尝试加载 CommonLibrary.dll 时,这种不兼容性会导致 FileNotFoundException
选项1: 更改测试项目的目标框架:将测试项目的 .csproj 文件更新为目标 .NET Framework 4.8。
选项2: 将项目升级到 .NET Standard 或 .NET 8:将 CommonLibrary 和 MyProject_VM 迁移到目标 .NET Standard 或 .NET 8,以获得现代功能和交叉兼容性。这是一个长期的解决方案,但可能需要付出巨大的努力来升级依赖项并彻底测试。