Daily Usage Issues

NCrunch attributes on interface?

Started by GreenMoose on 6,546 views

Hi.

NCrunch does not seem to care if an attribute is used on interface method, but only when used on the test method itself. Is this by design?

Scenario: I have 2 test projects, 1 for external system HTTP tests and 1 for local HTTP tests. In order to avoid duplicating the tests I have an interface which declares the tests. I would like NCrunch attributes to work on interface level.

In code below, I need to duplicate both [Test] and [ExlusivelyUses] on implementations in order for NCrunch to pick up methods as tests, as well as reading the attributes.
(It would be nice for NCrunch to see if one test methods are interface implementations and if so pick up any attributes defined in that one).

Example code:

public interface IBusinessTests {
    [Test]
    [ExclusivelyUses("WebResource")]
    void TestSomething();
}

public class MyTestInvoker : IBusinessTests {
    public MyTestInvoker(IApiClient client) {
        _client = client;
    }
    public void TestSomething() {
        _client.DoSomething();
    }
}

//assembly1
public class DbTest { ... }
public class SystemTests : DbTest, IBusinessTests {
    private IBusinessTests _invoker = new MyTestInvoker(new ApiClient("(- BROKEN LINK -)"))
    
    [Test]
    [ExclusivelyUses("WebResource")]
    public void TestSomething() {
        _invoker.TestSomething();
   }
}
//assembly2
public class RemoteTest { ... }
public class LocalTests : RemoteTest, IBusinessTests {
    private IBusinessTests _invoker = new MyTestInvoker(new ApiClient("(- BROKEN LINK -)"))
    
    [Test]
    [ExclusivelyUses("WebResource")]
    public void TestSomething() {
        _invoker.TestSomething();
   }
}




Thanks.

Edited

Hi, thanks for sharing this.

When NCrunch scans for attributes attached to a method, it searches only through base classes - not interfaces.

Although it could be possible to adjust this, I have some reservations for doing so. The code that performs this scanning is performance critical and adding an extra dimension (interfaces) to it will likely slow it significantly. It might be better to put this through uservoice instead, as I think it would be a bad idea to implement such a feature without first being sure that it isn't a niche area that only very few people would use.
Ok thanks. As an alternative approach, maybe it is possible to have something like NUnit's TestContext e.g. NCrunch.TestContext.CurrentTest.Attributes or similar?

Then one could take actions depending on existence of them (in my case fail the test if the actual tests are not having required attributes).
GreenMoose wrote:Ok thanks. As an alternative approach, maybe it is possible to have something like NUnit's TestContext e.g. NCrunch.TestContext.CurrentTest.Attributes or similar?

Then one could take actions depending on existence of them (in my case fail the test if the actual tests are not having required attributes).


This is certainly worth a feature request. It would perform better and might also be useful for other things.
Great. New feature request added at https://ncrunch.uservoice.com/forums/245203-feature-requests/suggestions/19224088-introduce-ncrunch-testcontext-or-similar-to-get-in .

Post a reply

Log in to reply.