How can I get code coverage for a .net core web application which targets net452 in VS2017 (or VS2015)?
I have my tests set up with xUnit but I get no coverage results for the .net core web application. The tests run fine, but I get no coverage!
Is this a known issue?
It doesn't work with MS's test library either.
Quick to repro:
Load up VS2017
Create new ASP.NET Core Web Application (.NET
Framework) called WebApplication1
Create TestClass.cs as below
Create new Unit Test Project (.NET Framework) called UnitTestProject1
Add reference to WebApplication1 in UnitTestProject1
Edit UnitTest1.cs as below
Run Test -> Analyze Code Coverage -> All Tests
Open Test -> Windows -> Code Coverage Results
Code coverage only shows unittestproject1.dll
TestClass.cs
namespace WebApplication1
{
public class TestClass
{
public bool TestMethod(bool test)
{
if (test) { return true; }
return false;
}
}
}
UnitTest1.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject3
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var testClass = new WebApplication1.TestClass();
var val = testClass.TestMethod(true);
Assert.IsTrue(val);
}
}
}
Code coverage is not implemented for netcore projects yet. This
support requires datacollectors infrastructure
(https://github.com/Microsoft/vstest/issues/309) . It will come post
RTW. We recommend that you follow the above issue for updates and fix
notifications.
https://developercommunity.visualstudio.com/content/problem/5813/cannot-get-test-coverage-for-net-core-projects.html
Related
I created a project in Visual Studio for unit testing using the nuget package of nunit. The test runs well in Visual Studio using the test explorer, but i Need to run them using the nunit3 console.
My project is very simple I:
Created a console project in C#
I Installed Nunit and NUnitTestAdapter using NuGet Package Manager.
I create a class called MyMath.cs with the next code:
namespace NunitDemo
{
class MyMath
{
public int add(int a, int b)
{
return a + b;
}
public int sub(int a, int b)
{
return a - b;
}
}
}
I create a MyTestCase Class with the following code to test MyMath methods:
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NunitDemo
{
[TestFixture]
class MyTestCase
{
[TestCase]
public void Add()
{
MyMath math = new MyMath();
Assert.AreEqual(31, math.add(20, 11));
}
[TestCase]
public void Sub()
{
MyMath math = new MyMath();
Assert.AreEqual(9, math.sub(20, 11));
}
}
}
I rebuild my solution and using The test explorer panel I can run my test in Visual Studio without problems.
But I need to run my test using the nunit3-console prompt, So, How Can I generate (Or Where Is) a DLL file to run test from the console o using nunit-gui?
I search inside C:\Users\Manuel\source\repos\ConsoleAppForNunit\ConsoleAppForNunit\bin\Debug but there is not a suitable .DLL
There is a Screenshot of that path:
From what you describe, you have never installed the NUnit Console application. You can find it in various places...
If you use chocolatey, use the choco command-line utility to install nunit-consolerunner.
If you prefer to have it installed in a project directory, install NUnit.ConsoleRunner from nuget.org. You can do this within visual studio.
You can download the files from the project site at https://github.com/nunit/nunit-console
I have a solution with 2 projects.
The static web page project
The selenium tests project
Here's my Test File:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace SeleniuumTestSuite
{
[TestClass]
public class HomePageTest
{
private string baseURL = "http://localhost:56403/";
private static IWebDriver driver;
[AssemblyInitialize]
public static void SetUp(TestContext context)
{
driver = new ChromeDriver();
}
[TestMethod]
public void RemoteSelenium()
{
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl(this.baseURL);
}
[TestCleanup]
public void Finally()
{
driver.Quit();
}
}
}
I need to start the localhost project before the test case runs so that navigating to the localhost doesn't result in a 404. I found this post that seems to answer that question but I have no idea what library the solution is using.
I tried using NuGet to download Microsoft.AspNet.WebApi.WebHost but if I try to do private Server _webServer = new Server(Port, VirtualPath, SourcePath.FullName);, VS doesn't recognize Server and has no idea what library to import. So I'm kind of stuck here.
Any idea how to get this to work?
In order to solve the problem, it is necessary to run the two projects at the same time, as mrfreester pointed out in comments:
The main project in order to be able to run the main application.
The test project which will access the running main application to test it.
As mrfreester suggested, you might use two visual studio instances and it will work. Yet, to enhance this solution and manage everything in just one visual studio instance, you can run the main project using Debug.StartWithoutDebugging (default keyboard shortcut is ctrl + f5). This will effectively run the server for the application without starting VS debug mode, allowing you (and your test project) to normally use the application. The application will run even if you close your browser.
Be noted: if you start your application debugging normally, when you stop the execution, the server will stop, and you will have to start again without debugging to be able to pass your tests again.
I have a MVC project which uses the application insight and it is working fine and it is capturing all the telemetric details in azure under the proper dashboard.
I am trying to test this functionality through the unit test project, from the unit test project i am calling the class file which is present in the MVC project.,
It is working and executing the the Funciton1() but these values are not diaplaying under the dashboard...
Any suggestions..
Application 1 -> Testproject C# Class project
[TestMethod]
Method1()
{
MVCAppinsightCls a = new MVCAppinsightCls();
a.Function1();
}
MVC WebApplication
Class MVCAppinsightCls
{
Funciton1()
{
TelemetryClient o = new TelemetryClient();
o.trackEvent("someName");
}
}
When you run the method like this from your Test project, then your Test project is your host so what you need is to add all the configurations related to your App Insights in your Test Peoject too (Instumentation Key settings and all other stuff) so that it sends the logs to App Insights.
You need the ApplicationInsights.config file and also you need to add the nuget packages related to App Insights to your Unit Test project.
I am trying to run unit tests for my C# project with .NET Core.
I am using a Docker container for the runtime.
Dockerfile
FROM microsoft/dotnet:0.0.1-alpha
RUN mkdir /src
WORKDIR /src
ADD . /src
RUN dotnet restore
"NUnit" and "NUnit.Runners" have been added into project.json
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"NETStandard.Library": "1.0.0-rc2-23811",
"NUnit": "3.2.0",
"NUnit.Runners": "3.2.0"
},
"frameworks": {
"dnxcore50": { }
}
Run dotnet restore successfully with the following output
...
log : Installing NUnit.ConsoleRunner 3.2.0.
log : Installing NUnit.Extension.NUnitV2ResultWriter 3.2.0.
log : Installing NUnit.Extension.NUnitV2Driver 3.2.0.
log : Installing NUnit.Extension.VSProjectLoader 3.2.0.
log : Installing NUnit.Extension.NUnitProjectLoader 3.2.0.
log : Installing NUnit.Runners 3.2.0.
info : Committing restore...
log : Restore completed in 4352ms.
I tried to run the tests with:
dotnet nunit
dotnet nunit-console
But it doesn't work.
How am I going to call the runner? Or is there another unit testing framework that works with the current version of .NET Core?
Update 4: The NUnit3TestAdapter v3.8 has been released, so it is no longer alpha.
Update 3:
With NUnit3TestAdapter v3.8.0-alpha1 it is possible now to run the tests using dotnet test command. You just need to have these dependencies in your test project:
<PackageReference Include="nunit" Version="3.7.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0-*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.*" />
You can try it out!
Update 2: Visual Studio 2017 and the move from project.json to csproj made the dotnet-test-nunit test adapter obsolete, so we needed to release another updated adapter to run .NET Core tests. Please see Testing .NET Core with NUnit in Visual Studio 2017 if you are using VS2017 and the new .NET Core tooling. See the update below if you are using project.json.
Update: NUnit now has support for dotnet test, so you no longer have to use NUnitLite. See testing .NET Core RC2 and ASP.NET Core RC2 using NUnit 3.
NUnit console (and the underlying NUnit Engine) do not support running unit tests against .NET core yet. Hopefully we will get that support in NUnit 3.4.
In the meantime, you can use NUnitLite to switch your tests to a self-executing test runner.
I wrote a blog post on the process at Testing .NET Core using NUnit 3. A quick summary is;
Create a .NET Core Console application for your test project.
Reference NUnit and NUnitLite from your test project. You do not need the runner.
Modify main() to execute the unit tests.
It should look like this;
using NUnitLite;
using System;
using System.Reflection;
namespace MyDnxProject.Test
{
public class Program
{
public int Main(string[] args)
{
var writter = new ExtendedTextWrapper(Console.Out);
new AutoRun(typeof(Program).GetTypeInfo().Assembly).Execute(args, writter, Console.In);
}
}
}
For more complete information, see my blog post.
I did as Rob-Prouse suggested, with minor changes. It finally works now.
using System;
using System.Reflection;
using NUnitLite;
using NUnit.Common;
........
public class Program
{
public static void Main(string[] args)
{
var writter = new ExtendedTextWrapper(Console.Out);
new AutoRun(typeof(Program).GetTypeInfo().Assembly).Execute(args, writter, Console.In);
}
}
Some tweaks to the answer, to make it compile.
using System;
using System.Reflection;
using NUnit.Common;
using NUnitLite;
namespace NetMQ.Tests
{
public static class Program
{
private static int Main(string[] args)
{
using (var writer = new ExtendedTextWrapper(Console.Out))
{
return new AutoRun(Assembly.GetExecutingAssembly())
.Execute(args, writer, Console.In);
}
}
}
}
Note that you may also have to convert your project from a class library to a console application.
I'm currently working on a Windows Store Application (Windows 8) for a class and I'm having problems getting my NUnit tests to run.
My Solution/Project setup looks like the following:
TheMetroApp.sln
SQLite-net.csproj - Class Library (Windows Store Apps). Files are pulled from NuGet.
DataModel.csproj - Class Library (Windows Store Apps)
UnitTests.csproj - Unit Test Library (Windows Store Apps). NUnit framework is pulled from NuGet.
TheMetroApp.csproj - A project file which was pulled from one of the Windows SDK examples.
Misc. Dependencies and Utilities
Windows 8 Pro RTM/Visual Studio 2012 RTM
ReSharper 7
NUnit 2.6.1
SQLite (Set up per the instructions here)
UnitTests is dependent upon and references DataModel. DataModel is dependent upon and references SQLite-net. The only thing I have added to the UnitTests project is a single class containing some stub NUnit unit tests. As far as I can tell, these are set up correctly:
[TestFixture]
public class TaskSourceTests
{
#region Private Class Members
private ITaskSource _taskSource;
private String _dbPath;
#endregion
#region Testing Infrastructure
[SetUp]
public void SetUp()
{
// This part makes NUnit/ReSharper have problems.
_dbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "UnitTestDatabase.sqlite");
}
#endregion
#region Misc. CRUD stuff
[Test]
public void CreateTaskTest()
{
// Save the task.
Task task = new Task( "Some Task", "lol.", DateTime.Now, false );
_taskSource.Save( task );
// Confirm that it is in the task db.
using( SQLiteConnection db = new SQLiteConnection( _dbPath ) )
{
const String query = "SELECT * FROM Task WHERE Id = ?";
IList<Task> results = db.Query<Task>( query, task.Id );
Assert.True( results.Contains( task ) );
}
}
// ...and so on [but with stubs that are basically Assert.Fail( "" )].
#endregion
}
TheMetroApp is one of the Windows 8 SDK sample projects, but with some custom XAML forms thrown in. I'm not having any problems with this project.
My issue is that none of the Unit Test runners that I have tried to use are working.
When I try to use the official NUnit x86 Test runner (version 2.6.1), my tests fail due to certificate related issues (see here):
UnitTests.TaskSourceTests.CreateTaskTest:
SetUp : System.InvalidOperationException : The process has no package identity. (Exception from HRESULT: 0x80073D54)
ReSharper's NUnit test runner fails for the exact same reason. Unfortunately, it doesn't look like there is currently a workaround for that.
I have also tried using the test runner built into Visual Studio 2012 (through the NUnit Visual Studio Test Adapter). When I try to run my tests using "Run All", I get the following output:
------ Run test started ------
Updating the layout...
Checking whether required frameworks are installed...
Registering the application to run from layout...
Deployment complete. Full package name: "GibberishAndStuff"
No test is available in C:\Projects\project-name\ProjectName\UnitTests\bin\Debug\UnitTests.dll. Make sure that installed test discoverers & executors, platform & framework version settings are appropriate and try again.
========== Run test finished: 0 run (0:00:09.4873768) ==========
Something strange I have noticed is that if I select a specific test in the Test Explorer and tell it to run, I get a slightly different error message:
Could not find test executor with URI 'executor://nunittestexecutor/'. Make sure that the test executor is installed and supports .net runtime version 4.0.30319.18010.
This is kind of perplexing because I have the NUnit Test Adapter installed. I'm not seeing anything similar to my issue on the launchpad page for the test adapter.
I'm not really sure where I should proceed from here. If this doesn't work I don't mind reworking my project to use xUnit.net, Microsoft's unit testing framework or something else. It would be pretty awesome if I could get NUnit working though.
Thanks!
I have a Windows 7 Phone app which had the same issue that you have. My solution was to create a separate "linked" project which compiles the code using the standard .net libraries. The linked project will have no issues with unit test / NUnit.
See the following for more information:
http://msdn.microsoft.com/en-us/library/ff921109(v=pandp.40).aspx
http://visualstudiogallery.msdn.microsoft.com/5e730577-d11c-4f2e-8e2b-cbb87f76c044/
I've ported the app to Windows 8 and have no problems running my test cases.
I've just hit the same error message while creating unit tests for an Universal App (W81 + WP81). The only solution here was to stop using NUnit and use MSTest only.