Running .NETPortable NUnit tests on linux with mono - mono

I have a xamarin PCL project with a an NUnit project.
I want to run the NUnit tests in the mono-nunit docker image.
I run the tests with:
nunit -framework=4.0 bin/Debug/MY_AppTests.dll
and get:
.NETPortable test assemblies are not yet supported by the engine
Is this final? Is there a way I can run .NETProtale tests assemblies in the mono-nunit image?

This is not final, we will be adding Portable (or more likely .NET Standard) support into the engine soon, hopefully for the 3.7 release.
Meanwhile, switch your test project to an executable, add a reference to NUnitLite and add the following code to program.cs
public class Program
{
public static int Main(string[] args)
{
return new AutoRun(typeof(Program).GetTypeInfo().Assembly)
.Execute(args, new ExtendedTextWrapper(Console.Out), Console.In);
}
}
This will create self-executing tests. You can pass in most command line options that you would normally pass into the NUnit Console.
For more information, see Testing .NET Core RC2 Using NUnit 3. Except for the .NET Core project format, the information is the same.
There is also some documentation on NUnitLite.

Related

Nunit-console fails to run tests depending on ASP.NET Core dependencies

I have an issue running nUnit Tests that depend on ASP.NET core framework.
They run perfectly fine and green in VisualStudio 2019.
I can also run them in command line using 'dotnet test'. However, whenever I run them using nunit-console I get the following error:
System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore.Http,
Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
The system cannot find the file specified.
This only applies to tests which use HttpContext or are mocking the session - so they indeed have dependency on ASP (rest of the tests run fine):
var mockSession = new Mock<ISession>();
// or
var httpContext = new DefaultHttpContext();
All the projects are .net 5.0 and targeting only 5.0. I use nUnit 3.13.2 and NUnit3TestAdapter 4.2.1. I used nunit-consolerunner 3.14
The UnitTest project references my main ASP.NET Core MVC project.
I have tried numerous ways to get those assemblies into the output folder, but this particular one newer is copied and I don't want to do it manually.
I have also tried adding the framework reference to the UnitTest project directly (in csproj), but no help:
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
Since nunit-console normally runs in full framework, I have also tried running the core version of it: nunit.consolerunner.netcore (also 3.14). But with the same result.
The command line I'm using:
C:\Users\XXX\.nuget\packages\nunit.consolerunner.netcore\3.14.0\tools\netcoreapp3.1\any\nunit3-console.exe .\bin\Debug\net5.0\MyProject.UnitTests.dll
I also tried the version which was earlier recommended at github for forcing particular runtime to be used:
dotnet.exe --fx-version 5.0.13 "C:\Users\XXX\.nuget\packages\nunit.consolerunner.netcore\3.14.0\tools\netcoreapp3.1\any\nunit3-console.dll" .\bin\Debug\net5.0\MyProject.UnitTests.dll
Is it just a limitation of the not-so-mature yet nunit-consolerunner.netcore. Or maybe it's just missing setup of the project?

Issue with nested DLL referencing in a self-contained .Net Core web app running as a Windows service

The Error
First off, lets start with the error message:
System.BadImageFormatException: 'Could not load file or assembly 'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058)'
In my experience, this type of error usually occurs if the DLL is missing, or it's not in the expected format/version. However, I cannot work out what I need to do to resolve this error.
The Setup
The main project is an ASP.Net Core 3.1 application.
It is configured to be able to run as a Window service and support Web API and MVC frameworks. So my Program.cs looks something like this:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
var builder = Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
builder.UseWindowsService();
return builder;
}
}
That part all works great, I can run the app as a Windows service and serve razor views and web API endpoints, etc.
However, there are more projects in this solution. I have a couple of .Net 4.6 class libraries projects that are referenced by the service app. So my solution looks something like this:
Service App (NET CORE 3.1)
Lib 1 (NET 4.6)
Lib 2 (NET 4.6)
Service App references Lib 1, and Lib 1 references Lib 2.
Any code is either of these two libraries works fine and can be used by the service app.
The problem is that Lib 2 references System.Management DLL, and this is what is causing the error. Whenever I use a function that uses that DLL, I get the error above.
The Attempts So Far
My attempts so far have including:
Setting the DLL to "copy to output"
Adding the DLL reference directly to the web service app
Manually copying the DLL to the output folder. Both debug, and publish folder (note: publish is set to "self contained" application)
The Question
Basically, how can I resolve this issue? Or at least, how can I further debug the problem to potentially find a solution.
I can't repro this at the time, but I suspect this to be a bug in the way that the self-contained publication process using dotnet publish resolves transitive dependencies.
The point is, your .NET Core 3.1 application doesn't run on .NET Framework, while the library you reference, call it "Lib 2", does.
And your Lib 2 has a reference to the NuGet package System.Management, which, when installed on a project targeting .NET Framework ... does nothing, except making your project reference the .NET Framework reference assembly for System.Management, found in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1. At runtime, the .NET Framework will load the appropriate assembly from the GAC, but the .NET Core runtime does not do this.
Now when publishing your .NET Core app, MSBuild walks through the dependencies to be copied, and (again, I suppose) looks at the reference assembly and goes "I want that one!" and copies it to your output directory.
But you can't run reference assemblies, you can just reference them, so at runtime, your application blows up with an exception stating exactly that:
Reference assemblies should not be loaded for execution
A workaround, but definitely not a solution, is to reference the same System.Management package from your .NET Core application, where the package will actually extract a DLL containing runnable code, and that one will be copied to your output directory. But this might cause other issues, such as the DLL being overwritten during build with the reference one.
I'd suggest looking at GitHub whether this is a known issue, or that this is something caused by your project setup.

My asp.net core 2.1 web app can't run on hosting server

Php and html file run perfectly on hosting but when i try to run my asp.net core 2.1 web app its can not execute on hosting server.
its php code
and here is
asp core web code
this is asp core web uploaded file and folder publish from visual studio
Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
I answered a similar question I believe the following answer will be useful to you. You want to use the built in publish feature with a custom profile to upload directly from VS https://stackoverflow.com/a/53860224/10782174
Are you sure that you have deployed your project properly? The following is steps to publish your .net core:
Step 1: Download VS 2017 from Microsoft website
Step 2: Open solution within vs 2017, you will be told once this happens, you can no longer open the project within vs 2015, however they do backup automatically a version at 2015
step 3: The xproj file at the heart of the application is transformed into a csproj file. This filetype is now editable by right clicking on the Main project heart within the directory, and selecting edit {!ProjectName}.csproj, it should open an XML file format.
step 4. Add these lines to the project. (Please note, the project may contain some of these lines already).Some has to add the runtimeidentifiers and supperdocker targets to a property group that already existed.
step 5. Open the project within the command prompt in the usual method (where you right click the project name, open file explorer at the project location and then crtl+shift clicking within the file explorer and selecting the open with command prompt option.
Step 6. Run: dotnet publish -c Release -r win7-x64 within the command prompt
NOTE: Please run the following command: npm install -g bower - within the nugget package manager console, this is meant to install bower globally or something along those lines. This process will publish a self-contained application of your project within the root directory>Bin>Release>Win64>publish
Please do not forget to add the following lines:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<RuntimeIdentifiers>win7-x64;win7-x86;ubuntu.16.04-x64;</RuntimeIdentifiers>
<SuppressDockerTargets>True</SuppressDockerTargets>
</PropertyGroup>
I would recommend you to use Windows server. You can use asphostportal.com and they support .net core.

Using F# type providers in tests

I'm creating a class library with integration tests. I'm using combination of FsCheck and xUnit here. Tests are supposed to run queries on the database to verify an output. I'm using FSharp.Data.TypeProviders with System.Data.Linq (so Linq2Sql type provider) to access the database - type provider is defined inside test class library itself.
However when I'm trying to build a project, I'm getting a Static linking may not use assembly that targets different profile. error. Class library, where tests are located is targeting .NET Framework 4.5 (FSharp runtime 4.4.0). I've also set binding redirects for FSharp.Core assembly, however this doesn't change anything.

Force MSBuild 14.0 in psake build for C# 6.0 code base

I recently updated to Visual Studio 2015 and am using the new C# 6.0 features. In VS, everything builds correctly.
However, I use PSake as build script language, and the build always fails at places where I use c# 6.0 features. How can I tell psake to use MSBuild 14.0, so that the new c# 6.0 features build correctly?
Tried & failed:
Passing in the framework version to psake: Unknown .NET Framework version, 4.6
Call the vsvars32.bat of VS2015 prior to invoking psake. PSake still uses the old MSBuild version.
I had the same issue & fixed it by configuring PSake build script to use .Net Framework 4.6, simply by adding Framework "4.6" in the beginning of the file. You may check this unit test from PSake code base dotNet4.6_should_pass.ps1
The solution described by #WahidShalaly seems to be the correct one, so use that. However, on my machine it still didn't find the correct MSBuild version so here is the workaround that I have in place instead:
Add the following task to your PSake script:
Task SetEnvironment {
Exec { & $env:VS140COMNTOOLS\vsvars32.bat }
}
This sets the environment for VS2015 compilation from within PSake.
Now add a dependency to SetEnvironment from every task that calls MSBuild and use msBuild.exe instead of just msbuild. Example:
Task Compile -Depends SetEnvironment {
Exec { & "msbuild.exe" "theSolution.sln" /p:VisualStudioVersion=14.0 }
}