<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Refactoring: Introduce SetUp Method</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx</link><description>Tests in a TestFixture share common initialization code. Create a method in the TestFixture, move common initialization code to the method, and execute the method prior to each test. Before: [ Test ] public void UponCreationCountIsZero() { BookmarkCollection</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Refactoring: Introduce SetUp Method</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#258126</link><pubDate>Tue, 16 Nov 2004 10:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:258126</guid><dc:creator>JP</dc:creator><description>Wouldn’t this refactoring introduce execution sequenc dependencies? If the first test to run was AddBookmarkCountIsIncremented the second test UponCreationCountIsZero would fail.</description></item><item><title>re: Refactoring: Introduce SetUp Method</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#258140</link><pubDate>Tue, 16 Nov 2004 10:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:258140</guid><dc:creator>James Newkirk</dc:creator><description>The [SetUp] method is called prior to each test being executed. This results in a new collection being created prior to the execution of each test. </description></item><item><title>re: Refactoring: Introduce SetUp Method</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#258157</link><pubDate>Tue, 16 Nov 2004 11:48:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:258157</guid><dc:creator>Ben Lowery</dc:creator><description>Here's a simple one: why make the collection static?</description></item><item><title>re: Refactoring: Introduce SetUp Method</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#258188</link><pubDate>Tue, 16 Nov 2004 13:44:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:258188</guid><dc:creator>Darron</dc:creator><description>I agree with Ben, I wouldn't make the collection static until it was needed to make a test pass. This would seem to follow the &amp;quot;simple design&amp;quot; rule of doing the least you can do to make the test pass.</description></item><item><title>re: Refactoring: Introduce SetUp Method</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#258195</link><pubDate>Tue, 16 Nov 2004 13:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:258195</guid><dc:creator>Kelly Summerlin</dc:creator><description>In this simple case it's not hard to see the &amp;quot;intent&amp;quot; of the setup. But I have seen this taken to the extreme where the setup was doing way too much. In complex setup scenarios it's often hard to find/see the intent of a setup method. What's is it setting up? How is it going about doing it? All questions I find myself asking a lot.&lt;br&gt;&lt;br&gt;I've also seen setup code doing setup stuff for 5 test methods but the other 30 test methods don't need that piece of the setup. In those cases I advocate having a private method that is called first for the 5 methods that must have that functionality. </description></item><item><title>re: Refactoring: Introduce SetUp Method</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#258196</link><pubDate>Tue, 16 Nov 2004 13:57:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:258196</guid><dc:creator>Darrell</dc:creator><description>Yes it does reduce the communication of the method, but no more so than code-interception style AOP.  I think the price to pay is small enough that it doesn't matter, especially if your test fixture is small enough.&lt;br&gt;&lt;br&gt;Also, I'm sure later you'll want to add a certain number of items to the collection for various testing reasons. I usually break that functionality out into a separate private method, which, when well-named, doesn't reduce the communication of the test.</description></item><item><title>re: Refactoring: Introduce SetUp Method</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#258217</link><pubDate>Tue, 16 Nov 2004 14:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:258217</guid><dc:creator>Tony Chow</dc:creator><description>BTW, if you mark a class with TextFixtureAttribute all public methods will be treated as tests, there's no need to mark each individual method with TestAttribute.  I don't know why everybody keeps doing it; it's just not necessary.&lt;br&gt;&lt;br&gt;Also, your example just doesn't illustrate the benefits of SetUpAttribute very convincingly.  It's hardly inelegant to write one line of instantiation code for each test method; indeed it makes the logic of each test harder to decipher.  &lt;br&gt;&lt;br&gt;In most real tests, common code is shared by only a subset of tests in a test class, and it is far most logical to use an old-fashioned private method, which can be referred to by name in the host method.  Therefore I expect the SetUpAttribute to be of limited usefulness, if not a hinderance, to code readability.</description></item><item><title>re: Refactoring: Introduce SetUp Method</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#258226</link><pubDate>Tue, 16 Nov 2004 15:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:258226</guid><dc:creator>Brendan Tompkins</dc:creator><description>I recently wrote hundreds of tests to cover our legacy business tier code. I created a TestBase class, and inherited all my tests from this class. In my base class, I had a virtual [SetUp]Init() that could be overridden if need be in my test fixture.&lt;br&gt;&lt;br&gt;There was a good deal of setup involved for each test, mostly setting up user principal objects to flow through the tiers, and this worked out really well.  This probably obfuscates the intent of the tests to an even greater degree, but I agree with Darrell that it's a small price to pay.&lt;br&gt;&lt;br&gt;About small fixtures, I think this should be a best practice, if it's not already.  One thing I wish I'd done was reduce the number of tests per fixture.  Especially when using TestDriven.NET and &amp;quot;Test w/ Debugger&amp;quot;.  If I had 20 tests in a fixture, test w/ debugger would test them al.  I'd have rather tested fewer if I had to resort to the debugger.&lt;br&gt;&lt;br&gt;All in all it was a great experience, I identified lots of dead and broken code, and now have a good baseline for refactoring.&lt;br&gt;&lt;br&gt;Thanks James!&lt;br&gt;</description></item><item><title>re: Refactoring: Introduce SetUp Method</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#258242</link><pubDate>Tue, 16 Nov 2004 15:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:258242</guid><dc:creator>Brendan Tompkins</dc:creator><description>Tony, &lt;br&gt;&lt;br&gt;I don't think that's entirely true.  If you name your method Test(something) then, yes, it will be loaded into NUnit, regardless of the [Test] attribute.  But to me this is just a feature of NUnit.  Using the attribute seems like it's a much better way to write a test.</description></item><item><title>re: Refactoring: Introduce SetUp Method</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#258244</link><pubDate>Tue, 16 Nov 2004 15:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:258244</guid><dc:creator>Tony Chow</dc:creator><description>Actually, my bad, all my test methods are prefixed with the words &amp;quot;Test&amp;quot;, and therefore would not need the TestAttribute.</description></item><item><title>re: Refactoring: Introduce SetUp Method</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#258267</link><pubDate>Tue, 16 Nov 2004 16:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:258267</guid><dc:creator>James Newkirk</dc:creator><description>I was reading the release notes for the next version of NUnit and I see that it will become a configuration option to recognize methods by name (i.e. &amp;quot;Test&amp;quot;) as opposed to the attribute. </description></item><item><title>re: Refactoring: Introduce SetUp Method</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#259192</link><pubDate>Wed, 17 Nov 2004 22:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:259192</guid><dc:creator>John Watson</dc:creator><description>I did what Brendan mentioned above as well. I took it a step further and have testSetup and testTearDown stored procedures that are invoked from the base UnitTest class' Setup and TearDown methods. testTearDown truncates tables and uses DBCC to reset the identities to 1. testSetup (hard coded right now) does a bunch of inserts to various tables.&lt;br&gt;&lt;br&gt;The net effect is that before each test is run the database is in a known, clean, predicatable state. Each project has a different DB schema and such so each testSetup has to be unique and different.</description></item><item><title>re: Refactoring: Introduce SetUp Method</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#277719</link><pubDate>Tue, 07 Dec 2004 17:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:277719</guid><dc:creator>Robert Angers</dc:creator><description>I think that Refactoring at all level should be applied.  Eliminating dupblicate code is a goal every programmer should tend to, that for Production or Test code.&lt;br&gt;&lt;br&gt;I belive that communication is an issue, but should not stop you from using it.  It should be implicit that [SetUp] should be executed before every test.  Since it not today common knowledge for everyone, does not mean we should not use it.  It will become common knowledge.  From that point on, the communication won't be an issue anymore.  &lt;br&gt;&lt;br&gt;It's like Design Patterns.  Will you not use them because not all programmers know them?  Of course not. The strategy is to use them and transmit the knowledge to your team.  </description></item><item><title> James Newkirk s Blog Refactoring Introduce SetUp Method | Outdoor Ceiling Fans</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#9669967</link><pubDate>Sun, 31 May 2009 18:15:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9669967</guid><dc:creator> James Newkirk s Blog Refactoring Introduce SetUp Method | Outdoor Ceiling Fans</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://outdoorceilingfansite.info/story.php?id=2413"&gt;http://outdoorceilingfansite.info/story.php?id=2413&lt;/a&gt;&lt;/p&gt;
</description></item><item><title> James Newkirk s Blog Refactoring Introduce SetUp Method | Outdoor Ceiling Fans</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#9670812</link><pubDate>Sun, 31 May 2009 21:20:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9670812</guid><dc:creator> James Newkirk s Blog Refactoring Introduce SetUp Method | Outdoor Ceiling Fans</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://outdoorceilingfansite.info/story.php?id=20049"&gt;http://outdoorceilingfansite.info/story.php?id=20049&lt;/a&gt;&lt;/p&gt;
</description></item><item><title> James Newkirk s Blog Refactoring Introduce SetUp Method | home lighting</title><link>http://blogs.msdn.com/jamesnewkirk/archive/2004/11/16/258100.aspx#9779581</link><pubDate>Fri, 19 Jun 2009 08:30:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9779581</guid><dc:creator> James Newkirk s Blog Refactoring Introduce SetUp Method | home lighting</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://homelightingconcept.info/story.php?id=1589"&gt;http://homelightingconcept.info/story.php?id=1589&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>