Can not compile xUnit test classes (via mono) to exe file, using mcs command - mono

I am using MSYS2 and mono in order to run C# project (in .NET Framework) in Linux environment on Windows machine.
My projects are placed in the path: D:\MyRootPath and there I have two separate solutions:
D:\MyRootPath\MainProject - this is my main project;
D:\MyRootPath\xUnitProject- this is a separate solution that contains project that I use for unit testing with xUnit.net framework (for .NET Framework);
Building main project (working sample)
Inside of D:\MyRootPath\MainProject I have a sample class D:\MyRootPath\MainProject\HelloWorld.cs:
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine ("Hello Mono World");
}
}
When I want to build this via mono, to create an .exe file and run it, I am using the following commands inside of the MSYS2 console (this approach is working well):
mcs /d/MyRootPath/MainProject/HelloWorld.cs
mono /d/MyRootPath/MainProject/HelloWorld.exe
Building xunit project (need help here)
Inside of the xUnitProject, I have many classes, where each class represent a test case (and each test case have reference to the main project - MainProject.dll). One example of test case is D:\MyRootPath\xUnitProject\SampleRectangle.cs:
using MainProject;
using System;
using Xunit;
namespace xUnitProject
{
public class SimpleRectangle
{
[Fact]
public void simpleRectangle()
{
//this content is irrelevant to the question, I have a few Assert checks
Assert.Equal("someVariable", someVariable);
Assert.Equal("someOtherVarialble", someOtherVarialble);
}
}
}
Here I need to build this via mono, to create an .exe file and run it. Since I have many classes like SimpleRectangle, I need to run them all and to have separate .exe files for each.
So I am trying to build the test classes with the following (similar as above working example):
mcs /d/MyRootPath/xUnitProject/SampleRectangle.cs -r:/d/MyRootPath/xUnitProject/bin/Debug/MainProject.dll:/d/MyRootPath/xUnitProject/bin/Debug/xunit.core.dll
where I had to add -r flag, since it points to the .dll files that are used.
The output that I got is:
D:/MyRootPath/xUnitProject/SimpleRectangle.cs: error CS0012: The type `System.Attribute' is defined in an assembly that is not referenced. Consider adding a reference to assembly `System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
D:\MyRootPath\xUnitProject\bin\Debug\xunit.core.dll (Location of the symbol related to previous error)
D:/MyRootPath/xUnitProject/SimpleRectangle.cs: error CS0103: The name `Assert' does not exist in the current context
D:/MyRootPath/xUnitProject/SimpleRectangle.cs: error CS0103: The name `Assert' does not exist in the current context
error CS5001: Program `SimpleRectangle.exe' does not contain a static `Main' method suitable for an entry point
Compilation failed: 4 error(s), 0 warnings

Related

Maui App not working with Android Simulator namespace missing

I installed Visual Studio 2022 Preview Getting error when running Maui App using Android or Machine Simulator. Is anyone having the same issue?
Already installed
Single-project MSIX Packaging
Android device Manger
Namespace missing if add the namespace still not working
error "Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name 'MauiApp' could not be found (are you missing a using directive or an assembly reference?) MauiApp37 (net6.0-android)"
Missing namespace if I include it still not working "using Microsoft.Maui.Hosting;"
Second Error
Severity Code Description Project File Line Suppression State
Error NU1605 Detected package downgrade: Microsoft.WindowsAppSDK from 1.0.0-preview3 to 1.0.0-experimental1. Reference the package directly from the project to select a different version.
MauiApp37 -> Microsoft.AspNetCore.Components.WebView.Maui 6.0.101-preview.10.2068 -> Microsoft.WindowsAppSDK (>= 1.0.0-preview3)
MauiApp37 -> Microsoft.WindowsAppSDK (>= 1.0.0-experimental1) MauiApp37
Your main class is MainApplication of type MauiApplication
That means that the template that you used to create that project is old (I think it was preview 6 or 7)
If you create a new .NET MAUI App with a newer template, you will see that the main class is something like this. And it's the only main file
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
return builder.Build();
}
}
So I would recommend creating a new project using a new template, and then migrate the rest of the classes/views (use the same name for your project to make it easier)

XUnit 'Startup' does not exist

In my xunit project i am getting bellow error
The type or namespace name 'Startup' does not exist in the namespace 'TestService' (are you missing an assembly reference?) [Testervice.Tests]
I added the TestService reference to the Testervice.Tests by using
dotnet add reference ..\TestService\TestService.csproj
The reference was added successfully the the test project and its also available in TestService.csproj file. But still i am getting the error. Why i am getting the error.Any one try help me.
I am not sure this will solve your problem but what you need to is create a TestStartUp class which inherits the original Startup.cs to properly build your .net core app. Sample code is shown below.
public class TestStartup : Startup
{
public TestStartup(IConfiguration configuration, IHostingEnvironment hostingEnvironment) : base(configuration, hostingEnvironment)
{
}
}
Hope this solves your problem.

TeamCity - Testing with JUnit

I am using Intellij Idea version 12 (ultimate). Just installed Team City (version 8). One default agent, running in linux.
I've created a very simple test application:
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
public int sum(int x, int y) {
return x+y;
}
}
... and a very simple test...
import junit.framework.Assert;
import org.junit.Test;
public class MainTest {
#Test
public void testSum() throws Exception {
Main test=new Main();
Assert.assertEquals("Sum should be 7",7,test.sum(4,4));
}
}
If I run this in IntelliJ, the test gets run and fails just like it should.
If instead I commit this project and push it up to github, TeamCity sees the change and begins a build. The build fails fairly quickly with the following errors:
/home/ctb/TeamCity/buildAgent/work/742505fa88794219/test/MainTest.java:1: package junit.framework does not exist
import junit.framework.Assert;
^
/home/ctb/TeamCity/buildAgent/work/742505fa88794219/test/MainTest.java:2: package org.junit does not exist
import org.junit.Test;
^
/home/ctb/TeamCity/buildAgent/work/742505fa88794219/test/MainTest.java:12: cannot find symbol
symbol : class Test
location: class MainTest
#Test
^
/home/ctb/TeamCity/buildAgent/work/742505fa88794219/test/MainTest.java:15: cannot find symbol
symbol : variable Assert
location: class MainTest
Assert.assertEquals("Sum should be 7. Loser!!",7,test.sum(4,4));
^
So yeah, I see that TeamCity is not seeing JUnit.
On the TeamCity Discussion forum, one respondent to my question there asked me if junit.jar was added as a dependency (module or library) in the build. It was listed as a module dependency, but for kicks I tried it as a library dependency. I also tried checking and unchecking export and trying the compile and test scopes, but each time I get the same errors. My run configuration is shared.
I am not using Ant or Maven. Perhaps someday, but I'd like to start as simple as possible.
Clearly, I'm missing something, but the documentation on the subject is sparse.
Thank you.
So I heard back from Jetbrains tech support this and, in the interest of completeness and saving someone else the trouble, here's the response I received:
Seems the problem is that junit.jar is not placed in version control
under your project. In order to build your project on TeamCity agent,
the project ideally should be self contained. In your case junit.jar
only exists on your local machine, I suppose there is no such file on
agent at required location. So you have two options actually: put
junit.jar under version control into your project, or define global
library in IDEA and configure this global library on IDEA Project
runner page (Check/Reparse must be started), after that put library
files on all of the agents where your build will be executed.
Personally, I think the first approach is much simpler and better.
I added junit to version control and now the build works properly in TeamCity.

Can't get System.Numerics to work with command-line Mono (mcs) on OS X

We're trying to run a simple Mono script on the command line on OS X. Most scripts work fine for us, but as soon as we try to use System.Numerics, we get "error CS0234: The type or namespace name `Numerics' does not exist in the namespace System."
This isn't too surprising, and should be fixable with an appropriate command-line option to mcs, plus properly set up PKG_CONFIG_PATH... but this is where we get stumped. First, here's the script so you can follow along at home:
using System;
using System.Numerics;
public static class MainProgram {
public static void Main(string[] args) {
Console.WriteLine("Hello world!");
}
}
So next we tried "mcs -r:System.Numerics Test.cs". This produces "error CS0006: Metadata file `System.Numerics' could not be found".
"man mcs" suggests that we can get the other system packages by adding "-pkg:dotnet" to the command line. But that produces:
Package dotnet was not found in the pkg-config search path. Perhaps
you should add the directory containing `dotnet.pc' to the
PKG_CONFIG_PATH environment variable No package 'dotnet' found error
CS8027: Error running pkg-config. Check the above output.
OK then, we had no PKG_CONFIG_PATH, so we tried defining one:
export PKG_CONFIG_PATH=/Library/Frameworks/Mono.framework/Versions/Current/lib/pkgconfig/
This fixes the CS8027; but we still get the CS0234 we started with. And if I combine the -pkg and the -r, e.g. "mcs -pkg:dotnet -r:System.Numerics Test.cs", I get "error CS0006: Metadata file `System.Numerics' could not be found".
I'm stumped at this point... any idea what incantation I'm missing to make System.Numerics work with mcs?
If you're using Mono 2.10.x, you will have to compile with dmcs rather than mcs to enable the 4.0 profile (System.Numerics is a C# 4.0+ feature only).
If you're using Mono 2.11.x or 3.0.x, then mcs by default should select the 4.5 profile. mcs -help should show 2, 4, and 4.5 as possible values for the -sdk option. If it doesn't, then the framework isn't properly installed; I had that once, where I think that /Library/Frameworks/Mono.framework/Versions/Current pointed to the wrong directory; installing a second time fixed that.
Manipulating pkg-config should be unnecessary.

Does mono 2.8 support 'dynamic' keyword?

I tested IronPython on mono 2.8 with the code in the book Professional IronPython p.315 listing 15-3.
using System;
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting.Hosting;
namespace Method2
{
class Program
{
static void Main(string[] args)
{
// Obtain the runtime.
var IPY = Python.CreateRuntime();
// Create a dynamic object containing the script.
dynamic TestPy = IPY.UseFile("TestClass.py");
// Execute the __test__() method.
TestPy.__test__();
}
}
}
I see it's compiled OK, and run without a problem on Windows 7, whereas the mono 2.8 gives me the following error message.
Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:
`Microsoft.Scripting.Hosting.ScriptScope' does not contain a definition for `__test__'
at (wrapper dynamic-method) object.CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object)
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid1
(System.Runtime.CompilerServices.CallSite,object)
at Method2.Program.Main (string[])
I thought Mono 2.8 supports C# 4.0 which has the dynamic keyword, but I see that the 'dynamic' keyword is not fully supported with mono.
Is this a bug of Mono 2.8?
ADDED
This is the python script.
# The class you want to access externally.
class DoCalculations():
# A method within the class that adds two numbers.
def DoAdd(self, First, Second):
# Provide a result.
return First + Second
# A test suite in IronPython.
def __test__():
# Create the object.
MyCalc = DoCalculations()
# Perform the test.
print MyCalc.DoAdd(5, 10)
# Pause after the test session.
raw_input('\nPress any key to continue...')
This is the command that I used
dmcs Program.cs /r:System.Core /r:IronPython.dll /r:IronPython.Modules.dll /r:Microsoft.Dynamic.dll /r:Microsoft.Scripting.dll /r:Microsoft.CSharp.dll
It compiles well, but it still breaks when I run the execution binary. Do I need to have all the dlls in the same directory where the execution binary locates?
The fact that you are getting a Microsoft.CSharp.RuntimeBinder.RuntimeBinderException means that the dynamic keyword worked. The problem you are having is that IPY.UseFile("TestClass.py"); is returning a ScriptScope that doesn't see your test method. So the problem lies with your python source or how you are including IronPython with mono.
The dynamic keyword is certainly supported by Mono 2.8 when using the C# 4 profile.
I guess my question is how you are building this sample?
Just for kicks, I just pasted your sample into MonoDevelop. I had to tell MonoDevelop to use C# 4 instead of C# 3.5 first of all.
The dynamic keyword was introduced in C# 4 obviously.
Also, I did have to include a few assembly references: System.Core, IronPython.dll, IronPython.Modules.dll, Microsoft.Dynamic.dll, Microsoft.Scripting.dll, and Microsoft.CSharp.dll. I am not sure if I needed them all.
If you are building from the command-line you need to use 'dmcs' as the compiler (to indicate the .NET 4 profile) and you need to include the assembly references.
It builds without problems with this configuration (for me at least).
I am responding to this question with respect to Mono 2.10.
I could successfully build and execute the code from command line on Windows 7 without using MonoDevelop. Here are the steps:
Install Mono 2.10.8 on Windows 7.
Install IronPython 2.7.2.1.
"C:\Program Files\Mono\bin" or equivalent and "C:\Program Files\IronPython 2.7" or equivalent should be part of system path.
Both TestClass.py and Program.cs should be in the same folder.
From the dos prompt, corresponding to the folder where TestClass.py and Program.cs exists, execute setmonopath batch file.
From the same dos prompt execute the following command:
dmcs Program.cs /r:System /r:"C:\Program Files\IronPython 2.7\IronPython.dll" /r:"C:\Program Files\IronPython 2.7\IronPython.Modules.dll" /r:"C:\Program Files\IronPython 2.7\Microsoft.Dynamic.dll" /r:"C:\Program Files\IronPython 2.7\Microsoft.Scripting.dll" /r:"C:\program Files\IronPython 2.7\Microsoft.Scripting.MetaData.dll" /r:Microsoft.Csharp
Program.exe should be successfully generated and the same can be executed without any error message.