Skip to main content

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.


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

    1. :)

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

  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

    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.

  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

  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.

  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 ERP Providers in Chennai

  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

  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

  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

  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

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

    Manual Testing Training in chennai

  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

  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|

  13. This comment has been removed by the author.

  14. This comment has been removed by the author.

  15. Interesting post! This is really helpful for me. I like it! Thanks for sharing!

    Webseiten Gestaltung Lüdenscheid

  16. I really understand the last blog easily understand the concepts of unit test...Keep update your unit test..
    Java Training Institute in Chennai | DotNet Training Institute in Chennai | Web Designing Training Institute in Chennai


  17. The website is looking bit flashy and it catches the visitors eyes. A design is pretty simple .
    office 2010 professional plus key deutsch

  18. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, have a nice weekend!

    Data Science Training in Bangalore

  19. Interesting post! This is really helpful for me. I like it! Thanks for sharing!
    WebDesign Lüdenscheid

  20. Thanks For Your valuable posting, it was very informative

  21. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
    aws training in Chennai

  22. Those guidelines additionally worked to become a good way to
    recognize that other people online have the identical fervor like mine
    to grasp great deal more around this condition.

    white label website builder


Post a Comment

Popular posts from this blog

Advantages of using Test Management tools

Before I start talking about test management tools, let me clarify what I mean by the term test Management tools...  I am not taking about your office excel program where you store your test cases in. I'm talking about bespoke test Management tools, your quality centers or Microsoft test manager...
In the strict case of the term test Management tool, Microsoft Excel can be used as such, but heck, so could a notepad if used in the right way... For the sake of this blog post I am talking about bespoke test Management tools.
Firstly, what test tools are out there? There are many more out there today than when I first started in QA over 5 years ago. When I started the market was primarily dominated by a tool called Quality Center, this would run in a browser (only Ie unfortunately) and was hosted on a server.. Nowadays it's market share has somewhat dwindled, and there are some new kids on the block. 
One of the more popular tools is that of Microsoft Test Manager, it's big…

What is a PBI?

After my last post, I had the question of what is a PBI... so I thought i'd write a short blog post about what they are and why they are used.

A PBI is an acronym for Product Backlog Item. It is a description of a piece of work that your SCRUM team will develop and deliver. When you have a list of Product Backlog Items, you then refer to that collective list as a Product Backlog.

The product backlog is often prioritised and yourteam will work through each PBI, and release on a regular schedule... I am however going deep into the world of Agile development, which isn't entirely what this post is about, so I will stop myself now.

A Product Backlog Item is made up of the following:

Title - This is often a one liner that gives the team an idea of what the PBI is about, although it can just be an ID for the item and the team work off of that.

Description - Breaks down the PBI in a bit more detail, and can be written in any style, however I prefer it to be written as follows: 

By writin…

Dealing with Selenium WebDriver Driver.Quit crashes (Where chromedriver.exe is left open)

We recently came across a problem with Selenium not quitting the webdriver and this would then lock a file that was needed on the build server to run the builds.

We were using Driver.Quit() but this sometimes failed and would leave chromedriver.exe running. I looked around and found this was a common issue that many people were having. We (I say we, as we came to the solution through paired programming), came up with the following, that would encapsulate the driver.quit inside a task and if this task takes longer than 10 seconds, then it will clean up any processes started by the current process, in the case of the issue on the build server, it would kill any process started by Nunit.

        public static void AfterTestRun()
            var nativeDriverQuit = Task.Factory.StartNew(() => Driver.Quit());
            if (!nativeDriverQuit.Wait(TimeSpan.FromSeconds(10)))

        private s…