<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Milan Nankov’s Blog &#187; Testing</title>
	<atom:link href="http://blog.nankov.com/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.nankov.com</link>
	<description>WPF, XNA, MSBuild, and other technologies blog</description>
	<lastBuildDate>Fri, 11 Jun 2010 05:18:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Visual Studio Code Snippets for TestClass and TestMethod</title>
		<link>http://blog.nankov.com/2010/03/25/visual-studio-code-snippets-for-testclass-and-testmethod/</link>
		<comments>http://blog.nankov.com/2010/03/25/visual-studio-code-snippets-for-testclass-and-testmethod/#comments</comments>
		<pubDate>Thu, 25 Mar 2010 21:40:35 +0000</pubDate>
		<dc:creator>Milan</dc:creator>
				<category><![CDATA[Software Testing]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://blog.nankov.com/2010/03/25/visual-studio-code-snippets-for-testclass-and-testmethod/</guid>
		<description><![CDATA[Tired of writing [TestMethod] and [TestClass] when creating tests? Well, I got tired and wrote two very simple Visual Studio snippets that can create test classes and test methods.
Just copy the two .snippet files into C:\&#60;Program Files Folder&#62;\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C# , restart Visual Studio and you are good to go.
Enjoy.
Download snippets
Print Friendly]]></description>
			<content:encoded><![CDATA[<p>Tired of writing <strong>[TestMethod]</strong> and <strong>[TestClass]</strong> when creating tests? Well, I got tired and wrote two very simple Visual Studio snippets that can create test classes and test methods.</p>
<p>Just copy the two .snippet files into C:\&lt;Program Files Folder&gt;\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C# , restart Visual Studio and you are good to go.</p>
<p>Enjoy.</p>
<p><a href="http://blog.nankov.com/wp-content/uploads/2010/03/testsnippets.zip" target="_blank">Download snippets</a></p>
<div id="pfButton"><a href="http://blog.nankov.com/2010/03/25/visual-studio-code-snippets-for-testclass-and-testmethod/?pfstyle=wp" title="Print an optimized version of this web page" style="text-decoration: none;"><img id="printfriendly" style="border:none; padding:0;" src="http://cdn.printfriendly.com/pf-icon-small.gif" alt="Print"/><span style="font-size: 12px; color: rgb(85, 117, 12);">Print Friendly</span></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.nankov.com/2010/03/25/visual-studio-code-snippets-for-testclass-and-testmethod/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mocking The File System to Improve Testability</title>
		<link>http://blog.nankov.com/2009/12/10/mocking-the-file-system-to-improve-testability/</link>
		<comments>http://blog.nankov.com/2009/12/10/mocking-the-file-system-to-improve-testability/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 08:05:17 +0000</pubDate>
		<dc:creator>Milan</dc:creator>
				<category><![CDATA[Software Testing]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[file system]]></category>
		<category><![CDATA[mocking]]></category>
		<category><![CDATA[Moq]]></category>
		<category><![CDATA[MsTest]]></category>
		<category><![CDATA[testability]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog.nankov.com/?p=222</guid>
		<description><![CDATA[Recently I started writing a simple application that would help me organize my music files by automatically renaming them using their tags and moving them to their designated folders. 
As expected the program will rely on the file system classes that the .Net Framework provides. Naturally, I wanted to cover the I/O logic with tests [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I started writing a simple application that would help me organize my music files by automatically renaming them using their tags and moving them to their designated folders. </p>
<p>As expected the program will rely on the file system classes that the .Net Framework provides. Naturally, I wanted to cover the I/O logic with tests so that I am confident that when the software is used it will not do damage to my files and will behave according to my requirements. So I started writing tests…</p>
<p><span id="more-222"></span></p>
<p>And here is the catch – in general it is not a good idea to have tests that are performing I/O operations like accessing files and databases. In such situations, but of course not limited to, mock objects are your friend. And before I go into more details about what mocking really is lets find out why mocking I/O is important? Well, for a ton of reasons:</p>
<ul>
<li>Usually faster than performing I/O operations </li>
<li>You do not have have to deal with security issues </li>
<li>You have more control – easily test intricate scenarios </li>
<li>Test setup is far more easier to do&#160; </li>
</ul>
<p>The first thing that most people think about when asked about mocking is interfaces. And that is no surprise since mocking is built around interfaces. Here is a simplified version of the file system interface that I have created for my application:</p>
</p>
<pre class="c#" name="code">public interface IFileSystem
{
    bool FileExists(string path);

    string GetFullPath(string path);

    string GetFileName(string filePath);

    string GetPathRoot(string path);

    string GetDirectoryName(string path);

    string PathCombine(string firstPath, string secondPath);

    void MoveFile(string filePath, string newFilePath);
}</pre>
<p>This interface represents all operations that my application will be able to perform on the file system. As we will see, this interface allows me to create a fake implementation of the file system functionality (mock) which will be the cornerstone of all of my I/O tests.</p>
<p>As you have probably guessed I will be using MSTest to drive my tests but you can use a testing framework of your choice. In addition to a testing framework you also need a mocking framework. My favorite one is called <a href="http://code.google.com/p/moq/" target="_blank">Moq</a> &#8211; it is very powerful and yet easy to use. </p>
<p>Let’s write some tests…</p>
<pre class="c#" name="code">private Mock&lt;IFileSystem&gt; fileSystemMock;
private IFileSystem fileSystem;

[TestInitialize]
public void Initializer()
{
    this.fileSystemMock = new Mock&lt;IFileSystem&gt;();
    this.fileSystem = this.fileSystemMock.Object;
}</pre>
<p>The first thing to do is to create the mock implementation of the interface that we are interested in. This first line of the Initializer&#160; method will create a dummy implementation (mock) of the specified interface (in this case IFileSystem) and you can use the Object property to interact with an instance of the this dummy interface implementation. For convenience we copy the instance reference to “<em>this.fileSystem</em>”.</p>
<p>The mock (“<em>this.fileSystemMock</em>”) allows you to control the behavior of the mocked object and setup assertions which allow you to test the behavior of the mocked object. </p>
<p>The following two tests will assure that the Rename method of the MusicFileConfigurator class behaves according to my specifications. Here is the code that will be tested:</p>
<pre class="c#" name="code">public class MusicFileConfigurator
{
    private IFileSystem fileSystem;

    public MusicFileConfigurator(IFileSystem fileSystem)
    {
        if (fileSystem == null)
            throw new ArgumentNullException(&quot;fileSystem&quot;);

        this.fileSystem = fileSystem;
    }

    public void Rename(string filePath)
    {
        if(!this.fileSystem.FileExists(filePath))
            throw new FileNotFoundException(&quot;File was not found&quot;, filePath);

        var newFilePath = this.AdjustNewFilePath(filePath, &quot;renamed.test.file&quot;);

        if (this.fileSystem.FileExists(newFilePath))
            throw new IOException(&quot;New filename already exists -&gt; &quot; + newFilePath);

        this.fileSystem.MoveFile(filePath, newFilePath);
    }
}</pre>
<p>Notice that the constructor accepts a parameter of type IFileSystem. This is very important, as you will see shortly, since it enables us to plug in different interface implementations. This technique is know as Dependency Injection – in this particular case we use the so called Constructor injection. For the sake of simplicity the Rename method will always rename the specified file to “renamed.test.file”.</p>
<p>Back to the tests… First I want to be sure that if a non-existent file is passed as argument the method will throw an exception. </p>
<pre class="c#" name="code">[TestMethod]
[ExpectedException(typeof(FileNotFoundException))]
public void Rename_ThrowsException_WhenTheSpecifiedFileDoesNotExist()
{
    MusicFileConfigurator creator = new MusicFileConfigurator(this.fileSystem);
    this.fileSystemMock.Setup(fs =&gt; fs.FileExists(It.Is<string>(s =&gt; s == &quot;test.file&quot;))).Returns(false);

    creator.Rename(&quot;test.file&quot;);
}</pre>
<p>The first line, though boring, is crucial for understanding how the test works. Remember that “<em>this.fileSystem</em>” is actually an instance of the mock implementation? Well, now the MusicFileConfigurator is going to use this mock implementation of the IFileSystem.</p>
<p>The second line of the test reveals some of the power of Moq and mocking in general. This line instructs the file system mock to return “false” whenever “test.file” is specified as argument to the FileExists method. By doing that we ensure that the first condition of Rename will not be met and FileNotFoundException will be thrown. </p>
<p>The Setup method is very powerful and allows you to specify how the methods and the properties of your mocked objects behave – what results they returns, whether they throw exceptions and more. </p>
<p>Moving on to a move sophisticated test which asserts that the Rename method can successfully rename the specified file.</p>
<pre class="c#" name="code">[TestMethod]
public void File_IsRenamed_Correctly()
{
    MusicFileConfigurator creator = new MusicFileConfigurator(this.fileSystem);
    this.fileSystemMock.Setup(fs =&gt; fs.FileExists(It.Is<string>(s =&gt; s == &quot;test.file&quot;))).Returns(true);
    this.fileSystemMock.Setup(fs =&gt; fs.FileExists(It.Is<string>(s =&gt; s == &quot;renamed.test.file&quot;))).Returns(false);

    creator.Rename(&quot;test.file&quot;);

    this.fileSystemMock.Verify(fs =&gt; fs.MoveFile(&quot;test.file&quot;, &quot;renamed.test.file&quot;), Times.Exactly(1));
}</pre>
<p>Once again we have almost the same setup with just two additional lines. This time we want to test that when the Rename method executes without any errors it will actually rename the specified file. The two Setup statements ensure that the specified file exists and that the new filename does not (which is required by the Rename method).</p>
<p>So far we have just ensured that the Rename method will run without errors. To be sure that it can really rename files we have to make sure that Rename will call MoveFile with the correct arguments. With Moq this is a piece of cake.</p>
<p>Whenever we want to make sure that certain method has been executed or hasn’t been executed we use the Verify method. In this particular setup I want to assert that the Rename method calls the MoveFile method of the file system exactly one time with the expected arguments. If the conditions are not met – for instance the method is called more times &#8211; Verify will throw exception and the test will fail.&#160; </p>
<p>I do not know if it is just me but I think that this piece of test code is simply beautiful. In addition to the easily readable fluent interface of Moq you are not dealing with the actual file system which spares you a a great deal of time and resources to setup our tests. </p>
<p>You might be wondering how is the MusicFileConfigurator going to working in a real scenario? Well, I have created a special implementation of the IFileSystem interface which uses the actual file system. </p>
<pre class="c#" name="code">public sealed class DefaultFileSystem : IFileSystem
{
    public string GetFullPath(string path)
    {
        return Path.GetFullPath(path);
    }

    public string PathCombine(string firstPath, string secondPath)
    {
        return Path.Combine(firstPath, secondPath);
    }

    public string GetFileName(string filePath)
    {
        return Path.GetFileName(filePath);
    }

    public void MoveFile(string filePath, string newFilePath)
    {
        File.Move(filePath, newFilePath);
    }

    public bool FileExists(string path)
    {
        return File.Exists(path);
    }

    public string GetDirectoryName(string path)
    {
        return Path.GetDirectoryName(path);
    }

    public string GetPathRoot(string path)
    {
        return Path.GetPathRoot(path);
    }
}</pre>
<p>And whenever I initialize the MusicFileConfigurator I do something like that:</p>
<pre class="c#" name="code">class Program
{
    static void Main(string[] args)
    {
        MusicFileConfigurator configurator = new MusicFileConfigurator(new DefaultFileSystem());
    }
}</pre>
<p>I have attached the full source code for this article which includes more tests. </p>
<p>Related links:<br />
  <a href="http://code.google.com/p/moq/" target="_blank">The Moq Project</a><br />
  <a href="http://www.codethinked.com/post/2009/03/08/Beginning-Mocking-With-Moq-3-Part-1.aspx" target="_blank">Beginning Mocking with Moq</a>
</p>
<p> Download File &#8211; <a href="http://blog.nankov.com/wp-content/uploads/2010/01/FileSystemMockingSource.zip" target="_self">FileSystemMockingSource</a></p>
<div id="pfButton"><a href="http://blog.nankov.com/2009/12/10/mocking-the-file-system-to-improve-testability/?pfstyle=wp" title="Print an optimized version of this web page" style="text-decoration: none;"><img id="printfriendly" style="border:none; padding:0;" src="http://cdn.printfriendly.com/pf-icon-small.gif" alt="Print"/><span style="font-size: 12px; color: rgb(85, 117, 12);">Print Friendly</span></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.nankov.com/2009/12/10/mocking-the-file-system-to-improve-testability/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Books I Recommend &#8211; I. M. Wright&#8217;s Hard Code by Eric Brechner</title>
		<link>http://blog.nankov.com/2009/12/03/books-i-recommend-i-m-wrights-hard-code-by-eric-brechner/</link>
		<comments>http://blog.nankov.com/2009/12/03/books-i-recommend-i-m-wrights-hard-code-by-eric-brechner/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 06:46:36 +0000</pubDate>
		<dc:creator>Milan</dc:creator>
				<category><![CDATA[Software Testing]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Book]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Eric Brechner]]></category>
		<category><![CDATA[Hard Code]]></category>
		<category><![CDATA[Managing risk]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Quality]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog.nankov.com/2009/12/03/books-i-recommend-i-m-wrights-hard-code-by-eric-brechner/</guid>
		<description><![CDATA[&#160;
I have been yearning to write about this book for a long time since it&#160; had become an all-time favorite. You have got to love this book for its incredible depth, witty jokes, great style, and funky structure. Definitely a must-read for every developer. 
Don’t get fooled by the name though – the author covers [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.amazon.com/gp/product/0735624356/" target="_blank"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="I.M. Wright&#39;s Hard Code" border="0" alt="I.M. Wright&#39;s Hard Code" src="http://blog.nankov.com/wp-content/uploads/2009/12/hardcodesmall.jpg" width="254" height="309" /></a>&#160;</p>
<p>I have been yearning to write about this book for a long time since it&#160; had become an all-time favorite. You have got to love this book for its incredible depth, witty jokes, great style, and funky structure. Definitely a must-read for every developer. </p>
<p>Don’t get fooled by the name though – the author covers a lot of ground and the book could be helpful not only to developers but project/product managers, QAs, HR, even top-level management. </p>
<p><span id="more-218"></span></p>
<p>Hard Code is a compilation of opinion columns written by the fictitious figure I. M. Wright (got to love the name), or as Eric Brechner calls it “my alter ego”.&#160; In addition to topics like dev schedules, managing risk, agile methodologies, and testing the author covers other more esoteric, yet just as important, ones like people issues and team growth. Moreover the book can be considered as The Hitchhiker&#8217;s Guide to Microsoft since it offers quite a few details about the inner-workings of the software giant.</p>
<p>I will leave you with three of my favorite quotes from the book:</p>
<p><em>“A horse walks into a bar and says: I can code that feature in two days.” </em></p>
<p><em>“If you think Agile methods fix all that is wrong with how we build products, you are, in fact, a fool… If you are an anti-Agile curmudgeon who thinks Scrum is an acronym for System of Clueless Reckless Untested Methods, you are as much a fool and just as ignorant” </em></p>
<p><em>“If you treat your test team like trash, trash is likely what you’ll ship and trashy is how you’ll feel. If you prefer a smooth release and want to ship a great product, make your test team your ally. … [testers] They cover areas that you can’t or don’t want to do. They keep you on schedule and track. They keep you honest and represent the customer’s perspective.”</em></p>
<p>Now go read it!</p>
<div id="pfButton"><a href="http://blog.nankov.com/2009/12/03/books-i-recommend-i-m-wrights-hard-code-by-eric-brechner/?pfstyle=wp" title="Print an optimized version of this web page" style="text-decoration: none;"><img id="printfriendly" style="border:none; padding:0;" src="http://cdn.printfriendly.com/pf-icon-small.gif" alt="Print"/><span style="font-size: 12px; color: rgb(85, 117, 12);">Print Friendly</span></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.nankov.com/2009/12/03/books-i-recommend-i-m-wrights-hard-code-by-eric-brechner/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
