Friday, 10 May 2013

Tutorial: Creating Unit Tests 101

This blog post is what my last blog post was introducing, if you read my last blog post then you'll understand about unit tests and why they're important, now we're going to try and create some unit tests....

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.

You can download the solution (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:



Open up CalculatorTests and in there you will need to have the following Usings:


using Calculation.Service;
using NUnit.Framework;


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:
The TestFixture attribute marks the class as containing a set of tests, you can then use Setup or Teardown if you require anything to be set up or any test data created for your tests.

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:


We can now create multiple TestCases for adding two numbers together, providing we pass to the method 2 integers that we wish to add up, and an expected result (which we use in the assertion).

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.

15 comments:

  1. how can i create: Calculation.Service Reference?

    ReplyDelete
    Replies
    1. http://msdn.microsoft.com/en-us/library/wkze6zky(v=vs.80).aspx :)

      Right click on the References folder and select Add Reference, then select the Calculation.Service

      Delete
  2. Thanks for your informative post on selenium automation testing tool. Your article helped me a lot in understanding the future of automation testing tool and its career prospects. Selenium Training

    ReplyDelete
  3. The future of software testing is on positive note. It offers huge career prospects for talented professionals to be skilled software testers. software testing course in Chennai | Software Testing Training in Chennai | Software testing course in Chennai

    ReplyDelete
  4. Great information. I have got some important suggestions from it. I'm working in ERP Software Company in Chennai | ERP Providers in Chennai. Get your business to the next level in simple steps.

    ReplyDelete
  5. Thank you for this valuable information. I have got some important suggestions from it. I'm working in Brave Technologies Private Limited. We provides lowest price of ERP Software for our clients. Contact us on info@bravetechnologies.in. ERP Providers in Chennai

    ReplyDelete
  6. Thank you for this valuable information. Writing efficient unit test cases are not easy job, it is art of science!!

    JMeter Training in Chennai

    ReplyDelete
  7. The Blog gave us idea to create the Unit Test My sincere Thanks for sharing this post and please Continue to share This kind of post
    Software Testing Training in Chennai

    ReplyDelete
  8. really you have been shared very informative blog. it will be really helpful to the peoples who are all working under the technology.
    software testing training in chennai

    ReplyDelete
  9. The Blog gave us idea about the data driven testing My sincere Thanks for sharing this post and please continue to share this kind of post.
    Software Testing Training in Chennai | Selenium Training in Chennai

    ReplyDelete
  10. Nice testing forum. Good examples with software architecture.

    Manual Testing Training in chennai

    ReplyDelete
  11. The blog gaveidea to automate flash and windows control using selenium
    My sincere thanks for sharing thos post

    Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium.


    Software Testing Training in Bangalore
    Software Testing Training in BTM Layout


    Software Testing Training in Marathahalli

    ReplyDelete
  12. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.

    Software Testing Training in Marathahalli|

    Software Testing Training in Bangalore|

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete