C# – How to unit test ASP.NET Core Web Application (.Net Framework)

asp.netasp.net-core-mvccnetunit testing

I'm building an ASP.NET Core Web Application (.Net Framework) and am having a hard time figuring out how to hook unit tests up to it. I am targeting the .net framework version 4.6.1

If I create a regular "Class Library" project targeting 4.6.1, as I would for previous version of MVC, it lets me add references to my MVC project (which is part of the same solution) but any namespaces I add through a using statement report an error that I might be missing a reference or using statement. If I double click on the reference under the "References" section in the solution explorer it tells me that the project can't be found or hasn't been build yet.

I tried creating a "Class Library (.NET Core)" but that complains since I'm targeting .Net Framework and not .NET Core. I edited the class libaries Project.json to have it target the .net framework and that lets me add my references and doesn't complain when I the namespaces in a using statement but none of my tests are discovered by the test runner. I've tried both XUnit and NUnit and they both behave the same.

Is it possible to unit test an ASP.Net Core Web Application targeting the .Net Framework 4.6.1 or do I need to commit to the .NET Core?

Edit to add my test class
Here is my test class stripped down to the bare minimum. TestBank.Services is the class I want to test.

using System;
using TestBank.Services;
using Xunit;

namespace TestBankUnitTests
    {    
    public class Class1
    {        
        [Fact]
        public void TestA()
        {
            Assert.True(false);
        }
    }
}

and here is my project.json

{
    "version": "1.0.0-*",

    "dependencies": {
        "xunit": "2.1.0",
        "dotnet-test-xunit": "1.0.0-rc2-build10025",
        "TestBank": "1.0.0-*"
    },

    "frameworks": {
        "net461": {
        }
    }
}

Best Answer

Your project.json needs a testRunner setting. Per the project.json documentation, the testRunner setting not only specifies which test framework to use, but also marks the project as a test project.

Try adding it and see if it finds your tests (verified locally that it will not find tests without this setting in the file)

{
    "version": "1.0.0-*",

    "dependencies": {
        "xunit": "2.1.0",
        "dotnet-test-xunit": "1.0.0-rc2-build10025",
        "TestBank": "1.0.0-*"
    },

    "frameworks": {
        "net461": {
        }
    },

   "testRunner": "xunit"
}