Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom attribute that combines Category and Repeat #2589

Closed
GitSIPA opened this issue Nov 30, 2017 · 5 comments
Closed

Custom attribute that combines Category and Repeat #2589

GitSIPA opened this issue Nov 30, 2017 · 5 comments

Comments

@GitSIPA
Copy link

GitSIPA commented Nov 30, 2017

Short explanation of what I want to achieve:

I want a custom attribute named "CandidateTest" that sets a category name as well as a Repeat attribute for this test, so that I don't have to use the Repeat separately on my test.

The Reason why I want to do this is that we have a "StressTester" job, that executes all the tests with this category for 50 times each and the test should pass all the 50 executions.
This helps us figure out flaky tests before removing the attribute and letting them run with our normal test set.

The problem here is, that I can't figure out how to set a category as well as setting the Repeat(50).

How I tried to do it:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class CandidateTestAttribute : CategoryAttribute, ITestAction
{
    public CandidateTestAttribute() : base("CandidateTests")
    {
    }

    public void BeforeTest(ITest test)
    {
        TestContext.CurrentContext.Test.Properties.Set("Repeat", 50);
    }

    public void AfterTest(ITest test)
    {
        // do nothing
    }

    public ActionTargets Targets { get; private set; }
}

but it looks like setting the repeat value in BeforeTest is too late for NUnit to be considered.

I also tried it the other way round = CandidateTest is derived from RepeatAttribute and to set the category, but then the category doesn't seem to be considered when executing tests

Any suggestions from your side on how to get this done?
Or is there maybe even a possibility to get a kind of StressTester attribute from NUnit in the future?

@ChrisMaddock
Copy link
Member

I also tried it the other way round = CandidateTest is derived from RepeatAttribute and to set the category, but then the category doesn't seem to be considered when executing tests

This definitely seems like a better solution to me - RepeatAttribute contains a lot more logic you'd need to re-implement - which you haven't done in your example above. You'd need pretty much everything here.

Can you share how you attempted this the other way around? What gave you the impression the category wasn't considered?

@CharliePoole
Copy link
Contributor

ITestAction isn't a suitable interface for your purpose. It performs actions before and after the test, which is not what you are trying to do.

Because Repeat is the more complicated attribute, I would derive from that and add the behavior of Category.

Repeat Attribute is already derived from Property Attribute, so it has a Properties dictionary to which you can add the category value you want to use.

@rprouse
Copy link
Member

rprouse commented Nov 30, 2017

I am going to close this as a question since we wouldn't implement this in NUnit itself. If you have trouble, feel free to continue to post here, we will see it. I would suggest that you look a the code for the Repeat and Category attributes to get an idea what they do.

https://github.com/nunit/nunit/blob/master/src/NUnitFramework/framework/Attributes/RepeatAttribute.cs
https://github.com/nunit/nunit/blob/master/src/NUnitFramework/framework/Attributes/CategoryAttribute.cs

@rprouse rprouse closed this as completed Nov 30, 2017
@rprouse rprouse added this to the Closed Without Action milestone Nov 30, 2017
@GitSIPA
Copy link
Author

GitSIPA commented Dec 1, 2017

Thanks @ChrisMaddock and @CharliePoole for your suggestions and ideas!
I managed to solve my problem!

@rprouse thanks! I intended it to be a question.

I derived my custom attribute from RepeatAttribute and set the category name by using the Properties dictionary.

Here is the code that works for me:

[AttributeUsage(AttributeTargets.Method)]
public class CandidateTestAttribute : RepeatAttribute
{
    public CandidateTestAttribute() : base(50)
    {
        this.Properties.Set("Category", "CandidateTests");
    } 
}

@CharliePoole
Copy link
Contributor

@GitSIPA Looks good. Glad we could help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants