---
title: "Code-Reuse in unit tests is more important than you think"
url: "https://osherove.com/blog/2007/9/22/code-reuse-in-unit-tests-is-more-important-than-you-think.md"
canonical: "https://osherove.com/blog/2007/9/22/code-reuse-in-unit-tests-is-more-important-than-you-think.html"
published: "2007-09-22T04:51:06"
updated: "2007-09-22T04:51:06"
tags: ["Agile", "Agile Related", "Art Of Unit Testing", "Testing Guidelines", "Unit Testing"]
author: "Roy Osherove"
description: "Scott Bellware mentions how he really likes the new ideas in XUnit.NET , especially the lack of [Setup] and [TearDown] methods. (Brad Wilson wrote in the comments to my post about XUnit.NET that you can mimic this…"
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/"
---

# Code-Reuse in unit tests is more important than you think

By Roy Osherove September 22, 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), [Testing Guidelines](https://osherove.com/blog/category/testing-guidelines), [Unit Testing](https://osherove.com/blog/category/unit-testing)

**Historical article** · Originally published September 22, 2007; technical details may be outdated.

Scott Bellware mentions how [he really likes the new ideas in XUnit.NET](http://codebetter.com/blogs/scott.bellware/archive/2007/09/21/168390.aspx), especially the lack of \[Setup\] and \[TearDown\] methods. (Brad Wilson wrote in the comments to my [post about XUnit.NET](http://weblogs.asp.net/rosherove/archive/2007/09/21/xunit-net-aims-to-be-the-next-nunit-but-it-s-too-soon.aspx) that you can mimic this behavior by using the constructor of the test class and the IDisposable.Dispose() method of the class -just implement IDisposable) .

He especially says this:

> "I think that Jim is on the right track, but I'm the kind of guy that feels that a test class's greatest responsibility is to document behavior in the clearest possible way, even if that means sacrificing **design qualities best reserved for functional code - like reuse**."

(emphasis mine)

That's silly. Scott tries to write "pure" TDD tests by saying that tests are 100% different than functional code, but they are not. Proof -- Tests that don't have code re-use in the form of setup and teardown or various specialized factory methods will be harder to maintain because every time you change some semantic aspect of your production code (constructors, initialization phases etc..) many of your tests will break and you'll have to fix them all one by one because you don't have re-use for object creation in your tests. Pretty soon you'll just stop maintaining your tests and that means you'll stop writing new ones. Ta-da! mission accomplished.

If we don't all realize that in the **real world** tests have to be as maintainable (or even more so) as production code, we won't come out of the middle ages of unit testing.

he then goes on to say some rules he has about Setup methods. one of which is:

> "Don't put anything in a setup method that doesn't pertain to every test method in the test class"

I completely agree with this. You NEED to have re-use but you also need to make sure you don't abuse these abilities.

## More in Agile

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

- Sep 26, 2007

    ### [The Art Of Unit Testing - Available to Purchase Online Now](https://osherove.com/blog/2007/9/26/the-art-of-unit-testing-available-to-purchase-online-now.html)

- Sep 24, 2007

    ### [Slides and Demos for My Talk: Testing Data Access Code Pragmatically](https://osherove.com/blog/2007/9/24/slides-and-demos-for-my-talk-testing-data-access-code-pragma.html)

- Sep 21, 2007

    ### [XUnit.NET aims to be the Next NUnit, but it's too soon](https://osherove.com/blog/2007/9/21/xunitnet-aims-to-be-the-next-nunit-but-its-too-soon.html)

- Sep 18, 2007

    ### [The Art Of Unit Testing - Help me choose a book cover](https://osherove.com/blog/2007/9/18/the-art-of-unit-testing-help-me-choose-a-book-cover.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/
