C#: Make base class containing common tests abstract

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 the algorithm.

Resharper test runner insisted on executing tests from the “naked” base class and then complained that “No suitable constructor was found”. I fixed this by making the base class abstract. Fortunately, Resharper test runner does not go as far as to try to instantiate abstract classes.

    public abstract class SortTestBase
    {
        private readonly Action _sort;

        protected SortTestBase(Action sort)
        {
            _sort = sort;
        }

        [Test]
        public void EmptyArray()
        {
            var arr = new int[0];
            _sort(arr);
            CollectionAssert.AreEqual(new int[0], arr);
        }

        ... more tests ...
    }

    [TestFixture]
    public class QuickSortTest : SortTestBase
    {
        public QuickSortTest() : base(QuickSort.Sort)
        {
        }
    }

2 Comments


  1. У тебя в ЖЖ HTML форматирование поломано (только этот post). Я смотрю в Хроме на Маке.

    Reply

    1. Спасибо. Это он дженерики за HTML теги принял. Попробую исправить

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *