
{"id":2518,"date":"2018-09-03T18:17:31","date_gmt":"2018-09-03T22:17:31","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=2518"},"modified":"2018-09-21T19:04:02","modified_gmt":"2018-09-21T23:04:02","slug":"c-make-base-class-containing-common-tests-abstract-or-nunit-will-run-it-anyway","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=2518","title":{"rendered":"C#: Make base class containing common tests abstract"},"content":{"rendered":"<p>I was testing various sorting algorithms, so I had a base class that <code>SortTestBase<\/code> that contained actual tests and accepted the algorithm in the constructor, and derived classes that supplied the algorithm. <\/p>\n<p>Resharper test runner insisted on executing tests from the &#8220;naked&#8221; base class and then complained that &#8220;No suitable constructor was found&#8221;. I fixed this by making the base class <code>abstract<\/code>. Fortunately, Resharper test runner does not go as far as to try to instantiate abstract classes.<\/p>\n<p><code><\/p>\n<pre>    public <span style=\"background-color:yellow\">abstract<\/span> class SortTestBase\r\n    {\r\n        private readonly Action&lt;int[]&gt; _sort;\r\n\r\n        protected SortTestBase(Action&lt;int[]&gt; sort)\r\n        {\r\n            _sort = sort;\r\n        }\r\n\r\n        [Test]\r\n        public void EmptyArray()\r\n        {\r\n            var arr = new int[0];\r\n            _sort(arr);\r\n            CollectionAssert.AreEqual(new int[0], arr);\r\n        }\r\n\r\n        ... more tests ...\r\n    }\r\n\r\n    [TestFixture]\r\n    public class QuickSortTest : SortTestBase\r\n    {\r\n        public QuickSortTest() : base(QuickSort.Sort)\r\n        {\r\n        }\r\n    }\r\n<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was testing various sorting algorithms, so I had a base class that SortTestBase that contained actual tests and accepted the algorithm in the constructor, and derived classes that supplied <a href=\"https:\/\/ikriv.com\/blog\/?p=2518\" 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":[1],"tags":[],"class_list":["entry","author-ikriv","post-2518","post","type-post","status-publish","format-standard","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2518","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=2518"}],"version-history":[{"count":7,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2518\/revisions"}],"predecessor-version":[{"id":2531,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2518\/revisions\/2531"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}