C# 在.NET core中找不到TestContext类

C# 在.NET core中找不到TestContext类,c#,unit-testing,.net-core,C#,Unit Testing,.net Core,我正在将一段代码从.NET Framework迁移到C#中的.NET核心并进行重构。当我对一个应该对列表进行排序的方法运行一个简单测试时,我得到以下错误: “System.MissingMethodException:找不到方法:'System.Collections.IDictionary Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.get_Properties()。” 我已经检查了对其他必要名称空间的引用。我在网上搜索了错

我正在将一段代码从.NET Framework迁移到C#中的.NET核心并进行重构。当我对一个应该对列表进行排序的方法运行一个简单测试时,我得到以下错误:

“System.MissingMethodException:找不到方法:'System.Collections.IDictionary Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.get_Properties()。”

我已经检查了对其他必要名称空间的引用。我在网上搜索了错误,发现.NETCore中还没有提供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]
公共void TestMethod()
{
//安排
//抓取任意的开始时间。
时间开始时间=时间。现在;
列表值=新列表
{
//确保第二个时间戳在第一个时间戳之前
时间戳
新的时间值(开始时间加上分钟数(1)),
新时间值(开始时间)
};
//确保按时间的升序排序。
列出expectedValues=values.OrderBy(值=>
value.Timestamp.ToList();
采集评估AreNotEqual(期望值、值);
//表演
SortArray myObj=新SortArray(值);
//断言
CollectionAssert.AreEqual(期望值、SortArray.Values);
}
我希望TestMethod能够运行,但它没有运行,并给出以下错误:

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


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

微软提供了一个关于如何使用.NET内核进行xUnit的解决方案


另一种可能性是“dotnettest”,这是一种单元测试工具,微软将其与.netcore兼容

尝试将以下属性添加到测试类中:

public TestContext TestContext { get; set; }

一般来说,
MSTest
似乎没有得到积极开发。因为它的代码是<代码> VisualStudio,微软在代码> > NET<代码>(甚至在<代码> .NET内核< /COD>),但是它们本身在内部似乎使用了<代码> XUngs/Cuth>,所以也有必要考虑将您的测试切换到<代码> XUngs/COD>。p> 当您提供TestContext类型的字段时,它会起作用。当您将其设置为属性时,它不起作用。以下内容适用于.NETCore3.1,如下所述

但是换了以后

    private static TestContext Context;
进入


原因测试不再运行。

请。在你的答案中包括要点;留下链接以获取更多信息或作为参考。如果该网站关闭或链接失效,你的答案将变得毫无用处。@BrootsWaymb谢谢你让我知道。我相应地编辑了我的答案。我最近遇到了这个问题。解决方案是在调用测试运行程序时正确配置测试适配器的路径。比如:..\packages\MSTest.TestAdapter.1.4.0TestContext不应该是静态的,所以我无法理解为什么应该这样做。对于DotnetCore3.1,我可以使用
publictestcontext TestContext{get;set;}
作为写操作。
    private static TestContext Context;
    private static TestContext Context { get; set; }