最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Problems with unit testintegration .NET 8 project - Stack Overflow

programmeradmin1浏览0评论

I am having problems trying to do unit testing/integration with .NET 8 to a project (small api that works), I wanted to add tests to it to learn since I had never done them but I am running into something that I can't solve.

Root structure of my project: Controllers, DTOs, Migrations, Models, Repository, Services more files and the folder created to tests (WebApi.Tests/)

I added the WebApi.Tests folder inside the main project (I don't know if this is the right practice) but when I run it I get errors like duplicate attributes and missing packages like Xunit, FactAttribute, Fact.

Packages are installed:

/DotNetProjects/WebApi/WebApi.Tests$ dotnet list package --outdated

The following sources were used:
   .json

Project `WebApi.Tests` has the following updates to its packages
   [net8.0]: 
   Top-level Package                    Requested   Resolved   Latest 
   > coverlet.collector                 6.0.0       6.0.0      6.0.4  
   > Microsoft.NET.Test.Sdk             17.6.0      17.6.0     17.13.0
   > xunit                              2.6.3       2.6.3      2.9.3  
   > xunit.core                         2.6.3       2.6.3      2.9.3  
   > xunit.extensibility.core           2.6.3       2.6.3      2.9.3  
   > xunit.extensibility.execution      2.6.3       2.6.3      2.9.3  
   > xunit.runner.visualstudio          2.4.5       2.4.5      3.0.2

The weird thing is that if I run my test project without the reference to the main program I can run the tests without problem, however when I add the reference I get the errors: errors when running dotnet test:

DotNetProjects/WebApi/WebApi.Tests$ dotnet test 
  Determining projects to restore...
  All projects are up-to-date for restore.
/home/gustavo.ruiz/DotNetProjects/WebApi/obj/Debug/net8.0/.NETCoreApp,Version=v8.0.AssemblyAttributes.cs(4,12): error CS0579: Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute' attribute [/home/gustavo.ruiz/DotNetProjects/WebApi/WebApi.csproj]
/home/gustavo.ruiz/DotNetProjects/WebApi/WebApi.Tests/GlobalUsings.cs(1,14): error CS0246: The type or namespace name 'Xunit' could not be found (are you missing a using directive or an assembly reference?) [/home/gustavo.ruiz/DotNetProjects/WebApi/WebApi.csproj]
/home/gustavo.ruiz/DotNetProjects/WebApi/WebApi.Tests/UnitTest1.cs(5,6): error CS0246: The type or namespace name 'FactAttribute' could not be found (are you missing a using directive or an assembly reference?) [/home/gustavo.ruiz/DotNetProjects/WebApi/WebApi.csproj]
/home/gustavo.ruiz/DotNetProjects/WebApi/WebApi.Tests/UnitTest1.cs(5,6): error CS0246: The type or namespace name 'Fact' could not be found (are you missing a using directive or an assembly reference?) [/home/gustavo.ruiz/DotNetProjects/WebApi/WebApi.csproj]

WebApi.csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
    <PackageReference Include="FluentValidation" Version="11.11.0" />
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.11" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.tools" Version="8.0.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
  </ItemGroup>
  <!-- <ItemGroup>
    <Compile Include="Services.fs" />
  </ItemGroup> -->
</Project>

WebApi.Tests.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="Microsoft.NET.Test.Sdk" Version="17.6.0" />
    <PackageReference Include="xunit" Version="2.6.3" />
    <PackageReference Include="xunit.core" Version="2.6.3" />
    <PackageReference Include="xunit.extensibility.core" Version="2.6.3" />
    <PackageReference Include="xunit.extensibility.execution" Version="2.6.3" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="6.0.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\WebApi.csproj" />
  </ItemGroup>

</Project>

as I commented when removing ProjectReference this runs the tests without any problem (although it practically only created the test project and has a sample code)

UnitTest1.cs:

namespace WebApi.Tests;

public class UnitTest1
{
    [Fact]
    public void Test1()
    {
        Assert.True(true);
    }
}

Things I have tried:

  • Delete the project and recreate it (dotnet new xunit -n WebApi.Tests -o WebApi.Tests)
  • Delete the bin and obj directories and run dotnet restore, dotnet clean, dotnet build
  • Reinstall the test packages

I would like to be able to run the tests without errors

I am having problems trying to do unit testing/integration with .NET 8 to a project (small api that works), I wanted to add tests to it to learn since I had never done them but I am running into something that I can't solve.

Root structure of my project: Controllers, DTOs, Migrations, Models, Repository, Services more files and the folder created to tests (WebApi.Tests/)

I added the WebApi.Tests folder inside the main project (I don't know if this is the right practice) but when I run it I get errors like duplicate attributes and missing packages like Xunit, FactAttribute, Fact.

Packages are installed:

/DotNetProjects/WebApi/WebApi.Tests$ dotnet list package --outdated

The following sources were used:
   https://api.nuget./v3/index.json

Project `WebApi.Tests` has the following updates to its packages
   [net8.0]: 
   Top-level Package                    Requested   Resolved   Latest 
   > coverlet.collector                 6.0.0       6.0.0      6.0.4  
   > Microsoft.NET.Test.Sdk             17.6.0      17.6.0     17.13.0
   > xunit                              2.6.3       2.6.3      2.9.3  
   > xunit.core                         2.6.3       2.6.3      2.9.3  
   > xunit.extensibility.core           2.6.3       2.6.3      2.9.3  
   > xunit.extensibility.execution      2.6.3       2.6.3      2.9.3  
   > xunit.runner.visualstudio          2.4.5       2.4.5      3.0.2

The weird thing is that if I run my test project without the reference to the main program I can run the tests without problem, however when I add the reference I get the errors: errors when running dotnet test:

DotNetProjects/WebApi/WebApi.Tests$ dotnet test 
  Determining projects to restore...
  All projects are up-to-date for restore.
/home/gustavo.ruiz/DotNetProjects/WebApi/obj/Debug/net8.0/.NETCoreApp,Version=v8.0.AssemblyAttributes.cs(4,12): error CS0579: Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute' attribute [/home/gustavo.ruiz/DotNetProjects/WebApi/WebApi.csproj]
/home/gustavo.ruiz/DotNetProjects/WebApi/WebApi.Tests/GlobalUsings.cs(1,14): error CS0246: The type or namespace name 'Xunit' could not be found (are you missing a using directive or an assembly reference?) [/home/gustavo.ruiz/DotNetProjects/WebApi/WebApi.csproj]
/home/gustavo.ruiz/DotNetProjects/WebApi/WebApi.Tests/UnitTest1.cs(5,6): error CS0246: The type or namespace name 'FactAttribute' could not be found (are you missing a using directive or an assembly reference?) [/home/gustavo.ruiz/DotNetProjects/WebApi/WebApi.csproj]
/home/gustavo.ruiz/DotNetProjects/WebApi/WebApi.Tests/UnitTest1.cs(5,6): error CS0246: The type or namespace name 'Fact' could not be found (are you missing a using directive or an assembly reference?) [/home/gustavo.ruiz/DotNetProjects/WebApi/WebApi.csproj]

WebApi.csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
    <PackageReference Include="FluentValidation" Version="11.11.0" />
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.11" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.tools" Version="8.0.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
  </ItemGroup>
  <!-- <ItemGroup>
    <Compile Include="Services.fs" />
  </ItemGroup> -->
</Project>

WebApi.Tests.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="Microsoft.NET.Test.Sdk" Version="17.6.0" />
    <PackageReference Include="xunit" Version="2.6.3" />
    <PackageReference Include="xunit.core" Version="2.6.3" />
    <PackageReference Include="xunit.extensibility.core" Version="2.6.3" />
    <PackageReference Include="xunit.extensibility.execution" Version="2.6.3" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="6.0.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\WebApi.csproj" />
  </ItemGroup>

</Project>

as I commented when removing ProjectReference this runs the tests without any problem (although it practically only created the test project and has a sample code)

UnitTest1.cs:

namespace WebApi.Tests;

public class UnitTest1
{
    [Fact]
    public void Test1()
    {
        Assert.True(true);
    }
}

Things I have tried:

  • Delete the project and recreate it (dotnet new xunit -n WebApi.Tests -o WebApi.Tests)
  • Delete the bin and obj directories and run dotnet restore, dotnet clean, dotnet build
  • Reinstall the test packages

I would like to be able to run the tests without errors

Share Improve this question edited Apr 3 at 6:38 Zhi Lv 22k1 gold badge27 silver badges37 bronze badges asked Mar 30 at 0:20 Gustavo RuizGustavo Ruiz 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You shouldn't put the test project inside the main project, you should create a solution then link the main project "webapi" and the test project.

on your new folder create the test project using the commands it's more stable I'd say:

# dotnet new xunit -n "Webapi.Tests"

your main project should be in the same folder

WebapiProject
|__ Webapi
|_
_ Webapi.Tests

Note that you should create a solution and add them together into a single solution, also don't fet to add the main project reference inside the test project.

发布评论

评论列表(0)

  1. 暂无评论