Tuesday, March 13, 2012

Unit tests


Do you use Unit tests on the regular base?
You can ask me - what is this? what exactly a unit tests?
I believe a lots of us  too lazy for using Unit tests, and that's true.  I would be willing to bet that only about a few  really do.
You see, the main problem  here is that when most developers are asked to provide any definition of unit testing they will say something like “tests that are cover every unit in  your application” or something like that. Yes, that's partially true.  Let me provide you some info regarding Unit Tests.
Unit test  it's s a test which verifies a “unit”, or the smallest piece of an application which is able to be tested.  Unit testing is all about testing small pieces in isolation.

Why should we use unit tests?

Unit tests find problems early in the development cycle.
Then  earlier problems are found, then cheaper it is to fix them.

The development process becomes more flexible
Sometimes it may be required to fix a problem and to deploy the new fix very quick. Despite efforts, a bug may hides in and an important feature may out of order.  Releasing quick fixes makes us feel uneasy because we are not certain what side-effects the changes might have. Running the unit tests with the fixes applied saves the day as they should reveal undesirable side-effects.

Unit tests are fast.
It's all  about point above.  after any changes you will definitely know  current  status of your source code, just tun unit test that you have wrote before.

At the end of the end, I would like to show you  how do I test QuickSort Method, that I've wrote recently.
I'll use NUnit framework  for  this purpose.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
namespace TestLib
{
    [TestFixture]
    public class TestQuickSort
    {
        [Test, TestCaseSource("Test_Success_Cases")]
        public void SortTest(int[] A)
        {
            bool verifyForNull = (A == null ? true : false);

            SortLib.QuickSort<int> sort = new QuickSort<int>();
            sort.Sort(A);
            if (!verifyForNull && A == null)
                Assert.Fail("Sorted Array is null, but should not be");
            if (verifyForNull)
            {
                Assert.IsNull(A);
                return;
            }

            int tmp = int.MinValue;
            for (int i = 0; i < A.Length; i++)
                if (tmp > A[i])
                    Assert.Fail("Array not sorted properly");
                else
                {
                    tmp = A[i];
                }

            Assert.Pass("Sorted properly");



        }



        static int[] GenArray(int size, int maxVal)
        {
            int[] result = new int[size];
            Random rnd = new Random();
            for (int i = 0; i < size; i++)
            {
                result[i] = rnd.Next(0, Math.Abs(maxVal));
                if (maxVal < 0)
                    result[i] *= -1;
            }
            return result;
        }

        static object[] Test_Success_Cases =
                     {
                         null,
                         new int[0],
                            GenArray(10,-11),                          
                            GenArray(10,0),                          
                            GenArray(1,1), 
                            GenArray(10,11), 
                            GenArray(100,101), 
                            GenArray(1000,1001), 
                            GenArray(1000000,10000001)
                           
                     };
    }
}

Yes, I know this not too deep dive description of Unit test, but I'll add some thing more additional later.

No comments:

Post a Comment