MS Test and NUnit

When I wrote my Socket library I also wrote a set of Unit Tests for it, using the MSTest framework built into Visual Studio. Now that I have released it, including a build for Android, I thought I had better run the unit tests on Android as well.

Actually, strictly speaking, they aren’t unit tests because they involve creating random length files of random data, and checking they are sent across correctly, but it’s close enough.

Creating a unit test project for Xamarin.Android is easy enough, but it couldn’t use the unit test classes I had created as they were. MS have obviously based their unit test system on NUnit, but there are some very annoying differences. The most obvious is in the names of the attributes used to flag test classes and methods. For example, a test class in MS Test is flagged with [TestClass], but in NUnit it is [TestFixture]. This means I have had to go through all the tests and use a compilation variable to set these attributes. (DEVICE is used for the Android build).

#if DEVICE
    [TestFixture]
#else
    [TestClass]
#endif
    public class DiscoverTests {
#if DEVICE
        [Test]
#else
        [TestMethod]
#endif
        [Timeout(30000)]
        public async Task BasicDiscover() {
...

Messy, but it works.

The Assert class in each is similar, but there are some differences. NUnit seems to be rather more advanced, but some methods have been removed from the NUnitLite version that ships with Visual Studio, so I’ve also ended up with things like

#if DEVICE
    Assert.That(reply, Is.InstanceOf<RecTextMessage>());
#else
    Assert.IsInstanceOfType(reply, typeof(RecTextMessage));
#endif

One odd thing turned up with async test methods, where MS seems a bit ahead. I couldn’t get them to work at all on Android, but after some googling I found a reference to it on the NUnit Bugzilla, where someone suggested adding a [Timeout] attribute to the test method. I can’t see why this works (must be something to do with the threading), but it does.

I’m hoping this will also apply to an iOS test project, because that’s the only way I will have to test the library on iOS for the moment.

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.