According to searchsoftwarequality.techtarget.com, unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. Unit testing is often automated but it can also be done manually.
The following steps should be performed in order to do unit testing:
MainActivity
class should contain the following:public class MainActivity : TestSuiteActivity { protected override void OnCreate (Bundle bundle) { // tests can be inside the main assembly AddTest (Assembly.GetExecutingAssembly ()); // or in any reference assemblies // AddTest (typeof (Your.Library.TestClass).Assembly); // Once you called base.OnCreate(), you cannot add more assemblies. base.OnCreate (bundle); } }
TestsSample
is also part of this AndroidTestProject
project. This file contains the following code:[TestFixture] public class TestsSample { [SetUp] public void Setup () { } [TearDown] public void Tear () { } [Test] public void Pass () { Console.WriteLine ("test1"); Assert.True (true); } [Test] public void Fail () { Assert.False (true); } [Test] [Ignore ("another time")] public void Ignore () { Assert.True (false); } [Test] public void Inconclusive () { Assert.Inconclusive ("Inconclusive"); } }
MainActivity
class so it looks like the following:[Activity (Label = "test", MainLauncher = true)] public class MainActivity : TestSuiteActivity { protected override void OnCreate (Bundle bundle) { AddTest (typeof (TestsSample).Assembly); base.OnCreate (bundle); } }
/tests.dll
and then on test.TestsSample
. You will now have a list of all the available tests:The unit tests of Xamarin are based on NUnit. Let's take a look at the following explanation:
[Test]
: These are annotations for marking the method to use for a test.[SetUp]
: This is for annotations for methods that should be executed before the tests begin.[TearDown]
: This is for methods that should be executed when all the tests are completed. We use them to clean/close all the resources we have used, if any, during the tests.Each test contains an assertion which will be evaluated, and if the assertion is true, the test is validated. There are plenty of static methods that you can use on the Assert
class, such as:
Assert.AreEqual(obj1, obj2)
: This asserts that obj1
and obj2
are equalAssert.AreNotEqual(obj1, obj2)
: This asserts that obj1
and obj2
are not equalAssert.AreSame(obj1, obj2)
: This asserts that obj1
and obj2
are the sameAssert.DoNotThrow(TestDelegate)
: This asserts that the test does not throw the specified delegateAssert.Catch(TestDelegate)
: This asserts that the specified delegate is caughtAssert.IsNotNull(Object)
: This asserts that the object is not null
Assert.IsNull(Object)
: This asserts that object is null
Assert.IsTrue(Object)
: This asserts that the Boolean is true
Assert.IsFalse(Object)
: This asserts that the Boolean is false
Learning how to use NUnit in depth is out of the scope of this book, but if you are interested in learning more about it and to use it efficiently, I would recommend that you refer to Pragmatic Unit Testing in C# with NUnit, Second Edition, by Andy Hunt or the official documentation of nunit at http://www.nunit.org/index.php?p=documentation.
18.117.153.38