.net core xunit测试项目中Autofixture的AutoData属性

Autofixture's AutoData attribute in .net core xunit test projects

我可以在 .net core 2.2 xunit 测试项目中使用 AutoData 吗? AutoFixture.Xunit 以 .NET Framework 4.5.2 为目标,但我无法使用它。也许还有其他适用于 .net 核心的 NuGet 包?

如果您查看 AutoFixture.Xunit package on nuget.org,您可能会注意到以下内容:

By leveraging the data theory feature of xUnit.net, this extension turns AutoFixture into a declarative framework for writing unit tests. In many ways it becomes a unit testing DSL (Domain Specific Language). For xUnit.net 2, please use the AutoFixture.Xunit2 NuGet Package.

(我的重点。)

此外,如果展开 Dependencies 部分,您会注意到包的依赖约束:

AutoFixture (>= 4.6.0)
xunit (>= 1.8.0.1549 && < 2.0.0)
xunit.extensions (>= 1.8.0.1549 && < 2.0.0)

请特别注意,xunit 的约束是 < 2.0.0。换句话说,它只适用于 xunit 1.8.0.1549 到 1.9.2(我相信这是最新版本,低于 2.0)。

如果您想使用 xUnit.net 2.0 或更高版本,您应该按照文档所述使用 AutoFixture.Xunit2 包。更新的版本也支持 .NET Core。