Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

NCrunch attributes on interface?
GreenMoose
#1 Posted : Monday, May 08, 2017 1:47:32 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 6/17/2012(UTC)
Posts: 312

Thanks: 48 times
Was thanked: 33 time(s) in 32 post(s)
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:
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("http://localhost"))
    
    [Test]
    [ExclusivelyUses("WebResource")]
    public void TestSomething() {
        _invoker.TestSomething();
   }
}
//assembly2
public class RemoteTest { ... }
public class LocalTests : RemoteTest, IBusinessTests {
    private IBusinessTests _invoker = new MyTestInvoker(new ApiClient("http://someUrl"))
    
    [Test]
    [ExclusivelyUses("WebResource")]
    public void TestSomething() {
        _invoker.TestSomething();
   }
}




Thanks.
Remco
#2 Posted : Monday, May 08, 2017 11:00:03 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 4,815

Thanks: 639 times
Was thanked: 744 time(s) in 709 post(s)
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.
GreenMoose
#3 Posted : Tuesday, May 09, 2017 6:28:23 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 6/17/2012(UTC)
Posts: 312

Thanks: 48 times
Was thanked: 33 time(s) in 32 post(s)
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).
Remco
#4 Posted : Wednesday, May 10, 2017 2:16:17 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 4,815

Thanks: 639 times
Was thanked: 744 time(s) in 709 post(s)
GreenMoose;10359 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.
GreenMoose
#5 Posted : Wednesday, May 10, 2017 6:42:06 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 6/17/2012(UTC)
Posts: 312

Thanks: 48 times
Was thanked: 33 time(s) in 32 post(s)
Great. New feature request added at https://ncrunch.uservoic...xt-or-similar-to-get-in .
1 user thanked GreenMoose for this useful post.
Remco on 5/10/2017(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.030 seconds.