Building and loading file in NUnit - nunit

I am following a book called "The Art of Unit Testing". I have reached a point where I need to test my test method that I have writtent using NUNit. The author instructs to build the project and then locate the path to the assembly file that was built and give the path to NUnit for testing.
My problem is that I cant seem to get this Assembly file path. Where is it located?
Plus, when I run my code, I am getting the following error:
Error 2 Program 'c:\Users\Documents\Visual Studio 2012\Projects\Loganalyzer\Loganalyzer\obj\Debug\Loganalyzer.exe' does not contain a static 'Main' method suitable for an entry point c:\users\documents\visual studio 2012\Projects\Loganalyzer\Loganalyzer\CSC Loganalyzer
Here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Loganalyzer
{
public class LogAnalyzer
{
public bool IsValidLogFileName(string fileName)
{
if (!fileName.EndsWith(".SLF"))
{
return false;
}
return true;
}
}
}
I am following the exact example that's in the book but cant get it to work as you can see. I will appreciate your help folks.
using Loganalyzer;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LogAnalyzerTest
{
[TestFixture]
class LogAnalyzerTest
{
[Test]
public void IsValidFileName_validFile_ReturnsTrue()
{
//Arrange ( Arranges objects, creating and setting them up as necessary).
LogAnalyzer analyzer = new LogAnalyzer();
//Act
bool result = analyzer.IsValidLogFileName("whatever.SLF");
//Assert ( Asserts that something is as expected)
Assert.IsTrue(result, "file name should be valid");
}
}
}

It seems as though you're trying to run your class project Loganalyzer, but you'll probably be wanting to use some sort of test runner. I prefer TestDriven.net.

NUnit is just the testing framework (very simplified, it specifies the rules for how to set up tests, etc).
What you need is some application or plugin to actually run them. You run the tests in the concole runner, or the GUI runner that come with NUnit for example, or in TestDriven.net (which I've heard is excellent).
Personally, I use the runner that comes with Resharper (although that is only free to try for a month or so).
The point is that you don't have an executable project, but rather a class library, containing stuff to test. The runner runs your tests, which in turn, call your code.

If you want to use the native nunit runner you typically use a class like this
static class NUnitLauncher
{
[STAThread]
static void Main()
{
AppEntry.Main(new[] { Assembly.GetExecutingAssembly().Location });
}
}
You'll also have to have it set as the start-up object (in the project's properties).
You'll also need to reference nunit-gui-runner.dll which you can find in the nunit install directory (normally program files). Mine is at
C:\Program Files (x86)\NUnit 2.5.7\bin\net-2.0\lib
The native nunit runner has it's problems but I find the resharper (6.1) test runner is unstable when debugging - it sometimes bombs out randomly. It also doesnt understand all the different types of parameterised tests you can have in nunit. Hopefully this isnt the case in the newer versions - it's got a much nicer UI.
The problems you describe above are two seperate things - one sounds like it is because you dont have a startup object set; the other sounds like it is confusion about what an assembly is. The executable that gets produced when you compile will contain an assembly - if you point nunit at that then it should work (assuming it has some nunit stuff in there (eg stuff tagged up with [Test] etc))

Related

How to resolve dependency issues with C# script csx file in visual studio code?

I am trying to create an azure function program with a service bus queue trigger template using a csx file. But I am having issues resolving dependencies. To be honest, i am very confused about the project structure that is mentioned in [this doc] (https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp). I've tried two approaches.
First approach...
Create a folder named MyAzureFunc in a visual studio code and manually add a function.json , project.json, project.lock.json, and run.csx. Then, outside of the MyAzureFunc folder I add a host.json and local.settings.json. This approach leads to dependency issues.
Second approach...
Create an Azure Function project with service bus queue template in visual studio code that generates a completely different folder and project structure. I'll then remove most of the auto-generated files and manually add in the structure mentioned in the first approach. This still leads to dependency issues.
Some possible reasons i believe this could be happening is the following...
1. Framework versions - my project.json calls for "net46", but when i create an azure function in visual studio code using the service bus queue template it implements "netcoreapp2.1"
2. The need to run some command to recognize the package dependencies
3. Some project configuration needed to work with csx
I believe the important file here is the project.json. Below is the code...
{
"frameworks": {
"net46":{
"dependencies": {
"MongoDB.Driver": "2.6.1",
"MongoDB.Driver.Core": "2.6.1",
"MongoDB.Bson": "2.6.1",
"SharpZipLib": "0.86.0",
"RabbitMQ.CLient": "5.0.1"
}
}
}
}
Below is the code for the packages i'm trying to use in my csx file...
#r "Newtonsoft.Json"
#r "Microsoft.ServiceBus"
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging; --> ERROR
using Newtonsoft.Json; --> ERROR
using MongoDB.Driver; --> ERROR
using MongoDB.Bson; --> ERROR
using MongoDB.Bson.IO; --> ERROR
using RabbitMQ.Client; --> ERROR
using ICSharpCode.SharpZipLib.Zip.Compression.Streams; --> ERROR

Azure Functions: Can compile but cannot run with custom datalayer library

I've tried to come up with a better title but can't.
The issue is I am new to Azure functions but have made a simple one work that writes to a SQL Azure table. Now I've attempted to build the simplest kind of Entity Framework based Datalayer and uploaded it. Right now it is compiled as .Net 4.6 and using EF 6.1.3.
I'm using a connection string as per the second answer here Second answer and have checked it is being retrieved correctly. Update - I also used this guide.
Removing this {#r "D:\home\site\wwwroot\sharedbin\TestDataLayer.dll"} causes the editor to complain about missing assemblies, so it IS finding the dll in question.
However it will not run - it cannot find TestDataLayer.dll.
I'm only running this in the portal editor (I've not yet mastered deployment direct from a Visual Studio Project - don't laugh :P).
#r "System.Configuration"
#r "System.Data.Entity"
#r "D:\home\site\wwwroot\sharedbin\TestDataLayer.dll"
using System;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.SqlServer;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Net;
using TestDataLayer;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
var connection = ConfigurationManager.ConnectionStrings["sql_connection"].ConnectionString;
using(var db = new SyncDbContext(connection))
{
var RK = new RKAzureTest() {TestField1 = "It finally worked?" };
db.RKAzureTests.Add(RK);
db.SaveChanges();
}
}
[DbConfigurationType(typeof(myDBContextConfig))]
public partial class SyncDbContext : System.Data.Entity.DbContext
{
public SyncDbContext(string cs) : base(cs) {}
public DbSet<RKAzureTest> RKAzureTests {get;set;}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
public class myDBContextConfig : DbConfiguration
{
public myDBContextConfig()
{
SetProviderServices("System.Data.EntityClient",
System.Data.Entity.SqlServer.SqlProviderServices.Instance);
SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.SqlConnectionFactory());
}
}
This is the function.json:
{
"frameworks": {
"net46":{
"dependencies": {
"EntityFramework": "6.1.3"
}
}
}
}
I've compiled the dll itself to .Net 4.6 after a suspicion that the Azure Functions don't support .net 4.7.1 and via Kudu uploaded the compiled dll to a sharedbin folder (checked the path a dozen times!).
This is the error thrown up:
2018-05-01T11:00:00.012 [Warning] Unable to find assembly 'TestDataLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Are you missing a private assembly file?
2018-05-01T11:00:00.012 [Error] Exception while executing function: Functions.TimerTriggerCSharp1. mscorlib: Exception has been thrown by the target of an invocation. f-TimerTriggerCSharp1__514732255: Could not load file or assembly 'TestDataLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
Not quite sure what else can be left - I'm using runtime version 1.0.11702 in the Application settings as I found life got a LOT more complicated if I went onto the Beta version.
If anyone can point me to a working guide for this use case (Database first, EF 6.1.3 etc) I'd be grateful.
Any help offered gratefully received!
Thank you :)
Go to Azure Portal, create a folder called, 'bin' inside your Azure functions using CMD Shell, upload the 'TestDataLayer.dll' file to bin folder which has just been created.
#r "System.Configuration"
#r "System.Data.Entity"
#r "TestDataLayer.dll"
Project structure should look like,
AzureFunctionProjectName001
bin
TestDataLayer.dll
run.csx
project.json
project.lock.json
...
Azure functions should be able to discover your library this time. I believe, EntityFramework works just fine.

Nuget packages with common Names

I was playing with NuGet so I created a project, wrote two simple functions (multiply and add), packaged it with the following spec:
<metadata>
<id>Math</id>
<version>1.0.0.0</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<copyright>Copyright 2015</copyright>
<tags>Matematica, Test</tags>
</metadata>
Then publicized it on my local NuGet.Server feed, added my new nuget package in another sample project, started it and it returns this:
Could not load file or assembly 'Math, Version=1.0.0.0, Culture=en-GB, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
Googled for various time but found nothing (was I the only dumb enough to call a temporary package 'Math'?), so I imagined that somewhat my module name was taboo, changed it into Math_Test, same source code and it worked like a charm.
Is there some more informations out there that specifies which values are taboo?
I can't find anything on nuget official documentation.
The class Math is specified in the following dll (mscorlib.dll) so it doesn't even have the same id...
#region Assembly mscorlib.dll, v4.0.0.0
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll
#endregion
EDIT:
And here is the code of the sample application
using Math;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = -1;
try
{
Common e = new Math.Common();
a = e.Multiply(3, 2);
}
catch (Exception e)
{
System.Console.Out.Write(e);
}
System.Console.Out.Write(a);
System.Console.Read();
}
}
}
Seems like the problem was not the name but the Culture inside the Assembly (which is not the same as the language specified into Nuget spec.
Changing the AssemblyCulture from
[assembly: AssemblyCulture("en-GB")]
to
[assembly: AssemblyCulture("")]
solved the issue.
I found the warning by toggling diagnostic output during build, as suggested by #ShyamalDesai
CSC : warning CS1607: Assembly generation -- Referenced assembly 'Math' has different culture setting of 'en-GB'

Error when calling Native Code using JNI from a Jython module

I am calling a simple HelloWorld program written in C from a Jython module (inside a PyDev project). There is an intermediate Java Class (in a separate Java project) which calls the native code using JNI (Java Native Interface). The native call is successful if I run the Java class directly. But when I call the Java class from my Jython module, I get the following error:
java.lang.UnsatisfiedLinkError: no ctest in java.library.path
I have successfully run several C programs from my Jython module using JNA (Java Native Access). But JNA hits performance (speed) really hard and I want to revisit JNI and fix this problem (also I simply want to know what is going on). The IDE I am using is Eclipse.
Here is the code for the Java class:
package JNIPackage;
public class HelloWorld {
native void helloFromC(); /* (1) */
static {
// Added the line below but still no luck. Was sure this would fix it.
System.setProperty("java.library.path", "/Users/haiderriaz/Desktop/JNI-C");
System.loadLibrary("ctest"); /* (2) */
}
static public void main(String argv[]) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.helloFromC(); /* (3) */
}
}
Running this Java class directly, there is no error and "Hello World" gets printed out. But when I import JNIPackage to my Jython module and then call JNIPackage.HelloWorld, then all of a sudden java can not find ctest. I think this is strange and the problem only exists when calling C code using JNI as opposed to JNA.
Try System.load([full path and filename of ctest]), which works independently from values of LD_LIBRARY_PATH or java.library.path.
To ease user configuration I usually implement my own library-search-mechanism, i.e. make it look for libraries in the working directory and on the classpath too. I know ideologically this is somewhat wrong, but works much smoother for your users. Use java.io.File.exists to confirm the actual location of ctest-file and then use java.io.File.getAbsolutePath() to get the appropriate input for System.load.
System.mapLibraryName(...) can also be useful for this.

Why won't resharper find my unit tests?

For some reason when I open a new solution Resharper always refuses to find unit tests. I spend half an hour struggeling/rebuilding/poking until suddenly Resharper magically finds my unit tests. Once they are found it runs them fine every time.
Test example:
using NUnit.Framework;
namespace NamespaceOfTheCodeToTest.Test
{
[TestFixture]
public class FunctionalityTest
{
[Test]
public void Scenario_Input_Result()
{
}
}
}
I am using RESHARPER->Unit Tests->Run All Tests from Solution
What am I doing wrong here?
It seems the problem was NUnit test adapter was not installed. After installing that visual studio extension all tests where found instantly.
Edit: a better solution is to use "Run Unit Tests" on the test project, folder with test projects or solution.