I'm trying to run some unit tests on a .NET project (not .NET Core). Visual Studio Code is able to recognize the project, intellisense works and it even offers the links to run and debug unit tests:
The problem is that clicking the unit test does not work. It gives the following error:
Build FAILED.
/usr/local/share/dotnet/sdk/1.0.4/Microsoft.Common.CurrentVersion.targets(1111,5): error MSB3644: The reference assemblies for framework ".NETFramework,Version=v4.6.2" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.
My guess is that it's trying to use the .NET Core MSBuild, but this is not a .NET Core project.
If I use mono's msbuild from the command line, the project builds. I'm also able to run the unit tests from the command line using NUnit's runner (from the NuGet package).
Do you know if it is possible to configure Visual Studio Code, on a Mac, so that it can run unit tests on a .NET project?
Related
I have a project where I use Azure Durable Functions, and they are available only on .NET Standard 2. So, it defines which class library can be used in testing projects. But, I cannot put together a library where either xUnit, NUnit or MSTest is used in unit/integration testing.
Adding NUnit to a project where .NET Standard 2 is class library fails with the following error:
INFO: Restoring packages for
C:\VSTS\github.com\netstandardXunitMsTestNunit\src\Netstandard2xUnitMsTestnUnit\nunit\nunit.csproj...
DEBUG: Restoring packages for .NETStandard,Version=v2.0... DEBUG:
Resolving conflicts for .NETStandard,Version=v2.0... ERROR: Cycle
detected. nunit -> NUnit (>= 3.9.0). DEBUG: Checking compatibility
of packages on .NETStandard,Version=v2.0. DEBUG: Checking
compatibility for nunit 1.0.0 with .NETStandard,Version=v2.0.
The error is the same for xUnit (just the error message talks about xUnit cycle).
Both error can be reproduced in Rider and Visual Studio 2017 Enterprise too. I tried it again after I cleaned nuget cache. The result is the same.
In case of MsTest, possible to add ms test libraries, but test discovery does not work neither Rider and nor Visual Studio.
Is it even possible unit test a .NET Standard 2 library?
Is there anything I can do beside waiting for these projects to pick up .NET Standard 2 stuff?
I created a small sample project, can be found here: https://github.com/SayusiAndo/netstandard2xunitresharper
There is no runtime for .NET Standard, so it will not execute your tests.
Your test assembly must target an executable platform, such as a version of .NET Framework or .NET Core.
<TargetFramework>net470</TargetFramework>
Or
<TargetFramework>netcoreapp2.0</TargetFramework>
See Running .NET Standard binaries on different frameworks for more details.
.NET Standard is a specification that each .NET Standard version(such as .NET Framework, .NET Core and Xamarin) defines the set of APIs that all .NET implementations must provide to conform to that version. You library has a value for TargetFramework of netstandard2.0 means you can reference the logic library not only from a .NET Core app, but also from an app built for .NET Framework or Xamarin.
However, You can’t build apps for it, only libraries. Here's the MSDN doc about .NET Standard.
So if you want to test the library, you need to specify the targets of which your library would support. And if you want to support multiple .NET version then you should test them all to make sure your library can run on these targets correctly. Here's the configuration of target framework in .csproj:
Single target:
<TargetFramework>net461</TargetFramework>
Multiple targets:
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
Create a new unit test project in the same solution that targets say .Net Framework 4.6.1 if your class library is to be used by an application that targets .Net Framework 4.6.1 so you test with the same combination of frameworks.
Add a reference to the class library project under references in the unit test project.
Add the xUnit and xUnit.runner.visualstudio nuget packages to the unit test project.
Rename the unit test class to something relevant, and replace the using MSTest directive with using XUnit.
Start writing and running tests.(build/re-build solution so it updates the tests list in the test explorer for each new test).
I have recently released my private unit test platform that does .NETStandard 2.0 just fine. Unless you have put a lot of time and effort into your tests you might want to have a look at Nuclear.Test.
Basically if you target your test project at .NETStandard it will execute these tests in a .NETFramework and .NETCore process to see if they both work correctly.
Unfortunately it requires .NETStandard 2.0 as a minimal version so far. Luckily that's what you are using.
This solution is neither NUnit nor xUnit nor MSTest but it will do exactly what you described.
I'm not even sure if what I'm trying to do is possible...
Sample code: here
Basically, I've built a netstandard20 class library which according to the .NET implementation support list should be compatible with a framework461 project (providing you have the .NET Core 2.0 SDK).
I can build the netstandard20 class library no worries and get the DLL back (it's included in the sample code).
This is where the problem starts, when attempting to run msbuild .\netstandard_test.sln to build the framework461 project I get a whole bunch of (what I believe to be misleading) errors about 'netstandard' not being referenced (which it is).
However, if I run dotnet build (or build from VS2017) everything works as expected.
I've tried importing various versions of NetStandard, NetStandard.Library and NetStandard.Library.Framework, as well as referencing Microsoft.DotNet.BuildTools but that didn't appear to help.
Is there any way to build this using msbuild? We have a monolithic build and deployment process and I'd rather not have to change it if it can be avoided. Who knows what else would go wrong!
To build with .NET Core or .NET Standard you need to use the dotnet msbuild command from the .NET Core SDK rather than just plain msbuild.
dotnet msbuild .\netstandard_test.sln /p:Configuration=Release
This will also work with .NET Framework 4.6.1 (and older versions).
Do note that Microsoft has provided install scripts to make installing the .NET Core SDK painless on continuous integration servers.
You need to use a recent 15.* version (currently 15.5.*) of MSBuild and make sure that the "Cross platform development" workload is installed in visual studio. This adds the required components to locate the .NET Core SDK, which contains MSBuild SDKs like Microsoft.NET.Sdk or Microsoft.NET.Sdk.Web used by .NET Standard and .NET Core projects.
You can then use msbuild from the developer command prompt to build these projects. I suggest adding /restore (msbuild >= 15.5.*) to make sure that a NuGet restore happened for sdk-based projects.
Since VS 2017 does not install a "global" version of MSBuild, be sure to use the version of MSBuild installed in the VS 2017 folders (check with msbuild /version or where msbuild).
dotnet msbuild has limitations when you use .resx files containing file references or non-string properties. Also, it does not support COM references or building strong named assemblies. If you need any of these features, use msbuild (VS 2017) over dotnet msbuild (.NET Core CLI)
Info about my setup:
Resharper 2016.3.2
XUnit 2.2.0
VS 2017 RTM
Test Project .net 4.5.2
Asp.net Core Project .net 4.5.2
Repo Steps:
Create an asp.net core site targeting .net 4.5.2
Add a unit test project or class library to the solution and reference the asp.net project
Create a xunit Fact test that targets a class in the asp.net project
Run the unit test using resharper test runner
Error:
System.BadImageFormatException
Could not load file or assembly 'AFAEMS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
at AFAEMS.Tests.Class1.return_errors_if_missing_fields()
Notes:
I've done a lot of searching and cannot find any similar issues to this. I can run a unit test with a simple assert that is not targeting the asp.net core class files. I couldn't get the VS test runner to find my xunit tests (maybe b/c resharper test runner is enabled?).
These were things I found but don't indicate how to fix with the new xproj and app.config using MSBuild for VS 2017.
https://blogs.msdn.microsoft.com/visualstudioalm/2016/05/30/announcing-mstest-framework-support-for-net-core-rc2-asp-net-core-rc2/
Testing Asp.Net Core on full .NET framework
https://github.com/dotnet/sdk/issues/926
In Visual Studio change the target from “X86 ” to “Any CPU”.
Also,you will get this error if you try to host an application with a 32-bit assembly reference ,on IIS running on a 64 bit server/machine. To fix this error set "Enable 32 bit applications" flag to true in Advanced settings of the application pool hosting the application in IIS.
I was able to get this fixed by using the following settings in my Resharper unit testing options.
After upgrading our project to .net 4.6.1, some of the unit test written for MEF Open Generics turned red when they are run using mstest.exe that shipped with visual studio 2015. After further investigation I found that the Open Generics types are not getting registered in the mef catalog. Same test used run fine with vs2015 mstest before upgrading projects to .net 4.6.1.
The problem is only with mstest.exe. The same test runs fine if I run them using visual studio test. runner(vstest.console.exe). Alsohe Open Generics types are properly registered when application is launched.
I tried using .runsettings file with Framework45 value and the tests run green. When TargetFrameworkVersion is not specified I believe the mstest is falling back to .net 40 where mef open generics were not supported.
the .runsettings file was not needed when projects were on 4.5. It seems there is some issue with mstest.exe shipped with vs2015. I tried vs2015 update 3 and the issue still exists. Does anyone know work around other than using .runsettings
I'm trying to run an NUnit case. When I load the library, I get the following message
System.BadImageFormatException : Could not load file or assembly ... or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
You may be attempting to load an assembly built with a later version of the CLR than the version under which NUnit is currently running, 2.5.5.0727.
I've searched everywhere to find out what is causing this problem. I found another post that mentioned changing in the config file to the current .NET version that is installed. I did that and I am still getting the same error.
I'm currently using NUnit 2.4.8 and the latest version of Selenium 2.0. Any thoughts on this?
EDIT - I've noticed a lot of people are running in to this issue when using the x64 exe when they should be using x86. My library was compiled with an x86 install of Visual C# Express and I'm using the x86 NUnit executable.
Which version of the framework have the unit tests been build against? Old version of NUnit (< 2.5.6) have issues with unit tests build against the .NET 4 framework. If you building against the .NET 4 framework then I would highly suggest upgrading to the latest NUnit both to run your tests, and build against.