Unit testing is fun. Somehow. At least in C# 🙂
As far as I am making a living mainly as a VBA developer and I am taking a look at other technologies, I am always somehow jealous of how sufficient and advanced Unit Testing functionality they have in their environments. I mean, if you compare the Visual Studio unit testing with the VBA unit testing the result is strange – you compare something with nothing. 🙂 I personally know two ways to unit test VBA and I am using this one usually. But now I will write about unit testing in C# 🙂
- How do you start unit testing in Visual Studio? There is a specific project for this, which is located here:
- Then you should add the solution you would like to test as a reference to the unit test.
- At the end you should write the tests and run them as here:
Easy to be said, than done. The tricky part in writing the unit tests is to think of anything, that you would need to run the test. E.g. in my program for Fibonacci, I have a public variable my_memo. Of course, this variable is used by the function for Fibonacci, which I am testing, thus the variable should be set from the test. And in order to set it, we should use the class name for it. E.g. FibonacciChecks.my_memo = new long[l_fib + 1];
The second tricky thing is to name your “public void TestWith0” correctly. If you do this, you would understand immediately which test is failing. Third tricky thing is to test always as strange things as you can – e.g. an input with 0, with 99999999, with a string, with Null, with empty string, with object, with no input. Trying to get all corner cases and user craziness. Anyhow, there are books about Unit testing and my idea was just to show how it is done in Visual Studio.
Pretty much that is all. Here comes the Unit Testing Code:
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 |
namespace Fibonacci { using System; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class FibonacciTests { [TestMethod] public void TestWith10() { long l_fib = 10; FibonacciChecks.my_memo = new long[l_fib + 1]; long l_result = FibonacciChecks.FibWithMemo(l_fib); Assert.AreEqual(l_result, 55); } [TestMethod] public void TestWith20() { long l_fib = 20; FibonacciChecks.my_memo = new long[l_fib + 1]; long l_result = FibonacciChecks.FibWithMemo(l_fib); Assert.AreEqual(l_result, 6765); } [TestMethod] public void TestWith0() { long l_fib = 0; FibonacciChecks.my_memo = new long[l_fib + 1]; long l_result = FibonacciChecks.FibWithMemo(l_fib); Assert.AreEqual(l_result, 0); } [TestMethod] public void TestWith1() { long l_fib = 1; FibonacciChecks.my_memo = new long[l_fib + 1]; long l_result = FibonacciChecks.FibWithMemo(l_fib); Assert.AreEqual(l_result, 1); } [TestMethod] public void TestWith2() { long l_fib = 2; FibonacciChecks.my_memo = new long[l_fib + 1]; long l_result = FibonacciChecks.FibWithMemo(l_fib); Assert.AreEqual(l_result, 1); } } } |
And here the Fibonacci code (once again):
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 |
using System; namespace Fibonacci { public class FibonacciChecks { public static long[] my_memo; public static DateTime my_time; public static long my_counter; public static void Main() { Console.WriteLine("Enter number between 2 and 1300 for Fibonacci calculation:"); long k = long.Parse(Console.ReadLine()); my_memo = new long[k + 1]; my_counter = 0; my_time = DateTime.Now; Console.WriteLine("\n{0}", FibWithMemo(k)); Console.WriteLine("Calculations {0}", my_counter); Console.WriteLine(DateTime.Now - my_time); } public static long FibWithMemo(long counter_inside, bool with_memo = true) { if (with_memo) { if (my_memo[counter_inside] != 0) return my_memo[counter_inside]; } my_counter++; if (counter_inside == 0) return 0; if (counter_inside == 1) return 1; my_memo[counter_inside] = FibWithMemo(counter_inside - 1, with_memo) + FibWithMemo(counter_inside - 2, with_memo); return my_memo[counter_inside]; } } } |
😀