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.
Related
I've tried using Environment.CommandLine to check the executable, but that didn't work, it always shows the dll file. Any idea on how to know if myapp.exe file was used to run the app or if dotnet myapp.dll was used instead?
I created a dotnet core console app and ran the following code:
using System;
using System.Diagnostics;
namespace TestApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Process.GetCurrentProcess().ProcessName);
}
}
}
When running dotnet run it shows dotnet. I built a standalone version dotnet publish -c Release -r win10-x64 and it shows TestApp.
I'm not sure how reliable this is though.
I'm trying to get pythonnet to work in my .Net Core app running on Linux.
I've made a reference to Python.Runtime.dll (which I got from nuget) in my .Net Core project.
My code is:
using System;
using Python.Runtime;
namespace pythonnet_w
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start");
using (**Py.GIL()**) {
// blabla
}
Console.WriteLine("End");
}
}
}
I get this runtime error:
Unhandled Exception: System.MissingMethodException: Method not found: 'System.Reflection.Emit.AssemblyBuilder
System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)'.
at Python.Runtime.CodeGenerator..ctor()
at Python.Runtime.DelegateManager..ctor()
at Python.Runtime.PythonEngine.Initialize(IEnumerable`1 args, Boolean setSysArgv)
at Python.Runtime.PythonEngine.Initialize(Boolean setSysArgv)
at Python.Runtime.PythonEngine.Initialize()
at Python.Runtime.Py.GIL()
at pythonnet_w.Program.Main(String[] args) in D:\Development\~.Net libraries (3.part)\phytonnet\.Net Core test (phytonnet)\c#\pythonnet_test\Program.cs:line 10
/usr/sbin/pythonnet_w: line 5: 19487 Aborted dotnet "/usr/share/pythonnet_wit/pythonnet_w.dll"
Tried to find a solution in these threads but without any luck:
How do I run a py file in C#?
Call Python from .NET
UPDATE:
I tried to open \pythonnet-master\src\runtime**Python.Runtime.csproj** in Visual Studio to see if I can compile it to .Net or .Core, but I can only compile to .Net framework.
I found this article "How to port from .net framework to .net standard"
Is that what I have to do?
I finally had success by using a self-compiled Python.Runtime.dll as of version 2.4.0. There are two options to create a working DLL:
Remove the other target framework net461 from the respective project file (leaving only netstandard2.0).
Run dotnet build using the appropriate options
For option 2, the following works(in Windows, Mac and Linux):
Clone the pythonnet repo (https://github.com/pythonnet/pythonnet)
In the pythonnet folder, cd src\runtime
Run dotnet build -c ReleaseWinPY3 -f netstandard2.0 Python.Runtime.15.csproj in Windows(in Mac/Linux, replace ReleaseWinPY3 with ReleaseMonoPY3 because the former use python37 and the later use python3.7)
Set DYLD_LIBRARY_PATH in Mac or LD_LIBRARY_PATH in linux(Windows skip):
export DYLD_LIBRARY_PATH=/Library/Frameworks/Python.framework/Versions/3.7/lib
Use the built DLL bin\netstandard2.0\Python.Runtime.dll as DLL reference in your Visual Studio .NET Core project (mine targets netcoreapp2.2, netcoreapp3.1 is also tested ok), e.g. in conjunction with the following code,
using System;
using Python.Runtime;
namespace Python_CSharp
{
class Program
{
static void Main(string[] args)
{
using (Py.GIL())
{
dynamic os = Py.Import("os");
dynamic dir = os.listdir();
Console.WriteLine(dir);
foreach (var d in dir)
{
Console.WriteLine(d);
}
}
}
}
}
You can host the IronPython interpreter right in your .NET application. For example, using NuGet, you can download the right package and then embed the script execution (actually the IronPython engine) right into your application.
Ref:
https://medium.com/better-programming/running-python-script-from-c-and-working-with-the-results-843e68d230e5
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
I wrote some tests and add solution that execute it smth like this:
class Program
{
static void Main(string[] args)
{
SmokeTests test = new SmokeTests();
test.Init();
test.OneCanLoginMail();
test.Cleanup();
}
}
And test example in another project,with Nunit and Selenium WebDriver libraries.
[Test]
public void OneCanLoginMail()
{
steps.LoginMail(USERNAME, PASSWORD);
Assert.True(steps.IsLoggedIn(USERNAME));
}
If i start up project via console everything ok and tests executed as expected.
Then i installed Jenkins,install MSBuild plugin, configured it and succesfully built project.
1>Done Building Project "C:\Users\Admin\Documents\Visual Studio 2015\Projects\QAlabs\test\StartUp\StartUp.csproj" (build target(s)).
Build succeeded.
0 Warning(s)
0 Error(s)
But tests are not running.What should i add to view assert log and execute tests?I think i need xml file somewhere smth like maven build file but for .Net
To run your NUnit tests, you have to execute nunit-console.exe after your MSBuild step.
Jenkins batch
I have tested .NET Core on Windows & Ubuntu succesfully ( Console & ASP.NET 5 app).
I am now trying to run a simple console app, followed by a web app on OpenSUSE 13.2 x64. I have followed the tutorial found here , but with no success.
I have installed the DNX using the dnvm tool, and tried a simple hello world program:
program.json
{
"version": "1.0.0-*",
"dependencies": {
},
"frameworks" : {
"dnx451" : { },
"dnxcore50" : {
"dependencies": {
"System.Console": "4.0.0-beta-*"
}
}
}
}
program.cs
using System;
public class Program
{
public static void Main (string[] args)
{
Console.WriteLine("Hello, Linux");
Console.WriteLine("Love from CoreCLR.");
}
}
Running
dnvm list
Active Version Runtime Architecture OperatingSystem Alias
------ ------- ------- ------------ --------------- -----
1.0.0-rc1-final coreclr x64 linux
1.0.0-rc1-final mono linux/osx default
* 1.0.0-rc1-update1 coreclr x64 linux
1.0.0-rc2-16237 coreclr x64 linux
The problem is that neighter dnu restore / dnu build / dnx run does anything, it throws no error and does nothing.
According to the guys developing .NET core, this is due to an incorrect version of libicu. So, I have downloaded the source files from here and used this tutorial to build and install libicu52.1.
unzip icu4c-52_1-src.zip
cd icu
mkdir build
cd build
../source/runConfigureICU Linux/gcc CXXFLGS="-D__STRICT_ANSI__ "
make -j4
sudo make install
export LD_LIBRARY_PATH=/usr/local/lib64