xUnit 测试在本地运行,但不使用 .NET 6 在 Azure DevOps 中运行
这个问题就像一年前发布的标题为 xUnit 测试在本地运行但不适用于 Azure DevOps。然而,它的不同足以保证一个单独的问题。我正在使用 .NET 6 开发一个 ASP.NET MVC 项目。我有一个 Azure CI Pipeline,它使用 DotNetCoreCLI@2,而不是管道中的 VSTest@2 任务,就像使用 Azure Pipeline 时通常包含的那样ASP.NET 模板。运行管道时,我在日志中收到以下警告消息:
D:\a\1\s\PharmacyWarehouseV2\PharmacyWarehouseV2.Test\HomeControllerTest.cs(18,49):警告 CS8625:无法将 null 文字转换为不可为 null 的引用类型。
查看 xUnit 测试(只有一个),很容易明白为什么要这样做:
varcontroller = new HomeController(null);
当时我主要对让单元测试正常工作感兴趣。现在我认为它不起作用的原因是因为 C# 编译器发现 NULL 被传递到 HomeController 中,在 HomeController 中它将被分配给 ILogger。因此,我开始寻找更好的日志记录方式。我遇到了另一个参考资料,但我无法再次找到(不幸的是)它说 .NET 6 不再与 ILogger 一起使用。那么,现在使用 .NET 6 在 MVC 控制器和 xUnit 单元测试中处理日志记录的首选方法是什么?
这是我的 YAML 文件中的 DotNetCoreCLI@2 任务:
- task: DotNetCoreCLI@2
displayName: 'Dot Net Core Test'
inputs:
command: 'test'
projects: '**/*[Tt]est*/*.csproj'
arguments: '--configuration $(BuildConfiguration)'
enabled: false
This question is like one posted a year ago titled xUnit tests run locally but not on Azure DevOps. However, it is different enough to warrant a separate question. I'm developing an ASP.NET MVC project using .NET 6. And I've got an Azure CI Pipeline which uses DotNetCoreCLI@2, instead of the VSTest@2 task in the pipeline, as is normally included when using the Azure Pipeline ASP.NET template. When running the pipeline I get this warning message in the log:
D:\a\1\s\PharmacyWarehouseV2\PharmacyWarehouseV2.Test\HomeControllerTest.cs(18,49): warning CS8625: Cannot convert null literal to non-nullable reference type.
Looking at the xUnit test (there's only one) it's easy to see why it's doing this:
var controller = new HomeController(null);
At the time I was mainly interested in getting a unit test working. Now I'm thinking that the reason why it isn't working is because the C# compiler sees that NULL being passed into the HomeController where it would be assigned to the ILogger. Therefore, I started looking for a better way of logging. I came across another reference, which I cannot find again (unfortunately) saying that .NET 6 doesn't work with the ILogger anymore. So, what is the preferred way of handling logging now with .NET 6, both in a MVC controller and in a xUnit unit test?
Here's the DotNetCoreCLI@2 task in my YAML file:
- task: DotNetCoreCLI@2
displayName: 'Dot Net Core Test'
inputs:
command: 'test'
projects: '**/*[Tt]est*/*.csproj'
arguments: '--configuration $(BuildConfiguration)'
enabled: false
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论