
{"id":2084,"date":"2016-08-25T11:15:31","date_gmt":"2016-08-25T15:15:31","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=2084"},"modified":"2016-08-25T11:15:31","modified_gmt":"2016-08-25T15:15:31","slug":"nunit-test-cases-with-dates","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=2084","title":{"rendered":"nUnit test cases with dates"},"content":{"rendered":"<p>nUnit has a great feature of running multiple similar test cases via data-driven approach:<\/p>\n<p><code><\/p>\n<pre>[TestCase(\"\", \"\")]\n[TestCase(\"q\", \"q\")]\n[TestCase(\"xyz\", \"zyx\")]\npublic void TestStringReverse(string s, string expectedResult)\n{\n    var result = Reverse(s);\n    Assert.AreEqual(expectedResult, result);\n}<\/pre>\n<p><\/code><\/p>\n<p>However, that does not work with dates, since <code>DateTime<\/code> is not a primitive type and cannot be used in an attribute.<\/p>\n<p><code><\/p>\n<pre>\n[TestCase(new DateTime(...), new DateTime(...), false)] \/\/ <font color=\"red\">Does not work<\/font>\npublic void TestIntervalIsGood(DateTime from, DateTime to, bool isGood)\n{\n    ...\n}<\/pre>\n<p><\/code><\/p>\n<p>The solution is to supply test data in runtime, using <code>[ValueSource]<\/code> attribute. This is more code, but it works.<\/p>\n<p><code><\/p>\n<pre>        \/\/ warning: not tested\n        public class Interval\n        {\n            public DateTime From;\n            public DateTime To;\n        }\n\n        public class TestCase\n        {\n            public int Id;\n            public DateTime From;\n            public DateTime To;\n            public bool IsGood;\n\n            \/\/ We need this override, so test cases are shown well in nUnit. Otherwise they all look like MyTestClass+TestCase\n            \/\/ We could show actual dates, but it is kind of long, so I opted for a numeric ID\n            public override string ToString()\n            {\n                return \"TestCase \" + Id;\n            }\n        }\n\n        private static TestCase T(int id, DateTime from, DateTime to, bool isGood)\n        {\n            return new TestCase { Id = id, From = from, To = to, IsGood = isGood };\n        }\n\n        private static readonly DateTime Before = ...;\n        private static readonly DateTime Inside = ...;\n        private static readonly DateTime After = ...;\n\n        private static readonly TestCase[] TestCases =\n        {\n            T(1, Before, Before, false),\n            T(2, Before, Inside, true),\n            T(3, Before, After, true)\n        };\n\n        public void TestIntervalIsGood(<font color=\"red\"><b>[ValueSource(nameof(TestCases))]<\/b><\/font> TestCase testCase)\n        {\n            bool isGood = IsIntervalGood(testCase.From, testCase.To);\n            Assert.AreEqual(testCase.IsGood, isGood);\n        }<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>nUnit has a great feature of running multiple similar test cases via data-driven approach: [TestCase(&#8220;&#8221;, &#8220;&#8221;)] [TestCase(&#8220;q&#8221;, &#8220;q&#8221;)] [TestCase(&#8220;xyz&#8221;, &#8220;zyx&#8221;)] public void TestStringReverse(string s, string expectedResult) { var result = <a href=\"https:\/\/ikriv.com\/blog\/?p=2084\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4],"tags":[],"class_list":["entry","author-ikriv","post-2084","post","type-post","status-publish","format-standard","category-hack"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2084","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2084"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2084\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}