NUnit3 throws exception System.Runtime, Version=4.2.0.0 not found while trying to run test in NUnit3 console - visual-studio-2017

Environment:
.Net Core 2.0
visual Studio Professional 2017
NUnit3
OpenCover (For code coverage)
ReportGenerator(For code coverage report)
Hi Nunit Team,
I am pretty familiar with MS Test framework and have worked on that extensively but was testing how suitable NUnit framework would be and so wanted to do a small POC around that. I created a small classlibrary and a unit test project. Below are the NuGet package been installed:
NUnit 3.7.1
But when I try to run the test cases from console runner like below I get exception saying System.Runtime, Version=4.2.0.0 assembly not found.
C:\Users\UN\.nuget\packages\nunit.consolerunner\3.8.0\tools>nunit3-console.exe D:\Code_Trials\BoMLibrary\BomTest\bin\Debug\netcoreapp2.0\BomTest.dll
Exception detail:
Errors, Failures and Warnings
Invalid : D:\Code_Trials\BoMLibrary\BomTest\bin\Debug\netcoreapp2.0\BomTest.dll
Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
----> Could not load file or assembly 'System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Run Settings
DisposeRunners: True
WorkDirectory: C:\Users\Smukherjee.nuget\packages\nunit.consolerunner\3.8.0\tools
ImageRuntimeVersion: 4.0.30319
ImageTargetFrameworkName: .NETCoreApp,Version=v2.0
ImageRequiresX86: False
ImageRequiresDefaultAppDomainAssemblyResolver: False
NumberOfTestWorkers: 4
Am really not sure why so? No matter what I try not able to pass through this error. Moreover, when I try to install the assembly from Nuget I don't even find a assembly with version 4.2.0.0.
Tried installing the version 4.3.0.0 of System.Runtime but no luck. Can you please help on this.
Strange thing, I have got a sample unit test project (as part of opencover nuget install) which targets .Net Framework 4.5 and have NUnit2.6.7 package installed and when I try to run that using NUnit3 console it works like charm.
Even, installing NUnit3 latest stable version 3.10.1.0 doesn't help either. Also, it's surely not an Opencover issue since opencover console just passes the test run request to NUnit console. So the issue has to be around NUnit.
Let me know if you need more information around this.

Found the answer to this here https://github.com/nunit/nunit/issues/2763
NUnit Console doesn't run .NET Core tests - you need to use dotnet test instead. Take a look at this page:
https://github.com/nunit/docs/wiki/.NET-Core-and-.NET-Standard

Related

Guidance for running tests using MSTest v1 in Visual Studio 2017 15.8.1

I upgraded to Visual Studio 2017 last week. A solution with several test projects using MSTest v1 seemed to be fine at first. Most test projects execute tests fine. This is .NET Framework, not .NET Core.
However, there is one project where I am encountering issues. It's an integration test project, meaning it runs web application tests using the ChromeDriver and Selenium. This project references NUnit, though that reference comes from another project reference. I am not actually using NUnit as my test framework.
This is the error when trying to run any test in that particular test project:
Could not find test executor with URI 'executor://mstestadapter/v1'. Make sure that the test executor is installed and supports .net runtime version .
Test project {name} does not reference any .NET NuGet adapter. Test discovery or execution might not work for this project.
It's recommended to reference NuGet test adapters in each test project in the solution.
Disabling the option indicated below made everything work:
I found this solution after browsing the preview release notes at https://learn.microsoft.com/en-us/visualstudio/releasenotes/vs2017-preview-relnotes#--visual-studio-2017-version-158-preview-4 (scroll down to "... What's New ..." and then .NET Test Adapter Extension). It mentions this breaking change (though it didn't break my other projects), but there is no mention of what to actually do to solve the problem.
Of course, long-term, keeping that option unchecked doesn't seem feasible. So I am looking for guidance on how to proceed.
I already tried to see if I could "quickly" upgrade to MSTest v2, but that had an issue because System.Runtime 4.1.2 couldn't be found. I tried adding System.Runtime 4.3.0 via NuGet, but that didn't solve the issue.
I was getting a similar issue running Xamarin.UITests and resolved it by installing the "NUnit 2 Test Adapter" Extension in Visual Studio. In Visual Studio 2017 you do this from Tools > > Extensions and Updates... > Online > Search "NUnit Adapter" > click the NUnit 2 Test Adapter then the Download button to install. Restart Visual Studio when prompted to complete the installation.
It would be interesting to understand how the MSTest V1 framework assembly(Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll) is referenced in this project. The compatibility checkbox that also ended up being disabled in the image above should ensure that this project has MSTestV1 tests and enabled run tests. If it isn't then for some reason the detection logic is busted and needs to be fixed - a snippet on how the assembly is referenced would be helpful there.
As for moving to MSTest V2, this blog should help: https://blogs.msdn.microsoft.com/devops/2016/06/17/taking-the-mstest-framework-forward-with-mstest-v2/ . And for the specific issue you seem to be running into, this github issue on the repo sounds similar: https://github.com/Microsoft/testfx/issues/184 .
Caltor's answer pretty much solved it for me: it was the NUnit Adapter I needed. But I followed slightly different steps. Here they are for VS 2019 (but probably apply to VS 17 too):
Right clicked on the References node under my project in the solution explorer
Chose "Manager NuGet packages"
Clicked Browse
Searched for nunit3testadapter
Hit install on the result that popped up
The issue for me was similar to the OP's - the adapter was installed on some projects but not others. But confusingly this wasn't immediately visible under the "References" node for that project. The only node relating to nunit under that References node said "nunit.framework". So the references for test with and without the adapter looked the same.
"... but that had an issue because System.Runtime 4.1.2 couldn't be found."
I think this is a setting in the app.config that does not get updated when the project settings are updated. I searched for System.Runtime under the packages folder and then changed my app.config to match what I found, and then that particular problem went away.

build dotnet solution in travis against netstandard20 and framework462

TLDR; I've got a mixed solution: NetStandard 2.0 for the library I'm actually developing and .Net Framework 4.6.2 (or alternatively dotnetcore 2.0) for the tests. How to configure travis.yml to build the library and the test project as well.
I'm actually working on a client for a rest api. I decided to use .NET Standard 2.0 as project type. As it is a REST based api, I decided to use Newtonsoft.Json to deserialize and serialize the content.
A problem occurred: the nuget package Newtonsoft.Json 10.0.3 doesn't support NetStandard 2.0 by now. But I found the commit ab3315f does. So I cloned the repo and build the netstandard library on my own as a workaraound till the new version is available in NuGet.
I added the project to CI system travis following this post: https://rimdev.io/building-net-core-on-travis-ci/
Now I added a test project to my solution using Nunit to run unit tests and integration tests for my library. No I'm facing 2 problems and I need to solve one:
problem 1: When my test project targets .NET Framework, I'm able to execute all the unit tests without any problems, but now travis complains based on my travis.yml there's no mono installed to build the complete solution correctly.
my question here: how to change my .travis.yml to build both project types within the solution.
problem 2: When my test project targets dotnetcore, my local build works, but when I try to execute tests locally, I get a runtime error:
System.IO.FileNotFoundException : Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=null'.
That's why I didn't test travis build by now, as I want to get it running locally first if this is the solution. I don't know why it doesn't find the dependency.
Any suggestions either for problem 1 or for problem 2?
I ended up adding NuGet package of Newtonsoft.Json 10.0.3 to my dotnetcore test project though it is not directly used there. Now the runtime error diappeared. So I am able to test locally and remotely as well and let travis CI build my project.

Run ReSharper plugin tests using R# test runner

I'm building a R# plugin and decided to cover it with tests. However, I noticed, that plugin tests cannot be run using the R# runner if plugin target version is the same as R# version.
For instance, if you try to run tests using JetBrains.ReSharper.SDK.Tests of version 2016.2.20160818.171542, you'll get the following error:
ERROR System.TypeLoadException:
Method 'RunTests' in type 'JetBrains.ReSharper.UnitTestRunner.nUnit26.BuiltInNUnitRunner' from assembly 'JetBrains.ReSharper.UnitTestRunner.nUnit26, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.
In previous versions the error was different.
Likely, that happens because R# runner and SDK assemblies make a mix.
Is there any way to configure project/R# to be able to run plugin unit tests using the R# runner? Likely, R# dev team uses their own runner.
Thanks!
Unfortunately, this is a bug in the ReSharper runner in 201.6.2. Something to do with having multiple copies of the same assemblies in the same process - the assemblies from the install, and the assemblies from the bin folder. Something's getting messed up along the way.
You can subscribe to, vote and track this issue: RSRP-460399
The workaround is to install an older version of ReSharper into a Visual Studio "experimental instance" aka "custom hive", and use this instance to develop and test the plugin. The normal instance can be used for other day-to-day development.
You can download an older version of ReSharper here (e.g. 2016.1.2), and in the installer, click Options and enter the name of a custom instance, e.g. "Plugins". Then create a new shortcut to Visual Studio and set the command line to "devenv.exe /rootSuffix Plugins" (or whatever you called the custom hive).
R# and SDK must be either of different major versions, or of binary compatible versions. Unfortunately, within EAP the internal APIs inside the test runner have changed, so R# and SDK from different EAPs are not compatible.

Microsoft Fakes: Team Build throws IO file not found exception

I am using Microsoft Fakes for my unit testing. On my local dev machine, i can create a fake assembly of a class library and everything works fine. But when check-in the team build on the build server throws
"System.IO.FileLoadException: Could not load file or assembly 'Business.DataAccess.Core, Version=19.0.0.9999, Culture=neutral, PublicKeyToken=925fe695533b6fca' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)"
In my local dev machine 'Business.DataAccess.Core.dll' version is 19.0.0.9999, but on the build server it will be get a proper version number such as 19.0.0.1250.
Business.DataAccess.Core.fakes xml file is as follows. I have removed the version tag.
<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
<Assembly Name="Business.DataAccess.Core" />
</Fakes>
I tried both by committing FakeAssemblies folder and not committing it. In both times its not working. My assumption was that fake assemblies will be created at compile time. But it's not working.
How can i get team build to run my unit tests using Microsoft Fakes.
When i fake just the EntityFramework.dll and commit the fake assemblies it works. This proves our build server is equipped with testing MS fakes. Build server has got VS 2013 ultimate version installed.
Much Appreciated!!!
The problem was with 'Business.DataAccess.Core.dll' getting a new version on the build server.
So, To fix this problem, removed 'GlobalAssemblyInfo.cs' file from the project and then in 'AssemblyInfo.cs' added a version.
I had to check-in FakeAssemblies folder with the fake assembly as well for this to work.

NCover not finding unit tests

I am trying to install NCover in an existing C# .NET project for work. Unfortunately, after I download NCover and installed the Visual Studio extension, it wasn't finding any of the unit tests. The answers I've seen/heard seem to be in the vain of 'It wasn't working...time passed...it started working.'
I can run the tests with Visual Studio's Test Explorer, but I need to get NCover installed to see the code coverage. I had NCover installed previously, and other people on my team are getting it to work within the same project.
I am using Visual Studio 2013 with the newest version of NCover Desktop (v 5.1).
I am getting two errors in the console log.
The first switches between saying:
Agent Error (v4x64): An error occurred while loading assembly
and
Agent Error (v4x64): There was a problem loading test assembly [test path]. Could not get bytes from loader AppDomain
The second (after I try to run NCover) says it 'Could not load config' for the test dll.
I copied the 4 XML config file (Bolt.ext.config, Bolt.Extension2008.dll.config, NCover.exe.config, ncover.axplorer.exe.config) from one of my team members, so I'm pretty sure that I have my config correct.
Has anyone else experience this/have any suggestions? Help would be greatly appreciated!
Further information:
When I hover over the grayed out test dll in the bolt test panel, I get the following error:
Assembly is not loaded. Select and run to build and load this assembly
I was looking at this post, but it doesn't seem like any of the solutions are working for me...
After working with some of the other developers in my office, we discovered the issue was with the NCover version. For our tests to run, we needed to use NCover v5.0. Neither v5.1 or v5.2 would work with our setup. Not sure what changed in v5.1 that wouldn't let it find the tests, but I'm glad it's working now :)