Documentation

Documentation
V5 Documentation

NCrunch.Framework.DistributeByCapabilitiesAttribute

NCrunch.Framework Attribute

Purpose

The DistributeByCapabilitiesAttribute can be applied to tests, fixtures, or assemblies to simplify the use of NCrunch for multi-platform testing using distributed processing. When NCrunch detects this attribute, it will automatically create multiple clones of the target test(s), one for each parameter passed into the attribute. Each clone will be marked with a required capability equal to their source parameter.

The test clones are effectively treated as separate tests by the entirety of the NCrunch engine. Throughout the UI, they are distinctively marked with a suffix describing their required capability.

Declaration In NCrunch.Framework.dll

namespace NCrunch.Framework
{
    public class DistributeByCapabilitiesAttribute: Attribute
    {
        private string[] _capabilities;

        public DistributeByCapabilitiesAttribute(params string[] capabilities)
        {
            _capabilities = capabilities;
        }

        public string[] Capabilities
        {
            get { return _capabilities; }
        }
    }
}

You can declare this in your own code if you want NCrunch to use it - or otherwise reference the attribute from NCrunch.Framework.dll.

Usage Example

The following example will create 3 different tests, each declared with a required capability targeting a different operating system. It is assumed that such code would be executed on a grid containing at least one machine with each operating system, appropriately configured with matching capabilities.

public class MyTestFixture
{
    [NCrunch.Framework.DistributeByCapabilities("Windows XP", "Windows 7", "Windows 8")]
    [Test]
    public void MyTest()
    {
        // Place code to run on different platforms here
    }
}

Recommendations

When using this attribute, it is possible to have a single test that is run once on every platform using distributed processing.

The following code can also achieve the same objective without using this attribute:

public abstract class MyTestFixture
{
    [Test]
    public void MyTest()
    {
    }
}

[RequiresCapability("Windows XP")]
public class MyTestFixture_OnWindowsXP: MyTestFixture {}

[RequiresCapability("Windows 7")]
public class MyTestFixture_OnWindows7: MyTestFixture {}

[RequiresCapability("Windows 8")]
public class MyTestFixture_OnWindows8: MyTestFixture {}


The above approach requires considerable duplication of code and can be expensive to implement for large suites of many tests. Because the DistributeByCapabilities attribute can also be applied at assembly level, it is a much more effective solution for multi-platform testing requiring minimal changes to test code to implement.

Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download