2

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()'.

  • I've had this problem recently. The solution was the correct configuration of the path of the test adapter when invoking the test runner. Like: ..\packages\MSTest.TestAdapter.1.4.0 – Peter Jul 02 '19 at 19:08

3 Answers3

2

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.

Cabbage Champion
  • 1,193
  • 1
  • 6
  • 21
  • 3
    Please [do not post an answer that consists essentially of a link](https://stackoverflow.com/questions/how-to-answer). Include the important points in your answer; leave the link for extra information or as a reference. If that site goes down or the link goes dead, it renders your answer useless. – Broots Waymb Jul 02 '19 at 18:32
  • @BrootsWaymb Thanks for letting me know. I edited my answer accordingly. – Cabbage Champion Jul 02 '19 at 18:40
1

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.

Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121
0

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.

  • TestContext should not be a static so why this should work is more than I understand. With Dotnet core 3.1 I can use `public TestContext TestContext { get; set; }` as [@dmitry-pavlov](https://stackoverflow.com/a/56859075/521554) writes. – LosManos Apr 26 '20 at 18:11