Unit test in a class library

Hi all
Is there any tutorials or something like that which shows how to create unit test in a class library like C# project in Visual Studio.
I can't get it to work.
I tried adding NUnit as reference but still can't get it to work.

Regards
Jakob

Tagged:

Answers

  • JakobJensenJakobJensen DKMember ✭✭

    Yes it is. Keep getting different errors

  • mattwardmattward GBMember Xamurai

    @JakobJensen - Can you post the errors? How are you creating and setting up the test project?

    The NUnit Library project should work out of the box. This project template provides an empty test class and also adds a supported version of the NUnit NuGet package.

    Xamarin Studio does not currently support NUnit 3 which may be the problem you are seeing if you not using the NUnit Library project template.

  • JakobJensenJakobJensen DKMember ✭✭
    edited February 2016

    Im not home now, but i did use nunit 3 because its comming up under package when searching for it...
    Post the errors later.

    The error is something that it cant resolve dependencies...

    Can i still use the GMS Framework under a library? Have to make unit test for some gps functionality

  • JakobJensenJakobJensen DKMember ✭✭

    Im using unit.framework 2.6.4.14350
    The error im getting is

    Could not load file or assembly 'Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065' or one of its dependencies.
    Stack trace:
    at (wrapper managed-to-native) System.Object:__icall_wrapper_mono_remoting_wrapper (intptr,intptr)
    at (wrapper remoting-invoke) MonoDevelop.NUnit.External.ExternalTestRunner:Run (MonoDevelop.NUnit.External.IRemoteEventListener,NUnit.Core.ITestFilter,string,string,System.Collections.Generic.List1<string>,string,string,string) at (wrapper remoting-invoke-with-check) MonoDevelop.NUnit.External.ExternalTestRunner:Run (MonoDevelop.NUnit.External.IRemoteEventListener,NUnit.Core.ITestFilter,string,string,System.Collections.Generic.List1,string,string,string)
    at MonoDevelop.NUnit.NUnitAssemblyTestSuite.RunUnitTest (MonoDevelop.NUnit.UnitTest test, System.String suiteName, System.String pathName, System.String testName, MonoDevelop.NUnit.TestContext testContext) [0x001c2] in /Users/builder/data/lanes/2508/0b60eecd/source/monodevelop/main/src/addins/NUnit/Services/NUnitAssemblyTestSuite.cs:429

  • mattwardmattward GBMember Xamurai

    I am guessing that you created an Android library project, so it references Mono.Android, and then added a NUnit to that project.

    This is problematic since NUnit is a desktop .NET framework assembly whilst Mono.Android is not. If you set Mono.Android to be local copy true then you can fix the assembly resolution problem but you may hit other problems later on if you are using any Android specific code.

    If you writing code that you want to test with Android on a device or emulator there is the Android Unit Test project template which does not use NUnit but uses an Android testing library. The tests for this do not integrate into the Unit Tests window, instead you run the project.

  • JakobJensenJakobJensen DKMember ✭✭

    I know you can do it with a Android test project. Its just really time consuming. It takes quite some time to start up the test project and run it. I was hoping it could be more automated

  • NuninzNuninz USMember ✭✭

    @mattward I have a shared project using SQLite (mmvmcross). I want to tests those methods, do I need to create a project for Android and another one for iOS? Does it make sense to duplicate the tests ?

  • mattwardmattward GBMember Xamurai

    @Nuninz - It depends if the tests will be calling any platform specific code. If the tests are not calling any platform specific code then you should be able to just create a normal NUnit project, reference the shared project and test it. I would try to avoid duplicating any tests.

  • NuninzNuninz USMember ✭✭

    @mattward yes that's the problem, I need to initialise my database service with a factory that is passed from droid or iOS. Without this it's impossible to test methods that require access to the database. So I need to create a PCL + Droid + iOS for testing?

  • mattwardmattward GBMember Xamurai

    Can the database be created on a .NET project (i.e. NUnit)? You could use a different database service/factory in the test project - not sure how that is getting injected either through dependency injection or shared source file.

  • NuninzNuninz USMember ✭✭

    Thank you for all you help so far! this is what I'm using http://mvvmcross.com/docs/sqlite-pcl

    Perhaps it doesn't make much sense to use this plugin. I know this is going off topic now, but anyway, I will have a look at creating a database on a .NET project. I don't think the database should depend on anything platform specific

  • DanielbaheDanielbahe ESMember

    @Nuninz said:
    @mattward yes that's the problem, I need to initialise my database service with a factory that is passed from droid or iOS. Without this it's impossible to test methods that require access to the database. So I need to create a PCL + Droid + iOS for testing?

    Unit testing should not depend of nothing.
    If your problem is about dependency you have to use mocks, "Moq"nuget it's a wonderful tool for this.

    To do integration test for logic reasons to do test for each platform.

Sign In or Register to comment.