首页 > 解决方案 > 在 .NET-core 中找不到 TestContext 类

问题描述

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

“System.MissingMethodException:找不到方法:'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()'。

标签: c#unit-testing.net-core

解决方案


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

Microsoft 提供了有关如何使用 .NET Core 进行 xUnit的教程。

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


推荐阅读