Erez
Erez

Reputation: 6446

NUnit attributes

I'm using NUnit and apply on some of my test the category attribute:

[Category("FastTest")]

These are tests that must run very fast ,less than 10 seconds. so I also decorate them with

[Timeout(10000)]

And now the question:

How can I do that every time I decorate a method with [Category("FastTest")] behind the scenes it will be decorated automatically with [Timeout(1000)] ?

Upvotes: 6

Views: 4136

Answers (5)

Pedro
Pedro

Reputation: 12328

Visual Studio allows you to create custom code snippets, which are similar to the Resharper templates Marius mentions, but doesn't cost anything to make.

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

I don't think it's very good idea.

  1. Category attribute used for grouping tests, not for setting tests expectations.
  2. How will other developer know, that tests from group "FastTest" should run within 10 seconds? Why not 2 seconds? Or maybe 100 milliseconds?
  3. You will stuck with fixed timeout for all tests in category. How to set one test for 2 seconds, other for 10 seconds?
  4. You will not save much time doing this.

Of course you can do it. AOP. Reflection. But simplest way - group all fast tests in one test fixture and decorate it with [Timeout] attribute.

Upvotes: 3

Richard Forrest
Richard Forrest

Reputation: 3616

Create a custom attribute FastTest that inherites from TimeoutAttribute. This attribute would then implment its own Category naming convention. Something like this should work

public class FastTestAttribyte :TimeoutAttribute
{
    protected string categoryName;

    public FastTestAttribyte (int timeout):base(timeout)
   {
       categoryName = "FastTest";
   }

    public string Name { get return categoryName; }
}

Edit

It will work if you decompile the attribute this is what it does

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple=true, Inherited=true)]
public class CategoryAttribute : Attribute
{
    // Fields
    protected string categoryName;

    // Methods
    protected CategoryAttribute()
    {
        this.categoryName = base.GetType().Name;
        if (this.categoryName.EndsWith("Attribute"))
        {
            this.categoryName = this.categoryName.Substring(0, this.categoryName.Length - 9);
        }
    }

    public CategoryAttribute(string name)
    {
        this.categoryName = name.Trim();
    }

    // Properties
    public string Name
    {
        get
        {
            return this.categoryName;
        }
    }
}

I think Nunit will use reflection to Name property of the atttribute.

Upvotes: 0

Marius
Marius

Reputation: 9674

Create a Resharper live-template that spits out both attributes when writing "catfast" for example. Or buy PostSharp and let postshart AOP-adorn all methods which are marked with the specified category.

Upvotes: 3

Davide Piras
Davide Piras

Reputation: 44605

I understand what you mean/need but I don't think is possible. Are you expecting the IDE to add another attribute when you add one or nUnit to know what a category is and apply certain extra attributes for tests of certain categories?

Sounds like a good idea but in case sounds more like nUnit configuration rather than pure c# attribute chaining which I am not aware of if exists.

Upvotes: 0

Related Questions