UnitTesting – Asserts, etc.

Unit testing is well known way to provide quality code. This article presents a few options how to return test result (success/ fail).
There are following classes we can make use of:
Assert – verifies specific conditions. It contains following methods (among others): IsTrue, AreEqual, Fail, IsNotNull.
CollectionAssert – verifies specific condition for collections, i.e.: AreEqual, AreEquivalent, DoesNotContain, IsSubsetOf.
StringAssert – verifies specific condition for string, i.e.: StartsWith, Contains, EndsWith, Matches.

What’s more there is ExpectedExceptionAttribute which marks a test method that it should throw an exception. When exception is not thrown, it means that test failed.

There is also special exception type AssertInconclusiveException. It means that test does not return any valid result (success or fail). It can be used i.e. when test method implementation is not finished yet.

See a few examples below. I implemented simple classes Person and SimpleMath and a few unit tests to test implemented functionalities.

        /// <summary>
        /// Assert.IsTrue checks whether given condition is satisfied
        /// </summary>
        [TestMethod]
        public void SimpleMathRandomDateTest()
        {
            var minDate = DateTime.Now.AddYears(-1);
            var maxDate = DateTime.Now;
            var randomDate = SimpleMath.GetRandomDate(minDate, maxDate);
            Assert.IsTrue(minDate <= randomDate && randomDate <= maxDate);
        }

        /// <summary>
        /// Assert.AreEqual checks whether two given objects are equal
        /// </summary>
        [TestMethod]
        public void SimpleMathMinOddTest()
        {
            var numbers = Enumerable.Range(10, 100);
            var result = SimpleMath.GetMinOddNumber(numbers);
            Assert.AreEqual(11, result);
        }

        /// <summary>
        /// CollectionAssert.AreEquivalent checks whether two collections have the same elements in the same quantity, but in any order
        /// </summary>
        [TestMethod]
        public void SimpleMathEvenNumbersEquivalentTest()
        {
            var min = 10;
            var max = 14;
            var expected = new List<int>() { 14, 10, 12 };
            var result = SimpleMath.GetEvenNumbers(min, max).ToList();
            CollectionAssert.AreEquivalent(expected, result); // returns success
        }

        /// <summary>
        /// CollectionAssert.AreEquivalent checks whether two collections have the same elements in the same quantity and order
        /// </summary>
        [TestMethod]
        public void SimpleMathEvenNumbersEqualTest()
        {
            var min = 10;
            var max = 14;
            var expected = new List<int>() { 14, 10, 12 };
            var result = SimpleMath.GetEvenNumbers(min, max).ToList();
            CollectionAssert.AreEqual(expected, result); //returns fail (as order of elements is different)
        }

        /// <summary>
        /// CollectionAssert.IsNotSubsetOf checks whether one collection is not a subset of the other
        /// </summary>
        [TestMethod]
        public void SimpleMathEvenAndOddNumbersNotSubset()
        {
            var min = 10;
            var max = 14;
            var resultEven = SimpleMath.GetEvenNumbers(min, max).ToList();
            var resultOdd = SimpleMath.GetOddNumbers(min, max).ToList();

            CollectionAssert.IsNotSubsetOf(resultEven, resultOdd);
        }
		
		/// <summary>
        /// StringAssert.Contains checks whether returned string contains specific substring
        /// </summary>
        [TestMethod]
        public void PeronGetFullNameTest()
        {
            var person = new Person("John", "Smith", 20);
            StringAssert.Contains(person.SayYourFullName(), person.LastName);
        }

        /// <summary>
        /// ExpectedException attribute marks that method should throw an exception
        /// </summary>
        [ExpectedException(typeof(ArgumentException))]
        [TestMethod]
        public void SimpleMathEvenNumbersInvalidInputTest()
        {
            var min = 16;
            var max = 14;
            var resultEven = SimpleMath.GetEvenNumbers(min, max).ToList();
        }

        /// <summary>
        /// AssertInconclusiveException is thrown and it means that test is skipped as it does not return any specific information yet
        /// </summary>
        [TestMethod]
        public void SimpleMathCalculateAreaTest()
        {
            throw new AssertInconclusiveException("Test not implemented yet.");
        }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s