Unit testing

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.

Getting ready

In this recipe, we will reuse the solution we created in the first recipe.

How to do it...

The following steps should be performed in order to do unit testing:

  1. Add an Android unit test project to your solution.
    How to do it...
  2. The 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);
      }
    }
  3. Another file named 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");
      }
    }
  4. Modify your 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);
      }
    }
  5. Set your test project by right-clicking on it, then select the Set as Startup project option. Finally, run your project in your emulator and the following screen will appear:
    How to do it...
  6. Click on /tests.dll and then on test.TestsSample. You will now have a list of all the available tests:
    How to do it...
  7. Click on the first one and then, click on run this test and watch it fail.
    How to do it...

How it works...

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 equal
  • Assert.AreNotEqual(obj1, obj2): This asserts that obj1 and obj2 are not equal
  • Assert.AreSame(obj1, obj2): This asserts that obj1 and obj2 are the same
  • Assert.DoNotThrow(TestDelegate): This asserts that the test does not throw the specified delegate
  • Assert.Catch(TestDelegate): This asserts that the specified delegate is caught
  • Assert.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

See also

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.153.38