---
title: "Writing real unit tests for your custom FxCop rules"
url: "https://osherove.com/blog/2007/2/24/writing-real-unit-tests-for-your-custom-fxcop-rules.md"
canonical: "https://osherove.com/blog/2007/2/24/writing-real-unit-tests-for-your-custom-fxcop-rules.html"
published: "2007-02-24T16:39:56"
updated: "2007-02-24T16:39:56"
tags: ["Agile", "Agile Related", "Art Of Unit Testing", "Reflection", "Team Agile News", "Testing Guidelines", "Unit Testing"]
author: "Roy Osherove"
description: "Update : You might also be interested in FxCopUnit for FxCop Integration testing. It’s possible to create elegant small tests which require little to no configuration whatsoever that test out your custom rule’s ability…"
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/"
---

# Writing real unit tests for your custom FxCop rules

By Roy Osherove February 24, 2007 In [Agile](https://osherove.com/blog/category/agile), [Agile Related](https://osherove.com/blog/category/agile-related), [Art Of Unit Testing](https://osherove.com/blog/category/art-of-unit-testing), [Reflection](https://osherove.com/blog/category/reflection), [Team Agile News](https://osherove.com/blog/category/team-agile-news), [Testing Guidelines](https://osherove.com/blog/category/testing-guidelines), [Unit Testing](https://osherove.com/blog/category/unit-testing)

**Historical article** · Originally published February 24, 2007; technical details may be outdated.

_**Update**: You might also be interested in_ [_FxCopUnit_](http://weblogs.asp.net/rosherove/archive/2007/02/24/introducing-fxcopunit-a-framework-for-integrated-fxcop-rule-testing.aspx) _for FxCop Integration testing._

It’s possible to create elegant small tests which require little to no configuration whatsoever that test out your custom rule’s ability to verify problems. (I'll be discussing a framework for FxCop integration testing in a later post)

Here’s a sample unit test:

You'll need to add a reference to three libraries (on my machine they were located under C:\\Program Files\\Microsoft Visual Studio 8\\Team Tools\\Static Analysis Tools\\FxCop\\ )

- FxCopCommon.dll
- FxCopSDK.dll
- Microsoft.Cci.dll

\[Test\]

public void InvokeMyRuleAgainstTheCurrentMethod()

{

MethodInfo someMethodInfo = MethodBase.GetCurrentMethod() as MethodInfo;

Microsoft.Cci.Method **methodData** = **Microsoft.Cci.Method.GetMethod(someMethodInfo);**

MyRule ruleUnderTest = new MyRule();

ProblemCollection problems = **ruleUnderTest.Check(methodData);**

Assert.Greater(0,problems.Count);

}

A custom rule can accept many kinds of inputs, but ultimately the most important ones are “**Member**” and “**TypeNode**”.

Here’s the hiearchy for Member these

(thanks to Resharper  - **Small tip**: put the caret on a type name and press Ctrl-Alt-H):

![](https://osherove.com/img/wb/1f822714_files.osherove.com_images_member.jpg)

So the only question remains: how to you transform a simple reflection **MethodInfo** or **EventInfo** or **FieldInfo** or **Type** into the corresponding **Member** type in Microsoft.Cci?

It’s pretty simple: For each "thing" you'd like to transform There’s a static method on the counter "thing" to get that transformation. In the test above to get a **Method** object, I used the static **Method.GetMethod(MethodInfo)** to get what I need. You’ll find the same thing for **Event**, **TypeNode** and many others.

All you need is to create a new instance of your custom rule (without needing FxCop engine at all), and invoking it’s **Check** method with the data you have specified after transformation.

The Asserts should take place against the ProblemCollection object that you need to get back.

Voila!

You have yourself a true unit test with FxCop custom rules (or as close as possible to one..).

## More in Agile

[All posts in Agile](https://osherove.com/blog/category/agile)

- Feb 25, 2007

    ### [FxCop is heading in the wrong direction - No testability whatsoever](https://osherove.com/blog/2007/2/25/fxcop-is-heading-in-the-wrong-direction-no-testability-whats.html)

- Feb 24, 2007

    ### [Introducing FxCopUnit - A framework for integrated FxCop rule testing](https://osherove.com/blog/2007/2/24/introducing-fxcopunit-a-framework-for-integrated-fxcop-rule.html)

- Feb 11, 2007

    ### [Naresh Jain - Great Agile Blog](https://osherove.com/blog/2007/2/11/naresh-jain-great-agile-blog.html)

- Feb 3, 2007

    ### [Nice Israeli Blog from the Agile\\Java World](https://osherove.com/blog/2007/2/3/nice-israeli-blog-from-the-agilejava-world.html)


---

## 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/
