Skip to main content

ABC of Unit Testing

What is Unit Testing?
After writing any code, every programmer will do some kind of testing to make sure the code works as expected. This testing is called 'unit testing'.
Unit testing is done in different ways. Some programmers write simple test applications to test their own code. Some others simply debug the code and change the values during debugging to make sure the code works fine for different cases.
Some facts about unit testing:
  • Unit testing is done by developers.
  • Quality department does different tests, they are not unit tests.
  • Every programmer must do unit testing after finishing development or during development itself.
  • Unit testing can be done by writing separate test applications to call your classes and methods to make sure the classes work as expected. This is called manual unit testing.
  • Unit testing can be automated by writing unit test scripts. This is called Automated Unit Testing.
  • In automated Unit Testing, after writing each class/method, you will write several Test methods to test the actual code. The test method will call your actual class/method with some sample inputs and compare the result with expected result. If the actual result from your code is same as the expected result, the Unit Test will record the result as "Success". Otherwise the test result will be recorded as "Fail". All of the unit test scripts can be executed together and you can get a summary of all results.
  • Automated Unit test scripts can be saved and easily repeated so that everytime someone make a change in the project, hundreds of automated unit tests can be executed in few seconds to make sure that every piece of code is still working as expected.
  • NUnit is one of the most popular automated unit testing tool for .NET applications.
  • Automated unit tests can be done without using any unit testing tool like NUnit. However, unit test frameworks like NUnit make the job very easy.

Manual Unit Testing

Assume you are writing a class to calculate the Factorial of a number. You may write a class called 'Calculator' with a method called 'Factorial()' taking one input parameter and returning the Factorial of the input.

public class Calculator

{

public int Factorial(int a)

{

int factorial = 1;

while ( a > 0 )

{

factorial *= a; --a;

}

return factorial;

}

}

After you write the above class and method, you are done with your job. But before you deliver your code to your team leader, you must test the code to make sure this code works fine as expected for different cases.

Most of the programmers test the code by writing a small test application. You may create a new windows forms project and add some labels, a textbox and buttons etc to the form. In the above case, you need one textbox to input the number to find the Factorial. Also, you need a 'Calculate' button. When user inputs a value to the textbox and press the button, you will call the method and pass the user input value to the method. Then you will display the result as a messagebox. By seeing the result you will manually verify it to make sure that the code worked as expected.The above process is called 'Manual Unit Testing'.

Limitation of Manual Unit Testing

Some of the issues of manual unit testing are:

  • You will write test applications and throw away after doing your testing. After few weeks you may make some modifications to your code and at that time you may not find your old test application. So, you will have to write another test application.
  • Large projects will have hundreds of classes and several class libraries. It is very difficult to write separate test applications for each of the classes in the project and save all of them for repeated testing in future.
  • Test applications require a human person to test. You may test several possibilities on the first time, but you may not remember all of the test cases when you repeat the test in future.
  • When you do manual test, you are manually verifying the result. Even if the result is wrong, you may not realize it and think that the test was success.
  • It is not practical to repeat all tests manually before you make a product release everytime. It will take several hours or days to complete manual unit testing of every piece of code.

Automated unit testing solves all of the above problems

What is Automated Unit Testing?

Automated unit testing is the process of testing software using test scripts. A unit test is a piece of code that executes another piece of code and compare the actual result with expected result. In above example, you saw how to write a small class to calculate the Factorial of a number. Now let us try to write a small test script to test the Factorial() method. A simple test script will look like this:

public void TestFactorialMethod()

{

// Instantiate the calculator class.

Calculator calc = new Calculator();

// Call the Factorial method to calculate the factorial of 5.

int result = calc.Factorial(5);

// We know that the factorial of 5 is 120. The job of the test method is to compare the result from the factorial method with the expected value and make sure it is same as the expected value

if ( result != 120 )

{

MessageBox.Show("Expected value is 120, but actual value is '" + result + "'.");

}

}

The above method calls the Factorial method and compares the result. If the result is same as the expected result, then you know that the test is success. This indicates your Factorial method is working as expected.In the above case, the test script calls the Factorial method with one input (5). In real development environment, you have to write several test scripts to test each method, with various input values and make sure the result is correct.The above test script is not perfect. It displays a messagebox when the test fails. This requires someone has to manually look for the messageboxes.

Automated Unit Test Frameworks like NUnit solves this problem.

What is Nunit?

  • NUnit is an automated unit testing framework for .Net applications.
  • NETNUnit is free to use with your .NET projects.
  • NUnit is ported from another product called JUnit, which is a unit testing framework for Java applications.
  • NUnit provides a class library which includes some classes and methods to help you write test scripts.
  • NUnit provides graphical user interface application to execute all the test scripts and show the result as a success/failure report (instead of message boxes from the test script).
  • NUnit is the most popular unit testing framework for .net applications.
  • NUnit does not create any test scripts by itself. You have to write test scripts by yourself, but NUnit allows you to use it's tools and classes to make the unit testing easier.
  • NUnit is an open source product. You can get this tool along with source code and modify the source code if required for your specific needs. However, 99% of the programmers do not mess with the source code. Instead, you can download only the executable and class library and implement unit test scripts for your application.
  • Now Microsoft offers a team collaboration system called 'Visual Studio Team System' (VSTS). This tool includes a unit testing framework also. So, eventually most of the developers may switch to VSTS instead of NUnit. However, NUnit will continue to exist because it is free and easy to use.

Comments

Jagran Today said…
Unit Software Testing = The unit testing features of Visual Studio 2012 were shown to be an effective way to improve software quality by introducing various tests.

Popular posts from this blog

Revolutionize software testing with AI! 🤖💻 Share your thoughts on ethical implications in the comments.

  As technology evolves, so too does the field of software testing. One exciting development in recent years is the use of AI (Artificial Intelligence) to automate repetitive tasks and improve testing accuracy. Through analyzing large amounts of data and identifying patterns, AI can help identify potential defects or vulnerabilities in software. AI-powered tools can also generate test cases and scenarios by simulating user behavior and input, allowing for more efficient and effective testing. In addition, machine learning algorithms can analyze and learn from past testing data, leading to better predictions and more streamlined testing. AI-powered tools can also help identify and prioritize critical bugs and defects, saving valuable time and effort in manual testing. But it's important to note that AI-powered testing is not a replacement for human testers. While AI can automate certain tasks and help identify potential issues, it's still necessary for human testers to provide a

Is AI taking over your job in software development and testing? 😱"

Are you a software developer or tester feeling threatened by the rise of AI in your industry? 😰 You're not alone. Many professionals in the field are concerned about the potential consequences of AI's integration into software development and testing. While some experts believe that AI can bring significant benefits to the industry, others worry that it could replace human expertise altogether. 🤔 AI algorithms can analyze massive amounts of data and automate many tasks, but they cannot replace the critical thinking and creativity of human beings. Additionally, there are ethical concerns associated with the use of AI in software development and testing. So, what can you do to ensure that you're not replaced by AI in your job? 💪 First, it's essential to recognize that AI is not a replacement for human expertise but rather a tool to augment it. Therefore, it's essential to learn how to work with AI-powered systems to increase your efficiency and productivity. Additi

HP Quality Center - Best Practices

1.Introduction Quality Center is a test management tool which provides very good features for managing both your manual and automated test cases. This paper highlights the best practices for managing your test cases. When you open Quality center, depending on your rights it display the below mentioned option in the sidebar: 1. Requirements 2. Test Plan 3. Test Lab 4. Defects 5. Dashboard 2.Requirements When you have assigned with the responsibility of developing your test cases in the quality center then you must be wondering where to start with. I am going to share my experience to overcome such a situation. You need to find the solution of some question before you start writing your test cases. 1. Is your requirement developed and available? 2. Is your requirement organized in a logical sequence? If answer to both of the above question is Yes, then you can start with Requirement option in the side bar. In case your requirement is under development, then you keep your