Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit attributes

Tags:

c#

.net

nunit

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)] ?

like image 937
Erez Avatar asked Jun 15 '11 12:06

Erez


People also ask

What is NUnit setup attribute?

SetUpAttribute (NUnit 2.0 / 2.5) This attribute is used inside a TestFixture to provide a common set of functions that are performed just before each test method is called. It is also used inside a SetUpFixture, where it provides the same functionality at the level of a namespace or assembly.

What are annotations in NUnit?

NUnit is basically composed of attributes, or annotations. These indicate to the framework to execute the tests that are implemented in the class, and also how to interpret them.

What is TestFixture attribute used in NUnit?

The [TestFixture] attribute denotes a class that contains unit tests. The [Test] attribute indicates a method is a test method. Save this file and execute the dotnet test command to build the tests and the class library and run the tests. The NUnit test runner contains the program entry point to run your tests.


2 Answers

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.

like image 117
Marius Avatar answered Sep 20 '22 22:09

Marius


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.

like image 20
Sergey Berezovskiy Avatar answered Sep 21 '22 22:09

Sergey Berezovskiy