A colleague of mine wrote a simple console calculator app that would accept inputs and output the results. The aim was to identify what test we would perform as Unit Tests and what tests we would perform as integration, automated acceptance tests and manual tests.
We were given the solution at the end of the session, minus any tests. I thought it would be useful to write Unit Tests for the calculator app myself, seeing as most of my experience is with Selenium WebDriver using C#, I thought this would be a useful learning experience for me.
I'll briefly explain the architecture of the calculator, before I delve off into the world of what unit tests I created at first... here's a simple diagram of how it all sits together.
with unit tests / without unit tests), so have a look at it, have a play and see how it all sits together. We can then go about creating the actual unit tests together.
Firstly, it's important to understand that the calculator app can only have 2 inputs, and an operator. I will start by creating unit tests for the Calculation.Service.
Firstly I need to create a new project within the solution, I will call it Calculation.ServiceTests, in here I will have references to nunit.framework and Calculation.Service in order to reference the code in that project.
I will create an empty class, and call it CalculatorTests, this is where all the unit tests will live.
You should now have the following setup:
This is so that you can use Nunit to write the assertions and invoke the Calculation.Service to run the tests against. Once we have that, the next step is to create an Nunit TestFixture inside the Calculation.ServiceTests namespace.
Then create a new class called CalculatorTests.
You will end up with the following:
Looking at the Calculation.Service we can see that it returns an int based on a calculationRequest, which is made up of the following:
It's made up of 2 integers, and an operation. Our first test will be a simple test checking that it can add 2 numbers together.
We will start by creating an Nunit [Test] attribute inside the CalculatorTests class, and then we'll have a public void (a void is basically a method), and name it based on the Unit Test that we are creating.
We will now start to create the actual test. The test will be made up of a CalculationRequest, which as discussed above will be 2 integers and an Operation, we will then invoke the Calculation Service to perform the sum, and then an assertion that the sum is correct.
I will start by creating the CalculationRequest, we will have a variable named calculationRequest, which will be a new CalculationRequest, and contain the integers and the operation.
So we have 2 integers, 3 and 7, and have the CalculationOperation.Add (+). We will pass this into the Calculation service to perform the Calculation, and return an integer. This will be achieved with the following code:
We now have sum as an integer, which is whatever the calculator returned to us. The final step for this UnitTest will be to create the assertion, this will be written in Nunit, and will be as follows;
So there we have it, our first Unit Test for the calculator service.
When we run this test it will pass. This test isn't complete though, we want it to be able to test more than just adding 7 and 3, we want it to test a multitude of number, we can achieve this by rather than having an Nunit [Test] attribute, we provide a [TestCase] attribute instead. We can also parametrize the X and the Y in the CalculationRequest, and add this to the TestCase attribute, we will end up as follows:
Feel free to download the solution and create your own unit tests, and let me know how you get on or if you'd have any improvements to the above.