Search code examples
c#unit-testing.net-assembly

FileNotFoundException: Could not load file or assembly CustomAssembly from unit testing


I'm adding a NUnit test project to an existing solution. The test project .csproj file looks like this:

<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>

I want it to test classes and methods on MyProject_VM, defined like this:

<?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>

(I have removed <Reference/> and <Compile/> elements for brevity.)

My test project has only two classes so far. TestsArchitecture does the general setup/teardown:

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 uses CommonLibrary.

And TestMyNeatClass tests the methods in MyNeatClass:

namespace TestMyProject
{
    [TestFixture]
    public class TestMyNeatClass
    {
        [OneTimeSetUp]
        public void SetsThingsUp()
        {
        }

        [SetUp]
        public void ResetsThings()
        {
        }

        [Test]
        public void TestsMyNeatMethod()
        {
        }
    }
}

Doesn't do much, but it already has an error. When I run TestsMyNeatMethod(), I get the following error message:

OneTimeSetUp: System.IO.FileNotFoundException : Could not load file or assembly 'CommonLibrary, Version=1.3.5.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.

What did I do wrong? I tried to set <Private> to False and then to True, to no avail.


Solution

  • In .NET, you cannot reference a .NET Framework project or assembly from a .NET Core or .NET 5-8 project. This incompatibility leads to the FileNotFoundException when the runtime tries to load CommonLibrary.dll

    Option 1: Change Test Project's Target Framework: Update the test project's .csproj file to target .NET Framework 4.8.

    Option 2: Upgrade Projects to .NET Standard or .NET 8: Migrate CommonLibrary and MyProject_VM to target .NET Standard or .NET 8 for modern features and cross-compatibility. This is a long-term solution, but may require significant effort to upgrade dependencies and test thoroughly.