Search code examples
c#.netunit-testingmstestdata-driven-tests

MSTest differences between .NET Core and .Net Framework projects


TL;DR

  1. Are there significant differences in those 2 types of unit test projects?
  2. Do I have to always use matching UT project type to application (.Core WPF -> MSTest .Core or .Framework), or it doesn't matter (.Framework -> MSTest .Core and in opposite way)

Full version

I'm starting research for my new C# WPF project with unit tests, decided to use MSTest for long-term and flexible lib developing (via this comparison), which I'll use for setting database to known state. I was wondering if there are more differences in MSTest .Core and .Framework then "usual" ones, like multiplatform solutions etc.

Add a new project window in VS19 with MSTest and xUnit projects filtered

Second questions is also important, because my earlier projects are written on both types of .NET, but uses same database (same data logic), so developing two very similar test projects might be pointless at this point, I can use other framework like xUnit, which probably would be more flexible for such thing.


Solution

  • Actual answer is pretty obvious, although I had to test it myself

    Tests are compatible only within the same framework

    To test it out I created sample test with .Framework:

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System;
    using Answer;
    using System.Runtime.InteropServices;
    
    namespace UnitTestProject1
    {
        [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
                Console.WriteLine(AnswerClass.SomeMethod());
            }
        }
    }
    

    and referenced my application .Core at Solution Explorer.

    When I tried to build test project, compiler returned warning:

    The project 'Answer' cannot be referenced. The referenced project is targeted to a different framework family (.NETCoreApp)

    VS didn't inform me about such fact on test project creation, either Intellisense wasn't able to diagnose why is the project content unreferencable.

    At this point I used xUnit, which can handle testing .Framework WPF with some .Core dependencies.