vatbub
vatbub

Reputation: 3109

NUnit cancel a test in SetUp

We use NUnit for our automated tests and we have some criteria that must be met in order for a test to run. In particular, we are using Xamarin.UITest for mobile UITesting and we need to check if we are currently testing a platform that we want to test. Unfortunately, there is no way for us to do it using categories.

The way we do it right now is to have a list of platforms we want to test. In the [SetUp] method we then check if the current platform is contained in that list and if not, we abort the test. Currently, we abort the test by letting it fail with Assert.Fail(). However, we would prefer to let the test exit silently, with no failiure and no success message, just as if it was never run. Is that even possible and if so, how can it be done?

Here's the current code:

private IList<Platform> _desiredPlatforms;

public IList<Platform> DesiredPlatforms
{
    get
    {
        if (_desiredPlatforms == null)
        {
            // read from environment variable
        }
        return _desiredPlatforms;
    }
}

[SetUp]
public void BeforeEachTest()
{
   // Check if the current platform is desired
   if (!DesiredPlatforms.Contains(_platform))
   {
        Assert.Fail("Aborting the current test as the current platform " + _platform + " is not listed in the DesiredPlatforms-list");
   }
   _app = AppInitializer.Instance.StartApp(_platform, _appFile);
}

Upvotes: 0

Views: 2375

Answers (1)

Chris
Chris

Reputation: 6042

Sounds like either Assert.Inconclusive() or Assert.Ignore() better fit what you're looking for.

How I imagine you'd really want to do this however, is with something equivalent to NUnit's PlatformAttribute - which will skip the tests on the irrelevant platforms. The NUnit PlatformAttribute isn't implemented for the .NET Standard/PCL versions of the framework yet - however, there's no reason you couldn't make a Custom Attribute, to do a similar thing for your particular case. You can look at the NUnit code for the PlatformAttribute as an example, and write your own equivalent of PlatformHelper, to detect the platforms you're interested.

Edit: I've linked to the NUnit 3 docs, but just read Xamarin.UITest is limited to NUnit 2.x. I believe everything I've said is equivalent in NUnit 2 - you can find the NUnit 2 docs here: http://nunit.org/index.php?p=docHome&r=2.6.4

Upvotes: 3

Related Questions