c# - 在 .NET-core 中找不到 TestContext 类

标签 c# unit-testing .net-core

我正在使用 C# 将代码从 .NET-Framework 移动并重构为 .NET-Core。当我对应该对列表进行排序的方法运行简单测试时,出现以下错误:

"System.MissingMethodException: Method not found: 'System.Collections.IDictionary Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.get_Properties()'."

我已经检查了对其他必要命名空间的引用。网上搜了一下错误,发现.NET Core中还没有提供TestContext类!我可以使用其他方法或替换库吗?谢谢。

        [TestMethod]
        public void TestMethod()
        {
            // Arrange
            // Grab an arbitrary start time.
            Time startTime = Time.Now;

            List<TimeValue> values = new List<TimeValue>
            {
                // Make sure that second timestamp comes before the first 
                   timestamp
                new TimeValue(startTime.PlusMinutes(1)),
                new TimeValue(startTime)
            };

            // Ensure that this is sorted in ascending order of time.
            List<TimeValue> expectedValues = values.OrderBy(value => 
            value.Timestamp).ToList();
            CollectionAssert.AreNotEqual(expectedValues, values);

            // Act
            SortArray myObj = new SortArray(values);

            // Assert
            CollectionAssert.AreEqual(expectedValues, SortArray.Values);
        }

我希望 TestMethod 能够运行,但它没有运行并给出以下错误:

'System.Collections.IDictionary Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.get_Properties()'.

最佳答案

您可以使用的一个可能的替代方案是 xUnit 。它是一个开源工具,您可以将其与 .NET Core 一起使用。

微软提供 tutorial关于如何使用 .NET Core 进行 xUnit。

另一种可能性是“dotnet test”,它是 Microsoft 兼容 .NET Core 的单元测试工具。

关于c# - 在 .NET-core 中找不到 TestContext 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56858246/

相关文章:

c# - centos7中net core 3的tcpclient无法收发数据

.net-core - .NET Core 应用程序是否可以从 AWS Certificate Manager 检索证书并在 HttpClient 帖子中使用它?

amazon-web-services - AWS Codebuild .NET Core构建Docker镜像

c# - 如何获取 SQL Server 中所有弹性池的名称?

c# - 如何将 DropDownList 的更改保存到 sql 数据库?

c# - .NET 运行时 (CLR)、JIT 编译器究竟位于何处?

django - 如何在单元测试中优雅地交换(修补)Django FileSystemStorage 设置?

unit-testing - Mockito 可以检测模拟使用和字段分配吗?

python - 将 Django 的系统检查合并到单元测试套件中?

c# - ASP.NET Core 2 的 Visual Studio Code 调试器不适用于不同的端口