Dotnet 6 集成测试缺少 Deps 文件
Deps File Missing for Dotnet 6 Integration Tests
在开始之前,我已经尝试了以下所有建议和 none 工作:
https://zimmergren.net/unable-to-find-deps-json-dotnet-azure-devops/
所以我正在尝试为 dotnet 6 编写一些集成测试。但是,我的 WebApplicationFactory
抛出以下错误:
System.InvalidOperationException: Can't find
'/repos/subscription-info-api/tests/SubscriptionInfoApi.Tests.Integration/bin/Debug/net6.0/...
System.InvalidOperationException Can't find
'/repos/subscription-info-api/tests/SubscriptionInfoApi.Tests.Integration/bin/Debug/net6.0/testhost.deps.json'.
This file is required for functional tests to run properly. There
should be a copy of the file on your source project bin folder. If
that is not the case, make sure that the property
PreserveCompilationContext is set to true on your project file. E.g
'true'. For
functional tests to work they need to either run from the build output
folder or the testhost.deps.json file from your application's output
directory must be copied to the folder where the tests are running on.
A common cause for this error is having shadow copying enabled when
the tests run. at
Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.EnsureDepsFile() at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory
1.EnsureServer()
at
Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.CreateDefaultClient(DelegatingHandler[] handlers) at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory
1.CreateDefaultClient(Uri
baseAddress, DelegatingHandler[] handlers) at
Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.CreateClient(WebApplicationFactoryClientOptions options) at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory
1.CreateClient()
at SubscriptionInfoApi.Tests.Integration.UnitTest1.Test1() in
/repos/subscription-info-api/tests/SubscriptionInfoApi.Tests.Integration/UnitTest1.cs:line
14 at SubscriptionInfoApi.Tests.Integration.UnitTest1.Test1() in
/repos/subscription-info-api/tests/SubscriptionInfoApi.Tests.Integration/UnitTest1.cs:line
16 at
Xunit.Sdk.TestInvoker1.<>c__DisplayClass48_0.<<InvokeTestMethodAsync>b__1>d.MoveNext() in /_/src/xunit.execution/Sdk/Frameworks/Runners/TestInvoker.cs:line 264 --- End of stack trace from previous location --- at Xunit.Sdk.ExecutionTimer.AggregateAsync(Func
1 asyncAction) in
//src/xunit.execution/Sdk/Frameworks/ExecutionTimer.cs:line 48 at
Xunit.Sdk.ExceptionAggregator.RunAsync(Func`1 code) in
//src/xunit.core/Sdk/ExceptionAggregator.cs:line 90
我的实际测试代码极其简单:
[Fact]
public async Task Test1()
{
await using var app = new WebApplicationFactory<Program>();
using var client = app.CreateClient();
var res = await (await client.GetAsync("/alive-test")).Content.ReadAsStringAsync();
Assert.Equal("Alive!", res);
}
根据建议,我已确保在我的集成测试项目中直接引用 Microsoft.AspNetCore.Mvc.Testing -> 6.0.0
。我还尝试了对建议的 .csproj
文件进行各种调整,但似乎没有任何效果。
我无法尝试进一步调试,有什么想法吗?
您可能在测试文件中将错误的程序命名空间作为目标(就像我一样)。
我必须在我的 Program.cs 文件末尾添加以下内容(最后一行)以使其对需要它的测试项目可见:
public partial class Program { }
可在此处找到示例:minimal api testing example
我遇到了同样的问题,尽管原因完全不同。
我正在使用 .net 6,但我故意选择使用实际上具有 Program.cs
文件的实现。
当我从 the official MS integration test guide 复制代码时,我让 VS 引入所有依赖项。用于解析 Program.cs
的依赖项不是我自己的(PersonalSite 为了这个答案),而是 MS 自己的实现之一:
当然是我的一个小错误,但也许我可以帮助别人。
对于那些真正需要部分 class 实施噱头的人,我链接的 MS integ 测试指南列出了指南来做到这一点。
在开始之前,我已经尝试了以下所有建议和 none 工作:
https://zimmergren.net/unable-to-find-deps-json-dotnet-azure-devops/
所以我正在尝试为 dotnet 6 编写一些集成测试。但是,我的 WebApplicationFactory
抛出以下错误:
System.InvalidOperationException: Can't find '/repos/subscription-info-api/tests/SubscriptionInfoApi.Tests.Integration/bin/Debug/net6.0/...
System.InvalidOperationException Can't find '/repos/subscription-info-api/tests/SubscriptionInfoApi.Tests.Integration/bin/Debug/net6.0/testhost.deps.json'. This file is required for functional tests to run properly. There should be a copy of the file on your source project bin folder. If that is not the case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g 'true'. For functional tests to work they need to either run from the build output folder or the testhost.deps.json file from your application's output directory must be copied to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the tests run. at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory
1.EnsureDepsFile() at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory
1.EnsureServer() at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.CreateDefaultClient(DelegatingHandler[] handlers) at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory
1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers) at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.CreateClient(WebApplicationFactoryClientOptions options) at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory
1.CreateClient() at SubscriptionInfoApi.Tests.Integration.UnitTest1.Test1() in /repos/subscription-info-api/tests/SubscriptionInfoApi.Tests.Integration/UnitTest1.cs:line 14 at SubscriptionInfoApi.Tests.Integration.UnitTest1.Test1() in /repos/subscription-info-api/tests/SubscriptionInfoApi.Tests.Integration/UnitTest1.cs:line 16 at Xunit.Sdk.TestInvoker1.<>c__DisplayClass48_0.<<InvokeTestMethodAsync>b__1>d.MoveNext() in /_/src/xunit.execution/Sdk/Frameworks/Runners/TestInvoker.cs:line 264 --- End of stack trace from previous location --- at Xunit.Sdk.ExecutionTimer.AggregateAsync(Func
1 asyncAction) in //src/xunit.execution/Sdk/Frameworks/ExecutionTimer.cs:line 48 at Xunit.Sdk.ExceptionAggregator.RunAsync(Func`1 code) in //src/xunit.core/Sdk/ExceptionAggregator.cs:line 90
我的实际测试代码极其简单:
[Fact]
public async Task Test1()
{
await using var app = new WebApplicationFactory<Program>();
using var client = app.CreateClient();
var res = await (await client.GetAsync("/alive-test")).Content.ReadAsStringAsync();
Assert.Equal("Alive!", res);
}
根据建议,我已确保在我的集成测试项目中直接引用 Microsoft.AspNetCore.Mvc.Testing -> 6.0.0
。我还尝试了对建议的 .csproj
文件进行各种调整,但似乎没有任何效果。
我无法尝试进一步调试,有什么想法吗?
您可能在测试文件中将错误的程序命名空间作为目标(就像我一样)。
我必须在我的 Program.cs 文件末尾添加以下内容(最后一行)以使其对需要它的测试项目可见:
public partial class Program { }
可在此处找到示例:minimal api testing example
我遇到了同样的问题,尽管原因完全不同。
我正在使用 .net 6,但我故意选择使用实际上具有 Program.cs
文件的实现。
当我从 the official MS integration test guide 复制代码时,我让 VS 引入所有依赖项。用于解析 Program.cs
的依赖项不是我自己的(PersonalSite 为了这个答案),而是 MS 自己的实现之一:
当然是我的一个小错误,但也许我可以帮助别人。
对于那些真正需要部分 class 实施噱头的人,我链接的 MS integ 测试指南列出了指南来做到这一点。