简体   繁体   中英

TestContext class not found in .NET-core

I am moving and refactoring a code from .NET-Framework to .NET-Core in C#. When I run a simple test on a method which is supposed to sort a List, I get this error:

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

I have checked references to other namespaces that are necessary. I searched for the error online and I realized that the TestContext Class has not been provided in .NET Core yet! Is there another way or a replacement library I can use instead? Thank you.

        [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);
        }

I expect the TestMethod to run, but it does not run and gives me the following error:

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

A possible alternate you could use is xUnit . Its a open sourced tool and you can use it with .NET Core.

Microsoft offers a tutorial on how to xUnit with the .NET Core.

Another possibility is "dotnet test" which is a unit testing tool that Microsoft made compatible for .NET Core.

Try adding to your test class the following property:

public TestContext TestContext { get; set; }

In general, MSTest does not seem to be actively developed. As it's shipped with Visual Studio , Microsoft keeps it working on .NET (and somewhat even on .NET Core ) but they seem to use xUnit themselves internally, so it makes sense to consider switching your tests to xUnit either.

It works when you provide a field of type TestContext. It doesn't work when you make it a property . Following works with .NET Core 3.1 as described 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);
        }
    }
}

But after changing

    private static TestContext Context;

into

    private static TestContext Context { get; set; }

causes tests don't run anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM