【问题标题】:TestContext class not found in .NET-core在 .NET-core 中找不到 TestContext 类
【发布时间】:2019-07-02 18:26:48
【问题描述】:

我正在使用 C# 将代码从 .NET-Framework 移动和重构到 .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()'。

【问题讨论】:

  • 我最近遇到了这个问题。解决方案是在调用测试运行器时正确配置测试适配器的路径。喜欢:..\packages\MSTest.TestAdapter.1.4.0

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


【解决方案1】:

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

Microsoft 提供了一个tutorial,介绍如何使用 .NET Core 进行 xUnit。

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

【讨论】:

  • do not post an answer that consists essentially of a link。在你的答案中包括要点;留下链接以获取更多信息或作为参考。如果该网站出现故障或链接失效,则您的答案将毫无用处。
  • @BrootsWaymb 感谢您告诉我。我相应地编辑了我的答案。
【解决方案2】:

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

public TestContext TestContext { get; set; }

总的来说,MSTest 似乎没有被积极开发。由于它与Visual Studio 一起提供,Microsoft keeps it working.NET 上(和somewhat 甚至在.NET Core 上)但他们似乎在内部使用xUnit,所以考虑将您的测试切换到@987654329 是有意义的@ 任何一个。

【讨论】:

    【解决方案3】:

    当您提供 TestContext 类型的 字段 时,它会起作用。当您将其设为属性时,它不起作用。以下适用于 .NET Core 3.1,如 here 所述。

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    namespace TimeLogger.Tests
    {
        [TestClass]
        public class YourTestClass
        {
            private static TestContext Context;
    
            [ClassInitialize]
            public static void InitClass(TestContext testContext)
            {
                Context = testContext;
            }
    
            [TestMethod]
            public void Test_1()
            {
                Assert.IsTrue(true);
            }
    
            [TestMethod]
            public void Test_2()
            {
                Assert.IsTrue(true);
            }
        }
    }
    

    但是改变之后

        private static TestContext Context;
    

    进入

        private static TestContext Context { get; set; }
    

    导致测试不再运行。

    【讨论】:

    • TestContext 不应该是静态的,所以为什么这应该工作比我理解的要多。使用 Dotnet core 3.1,我可以使用 public TestContext TestContext { get; set; } 作为 @dmitry-pavlov 写入。
    猜你喜欢
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多