Friday, 16 August 2013

NUnit using data source

I am using NUnit to run unit test.
I have been using  test cases for a while but I needed to use multiple tests with date time.
I did not wanted to write a parsing string into date time.
My code generates html as you can see from code below.


My solution after while of reading documentation:








 [Test, TestCaseSource(typeof(NewsHtmlHelperTests), "DisplayRelatedUpdateLinkTestSources")]
        public void DisplayRelatedUpdateLinkTests(string sourcePath, string articlePath, string name, DateTime createdDate, MvcHtmlString expected)
        {
            var actual = NewsHelper.DisplayRelatedUpdateLink(null, sourcePath, articlePath, name, createdDate);
            Assert.AreEqual(expected.ToString(),actual.ToString());
        }

        public static IEnumerable DisplayRelatedUpdateLinkTestSources
        {
            get
            {
                yield return new TestCaseData("/test", "/test", "Test Article", new DateTime(2012, 05, 28), 
                                            new MvcHtmlString("
  • 28 May Test Article
  • ")); yield return new TestCaseData("/test", "/test/Different", "Test Article Name", new DateTime(2012, 10, 21), new MvcHtmlString("
  • 21 Oct Test Article Name
  • ")); yield return new TestCaseData("/testing", "/test", "Test Article", new DateTime(2012, 09, 18), new MvcHtmlString("
  • 18 Sep Test Article
  • ")); yield return new TestCaseData("/News/Article1", "/News/Article1", "Test Article", new DateTime(2012, 06, 05), new MvcHtmlString("
  • 5 Jun Test Article
  • ")); yield return new TestCaseData("/", "/Hello", "Test Article", new DateTime(2013, 08, 25), new MvcHtmlString("
  • 25 Aug Test Article
  • ")); yield return new TestCaseData("/Snipe", "/Snipe", "Sniper in London", new DateTime(2011, 05, 15), new MvcHtmlString("
  • 15 May Sniper in London
  • ")); } }