<?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>PGS Software &#187; nunit</title>
	<atom:link href="http://www.pgs-soft.com/tag/nunit/feed" rel="self" type="application/rss+xml" />
	<link>http://www.pgs-soft.com</link>
	<description>Nearshore Outsourcing IT Company - Offshore Development</description>
	<lastBuildDate>Fri, 30 Jul 2010 09:54:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>New features in NUnit 2.5. Part 2 &#8211; testing exceptions</title>
		<link>http://www.pgs-soft.com/new-features-in-nunit-2-5-part-2-testing-exceptions.html</link>
		<comments>http://www.pgs-soft.com/new-features-in-nunit-2-5-part-2-testing-exceptions.html#comments</comments>
		<pubDate>Thu, 25 Jun 2009 11:33:08 +0000</pubDate>
		<dc:creator>Waldemar Mękal</dc:creator>
				<category><![CDATA[Developers]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[nunit]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.pgs-soft.com/?p=2581</guid>
		<description><![CDATA[In part 1 I have presented how to use parameterized test methods in NUnit 2.5. In this post I want to show a new way of testing exceptions by NUnit. Previously, we could use ExpectedException attribute. It looked like the one below. [Test] [ExpectedException(typeof(ArgumentNullException))] public void ExceptionTest() { ... someObject.MethodThatThrowsException(null); } That approach had several [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px; margin-top: 5px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.pgs-soft.com%2Fnew-features-in-nunit-2-5-part-2-testing-exceptions.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.pgs-soft.com%2Fnew-features-in-nunit-2-5-part-2-testing-exceptions.html&amp;source=pgssoftware&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>In <a href="http://www.pgs-soft.com/new-features-in-nunit-2-5-part-1-parameterized-tests.html">part 1</a> I have presented how to use parameterized test methods in NUnit 2.5. In this post I want to show a new way of testing exceptions by NUnit.</p>
<p>Previously, we could use ExpectedException attribute. It looked like the one below.</p>
<pre>[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void ExceptionTest()
{
    ...

    someObject.MethodThatThrowsException(null);
}</pre>
<p>That approach had several drawbacks &#8211; only one exception could be tested in a single method,  there was no way to test exception message or any exception property. The new way is to use Assert.Throws method. It takes exception type and TestDelegate, in which code that throws exception should be placed, as arguments. Below, two overloads are presented &#8211; the first one with type argument, the second &#8211; with typeof operator.</p>
<pre>[Test]
public void ExceptionTest()
{
    ...

    Assert.Throws&lt;ArgumentNullException&gt;(
        () =&gt; someObject.MethodThatThrowsException(null));

    Assert.Throws( typeof(ArgumentNullException),
        delegate { someObject.MethodThatThrowsException(null) } );
}</pre>
<p>Assert.Throws returns an exception that is expected. It gives us an opportunity to check some properties of thrown exception.</p>
<pre>[Test]
public void ExceptionTest()
{
    ...

    ArgumentException ex = Assert.Throws&lt;ArgumentException&gt;(
        () =&gt; someObject.MethodThatThrowsException(""));
    Assert.That(ex.Message, Is.EqualTo("Arg not given!"));
}</pre>
<p>There is also an overload of Assert.Throws which uses constraints.</p>
<pre>[Test]
public void TestException()
{
    Assert.Throws( Is.TypeOf&lt;ArgumentException&gt;(),
        () =&gt; someObject.MethodThatThrowsException(null) );
}</pre>
<p>That&#8217;s all. You can find more information in <a title="nunit documentation" href="http://www.nunit.org/index.php?p=exceptionAsserts&amp;r=2.5" target="_self">nunit documentation</a>.</p>
<hr /><small>Copyright &copy; 2004 - 2010 by <a href="http://www.pgs-soft.com">PGS Software</a>
<br/><br/>
<a href="http://www.pgs-soft.com/new-features-in-nunit-2-5-part-2-testing-exceptions.html#comments">Comments (0)</a> - originally posted here <a href="http://www.pgs-soft.com/new-features-in-nunit-2-5-part-2-testing-exceptions.html">http://www.pgs-soft.com/new-features-in-nunit-2-5-part-2-testing-exceptions.html</a> </small>

<h2>Related posts</h2><ol><li><a href='http://www.pgs-soft.com/new-features-in-nunit-2-5-part-1-parameterized-tests.html' rel='bookmark' title='Permanent Link: New features in NUnit 2.5. Part 1 &#8211; parameterized tests'>New features in NUnit 2.5. Part 1 &#8211; parameterized tests</a></li>
<li><a href='http://www.pgs-soft.com/quick-introduction-to-postsharp.html' rel='bookmark' title='Permanent Link: Quick introduction to PostSharp'>Quick introduction to PostSharp</a></li>
<li><a href='http://www.pgs-soft.com/pex-first-impressions.html' rel='bookmark' title='Permanent Link: Pex &#8211; first impressions'>Pex &#8211; first impressions</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://www.pgs-soft.com/new-features-in-nunit-2-5-part-2-testing-exceptions.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New features in NUnit 2.5. Part 1 &#8211; parameterized tests</title>
		<link>http://www.pgs-soft.com/new-features-in-nunit-2-5-part-1-parameterized-tests.html</link>
		<comments>http://www.pgs-soft.com/new-features-in-nunit-2-5-part-1-parameterized-tests.html#comments</comments>
		<pubDate>Wed, 10 Jun 2009 11:20:27 +0000</pubDate>
		<dc:creator>Waldemar Mękal</dc:creator>
				<category><![CDATA[Developers]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[nunit]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.pgs-soft.com/?p=2559</guid>
		<description><![CDATA[At the beginning of May 2009, NUnit 2.5 was released. It was a big release and brought a lot of new functionalities. The main changes were the following (full changelist): added support for parameterized tests, added support for theories, changed way of testing expected exceptions &#8211; Assert.Throws instead of ExpectedExceptionAttribute, added ability to specify the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px; margin-top: 5px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.pgs-soft.com%2Fnew-features-in-nunit-2-5-part-1-parameterized-tests.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.pgs-soft.com%2Fnew-features-in-nunit-2-5-part-1-parameterized-tests.html&amp;source=pgssoftware&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>At the beginning of May 2009, NUnit 2.5 was released. It was a big release and brought a lot of new functionalities. The main changes were the following (<a href="http://www.nunit.org/index.php?p=releaseNotes&amp;r=2.5">full changelist</a>):</p>
<ul>
<li> added support for parameterized tests,</li>
<li> added support for theories,</li>
<li> changed way of testing expected exceptions &#8211; Assert.Throws instead of ExpectedExceptionAttribute,</li>
<li>added ability to specify the thread and apartment state requirements of a test,</li>
<li>added support for generics &#8211; TestFixture classes and test methods may now be generic,</li>
<li> added support for lambda expressions,</li>
<li> added support for executing test in a separate process for better isolation,</li>
<li> added ability to display source code in the gui runner.</li>
</ul>
<p>I am going to describe some of these new features in a couple of posts. In this, I will focus on parameterized tests.</p>
<h3>Parameterized Tests</h3>
<p>Before NUnit 2.5 test methods always looked like this:</p>
<pre>[Test]
public void TestMethod()
{
  ....
}</pre>
<p>If you needed to run the same test with different parameters you would write something like this:</p>
<pre>[Test]
public void TestMethod()
{
  ParameterizedTestMethod(1, 1, 2);
  ...
  ParameterizedTestMethod(1, 3, 8);
}

private void ParameterizedTestMethod(int arg1, int arg2,
  int result)
{
  //some asserts
}</pre>
<p>Now, you can forget about it. NUnit 2.5 introduced a number of new attributes to help you to write parameterized tests. Some of those attributes are:</p>
<ul>
<li> TestCaseAttribute, TestCaseSourceAttribute &#8211; for test method,</li>
<li> RandomAttribute, RangeAttribute, ValuesAttribute, ValueSourceAttribute &#8211; for arguments in parameterized test method.</li>
</ul>
<h4>TestCaseAttribute</h4>
<p>This attribute allows you to specify values of arguments for given the test method. You may also set TestName, Description or ExpectedException, if you expect exception for given values.</p>
<pre>[TestCase("test1")]
[TestCase("test2", TestName = "This is a second test.")]
[TestCase("")]
[TestCase(null,
  ExpectedException = typeof(ArgumentNullException))]
public void TestCaseAttributeTest(string arg)
{
  ...
}</pre>
<h4>TestCaseSourceAttribute</h4>
<p>TestCaseSourceAttribute is used to specify the property, method or field that will provide required values of arguments for parameterized method. You must provide its name to identify the source. The source specified by the attribute must return IEnumerable or type that implements IEnumerable.</p>
<pre>[Test, TestCaseSource("TestCases")]
public void TestCaseSourceTest(int arg1, int arg2)
{
  ...
}

static object[] TestCases =
{
  new object[] { 1, 2 },
  new object[] { 1, 3 },
  new object[] { 2, 4 }
};</pre>
<p>You can also choose property or method from other class than one that contains your test cases. To do so, you must specify the source type.</p>
<pre>[TestFixture]
public class TestCaseSourceTests
{
  [Test,TestCaseSource(typeof(SourceClass),"TestCases")]
  public void TestCaseSourceTest(int arg1, int arg2)
  {
    ...
  }
  ...
}</pre>
<pre>public class SourceClass
{
  public static object[] TestCases =
  {
    new object[] { 1, 2 },
    new object[] { 1, 3 },
    new object[] { 2, 4 }
  }
}</pre>
<h4>ValuesAttribute</h4>
<p>It is used for method parameter to specify set of values that will be given for it. It must be applied on all parameters. NUnit then creates test cases from all possible combinations of the given value sets.</p>
<pre>[Test]
public void ValuesAttributeTest([Values(1,2,3)] int arg1,
  [Values(-1,-2,-3)] int arg2)
{
  ...
}</pre>
<p>It will be executed nine times:</p>
<pre>ValuesAttributeTest(1, -1)
ValuesAttributeTest(1, -2)
ValuesAttributeTest(1, -3)
ValuesAttributeTest(2, -1)
ValuesAttributeTest(2, -2)
ValuesAttributeTest(2, -3)
ValuesAttributeTest(3, -1)
ValuesAttributeTest(3, -2)
ValuesAttributeTest(3, -3)</pre>
<h4>ValueSourceAttribute</h4>
<p>It works in the same way as TestCaseSourceAttribute, but is used on method parameter.</p>
<h4>RangeAttribute</h4>
<p>It is used for specifying the range of values to be provided for the given parameter. You can specify minimum, maximum and step. Then, NUnit creates test cases of all possible combinations of given ranges.</p>
<pre>[Test]
public void RangeAttributeTest([Values(1,2,3)] int arg1,
  [Range(0.5,1.5,0.5)] double arg2)
{
  ...
}</pre>
<p>It will be executed nine times:</p>
<pre>RangeAttributeTest(1, 0.5)
RangeAttributeTest(1, 1.0)
RangeAttributeTest(1, 1.5)
RangeAttributeTest(2, 0.5)
RangeAttributeTest(2, 1.0)
RangeAttributeTest(2, 1.5)
RangeAttributeTest(3, 0.5)
RangeAttributeTest(3, 1.0)
RangeAttributeTest(3, 1.5)</pre>
<h4>RandomAttribute</h4>
<p>This attribute is used for specifying a set of random values to be provided for the given parameter. You can specify the range and count how many values will be produced for a parameter.</p>
<pre>[Test]
public void RandomAttributeTest([Values(1,2,3)] int arg1,
  [Random(-1.0,1.0,5)] double arg2)
{
  ...
}</pre>
<p>It will be executed 15 times. 3 times for each value of arg1, each combined with 5 random values of arg2.</p>
<h3>Summary</h3>
<p>Testing may be a repetitive task. There are many situations in which you have to test the same behavior with different input values.  Previously, you had to handle it by yourself. Now, using NUnit 2.5, you can use parameterized test methods and appropriate attributes in a simple declarative way.</p>
<p>Examples of attribute usage were brought from NUnit documentation.</p>
<p>Sources:</p>
<p><a href="http://www.nunit.org/">http://www.nunit.org/</a><br />
<a href="http://blog.benhall.me.uk/2008/08/taking-look-at-nunit-25-alpha-3.html">http://blog.benhall.me.uk/2008/08/taking-look-at-nunit-25-alpha-3.html</a></p>
<div id="_mcePaste" style="overflow:hidden;position:absolute;left:-10000px;top:513px;width:1px;height:1px;">
<pre>ParameterizedTestMethod</pre>
</div>
<hr /><small>Copyright &copy; 2004 - 2010 by <a href="http://www.pgs-soft.com">PGS Software</a>
<br/><br/>
<a href="http://www.pgs-soft.com/new-features-in-nunit-2-5-part-1-parameterized-tests.html#comments">Comments (0)</a> - originally posted here <a href="http://www.pgs-soft.com/new-features-in-nunit-2-5-part-1-parameterized-tests.html">http://www.pgs-soft.com/new-features-in-nunit-2-5-part-1-parameterized-tests.html</a> </small>

<h2>Related posts</h2><ol><li><a href='http://www.pgs-soft.com/new-features-in-nunit-2-5-part-2-testing-exceptions.html' rel='bookmark' title='Permanent Link: New features in NUnit 2.5. Part 2 &#8211; testing exceptions'>New features in NUnit 2.5. Part 2 &#8211; testing exceptions</a></li>
<li><a href='http://www.pgs-soft.com/quick-introduction-to-postsharp.html' rel='bookmark' title='Permanent Link: Quick introduction to PostSharp'>Quick introduction to PostSharp</a></li>
<li><a href='http://www.pgs-soft.com/pex-first-impressions.html' rel='bookmark' title='Permanent Link: Pex &#8211; first impressions'>Pex &#8211; first impressions</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://www.pgs-soft.com/new-features-in-nunit-2-5-part-1-parameterized-tests.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
