我遇到了难以解决的 BenchmarkDotNet 问题
这是我的项目结构:
-
| - Infrastructure
| |
| | - TestsBenchmark
| | - MyInfra.sln
| - src
| - Tests
| - MyProduct.sln
TestsBenchmark
引用Tests
并且只有这行代码:
BenchmarkSwitcher.FromAssembly(typeof(BasicTests).Assembly).RunAll();
但是当我通过dotnet run -c Release
它运行它时
// Generate Exception: Unable to find Tests in ...\Infrastructure and its subfolders. Most probably the name of output exe is different than the name of the .(c/f)sproj
以前我的项目结构是这样的:
-
| - src
| - Tests
| - TestsBenchmark
一切正常
复制步骤(手动),它重新创建文件夹结构、项目、项目关系、解决方案并添加 nuget。在一些空文件夹中的例如powershell中运行它:
mkdir Infrastructure
mkdir src
cd src
dotnet new xunit -n Tests
cd Tests
dotnet add package BenchmarkDotNet
cd ..
cd ..
cd Infrastructure
dotnet new console -n TestsBenchmark
cd TestsBenchmark
dotnet add package BenchmarkDotNet
cd ..
dotnet new sln -n Repro
dotnet sln add .\TestsBenchmark\TestsBenchmark.csproj
dotnet sln add .\..\src\Tests\Tests.csproj
cd TestsBenchmark
dotnet add reference "..\..\src\Tests\Tests.csproj"
单元测试1.cs
using System;
using BenchmarkDotNet.Attributes;
using Xunit;
namespace Tests
{
public class UnitTest1
{
[Fact]
[Benchmark]
public void Test1()
{
Console.WriteLine("asd");
}
}
}
程序.cs
using System;
using BenchmarkDotNet.Running;
using Tests;
namespace TestsBenchmark
{
class Program
{
static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(UnitTest1).Assembly).RunAll();
}
}
}
现在在里面Infrastructure\TestsBenchmark
履行dotnet run -c Release
你会看到
// Generate Exception: Unable to find Tests in C:\\Infrastructure and its subfolders. Most probably the name of output exe is different than the name of the .(c/f)sproj
// BenchmarkDotNet has failed to build the auto-generated boilerplate code.
// It can be found in C:\\repro\Infrastructure\TestsBenchmark\bin\Release\net5.0\65ba2c51-e794-4f44-93ab-f811411c86f5
// Please follow the troubleshooting guide: https://benchmarkdotnet.org/articles/guides/troubleshooting.html