C# – Unit Testing In C# – Fibonacci Example

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:unit_test1
  • Then you should add the solution you would like to test as a reference to the unit test.unit_test2
  • 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:

And here the Fibonacci code (once again):

In GitHub.com it is here.


Tagged with: , ,