Created
September 12, 2012 19:57
-
-
Save akimboyko/3709463 to your computer and use it in GitHub Desktop.
NUnit Exception Testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
// nunit runner | |
NUnit.ConsoleRunner.Runner.Main(new string[] | |
{ | |
Assembly.GetExecutingAssembly().Location, | |
}); | |
} | |
public interface IBookMarket | |
{ | |
} | |
public class BooksInfo | |
{ | |
} | |
public class BookInfoProvider | |
{ | |
public BooksInfo GetBookInfo(IBookMarket bookinfo) | |
{ | |
if(bookinfo == null) | |
throw new ArgumentNullException("bookinfo should not be null"); | |
return null; | |
} | |
} | |
[TestFixture] | |
public class TestGetBookInfo | |
{ | |
[Test] | |
[ExpectedException( | |
ExpectedException = typeof(ArgumentNullException), | |
ExpectedMessage = "bookinfo should not be null", | |
MatchType = MessageMatch.Contains)] | |
public void TestGetBookInfoExceptionOldStyle() | |
{ | |
new BookInfoProvider().GetBookInfo(null); | |
} | |
[Test] | |
public void TestGetBookInfoExceptionNewStyle() | |
{ | |
Assert.That( | |
() => new BookInfoProvider().GetBookInfo(null), | |
Throws.InstanceOf<ArgumentNullException>() | |
.And.Message.Contains("bookinfo should not be null")); | |
} | |
object[] TestData = | |
{ | |
new TestCaseData(new BookMarketStub()), // "good" case | |
new TestCaseData(null).Throws(typeof(ArgumentNullException)) // "bad" exceptional case | |
}; | |
[Test] | |
[TestCaseSource("TestData")] | |
public void TestGetBookInfoDataDriven(IBookMarket bookinfo) | |
{ | |
new BookInfoProvider().GetBookInfo(bookinfo); | |
Assert.Pass("all ok"); // this is not necessary | |
} | |
} | |
public class BookMarketStub : IBookMarket | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment