How do you unit test ASP.NET Core Web Application (.Net Framework)?

Andre Messier

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": {
        }
    }
}
stephen.vakil

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"
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you unit test RazorViewEngineOptions in ASP.NET Core MVC?

How do you unit test ASP.NET Core MVC Controllers that return anonymous objects?

ASP.NET Web Application (.NET Framework) vs ASP.NET Core Web Application (.NET Framework)

How do you Authorize a Web API Controller in ASP Net Core

ASP.NET Core Web Application (.NET Framework) TagHelper Intellisense

missing “ASP.NET Core Web Application (.NET Framework)” template

ImageResizer and ASP.NET Core Web Application (.NET Framework)

What is the proper way to unit test .NET Core Web Application?

Unit testing in Asp.Net Core MVC Web Application

how to unit test asp.net core application with constructor dependency injection

In an ASP.NET Core 7 MVC web application, how do you start a debugging session at a specific path based on an area?

How do you unit test LoggerMessage.Define() in .NET Core 3.1 with Moq?

How to unit test an action filter attribute for web api in asp.net core?

How do you setup unit testable model validation with dependency injection in ASP.NET Core 2 MVC?

How to save and retrieve images from database using Entity Framework Core in ASP.NET Core web application?

How to unit test, actionresult with using mysql in ASP.NET Core

How to unit test with ILogger in ASP.NET Core

How to unit test ViewBag in ASP.NET Core?

ASP.NET Core Web Application with .NET Framework, I want to switch the target to .NET Core

asp.net core SignalR configuration in ASP.NET Web Application (.NET framework)

Asp.net core session unit test

How to do Unit Test with Dependency Injection .net Core 2

How do you send a body with an erroring http response in ASP.NET Core Web API?

How do you setup an ASP.Net 3.0 Core Web API project to use AutoFac and NLog?

How to do an integration test on my Asp.Net core web api endpoint where I upload a file?

How do you add data from the database to the shared _Layout.cshtml in ASP.NET Core MVC (.NET 6) application?

In ASP.Net MVC with .Net Framework 4.X, how do you add keys to web.config during the deployment pipeline?

How do I remove Bower from a ASP.NET Core Web application?

How do I launch the web browser after starting my ASP.NET Core application?