---
title: "Creating custom test attributes easily with NUnit 2.2.1"
url: "https://osherove.com/blog/2004/12/4/creating-custom-test-attributes-easily-with-nunit-221.md"
canonical: "https://osherove.com/blog/2004/12/4/creating-custom-test-attributes-easily-with-nunit-221.html"
published: "2004-12-04T21:00:00"
updated: "2004-12-04T21:00:00"
tags: []
author: "Roy Osherove"
description: "One of the new features in NUnit 2.2.1 is the ability to extend the NUnit framework in two ways. The first one is to be able to create your own custom test suites (for example, if you wanted to run your unit tests in a…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Creating custom test attributes easily with NUnit 2.2.1

By Roy Osherove December 4, 2004

**Historical article** · Originally published December 4, 2004; technical details may be outdated.

One of the new features in NUnit 2.2.1 is the ability to extend the NUnit framework in two ways.

The first one is to be able to create your own custom test suites (for example, if you wanted to run your unit tests in a multi threaded fashion this would be a good candidate to go with).

The second one is the ability to create new test case attributes that add behavior to the running test case. A good example of that would be to be able to create a transaction in the beginning of a test and roll back that transaction at the end of it, thus rolling back any state that had changed in the database during the test.

I’ll show here how to do the second one, in a very easy manner.

Currently to go in the custom attribute route you need to create three separate classes to do your bidding.

- A custom attribute class, which is just a fancy name for your intentions but does not actually implement anything
- a custom test case, which overrides the standard Nunit test case behavior and lets you inject your on implementation along the way (this is where the real work is done)
- a test case builder class which simply returns an instance of your new test case.

I won’t go into too much detail as to how you actually do this, because there’s a much much simpler way. In this alternate way to still get all the great benefits of the old way, but:

- You create only one simple attribute class that inherits from a base class
- You override two methods : “BeforeRunTest” and “AfterRunTest”

That’s it.

Here’s how it looks in this alternate way, if I were to create a class that simply writes to the console before and after the test is executed:

---

\[AttributeUsage(AttributeTargets.Method, AllowMultiple\=false)\]

\[TestBuilder(typeof(CustomTestAttributeBase.CustomTestBuilder))\]

public class SimpleTraceAttribute:CustomTestAttributeBase

{

   public override void BeforeTestRun (TestCaseResult testResult,

                                       TemplateTestCase testCase)

            {

                  Console.WriteLine(“START: “\+ testCase.Name);

            }

   public override void AfterTestRun (TestCaseResult testResult,

                                    TemplateTestCase testCase)

            {

                  Console.WriteLine(“END: “\+ testCase.Name);

            }

      }

---

There you go. No interception or custom version of NUnit is needed. Only the latest version which is NUnit 2.21 is needed for this.  Here are the steps to allow this cool thing in your project.

1. Create a new class library project
2. Add a reference to NUnit.Framework and Nunit.Core
3. [Download this base class](https://osherove.com/downloads/nunitCustomTestBase.zip) and add it to your test project
4. Start adding your own derived classes like shown above.
5. That’s it.

The base class in the link actually implements all the three steps needed to create a custom test attribute, but allows you, the developer, to focus on the important things in life. you know, like, actually getting the job done and write a good unit test easily.

Have fun, and feel free to give feedback!


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
