What is Unit testing and what benefits I will have if I apply it on my project

What is Unit Test:

In computer programming, unit testing is a software design and development method where the programmer verifies that individual units of source code are working properly. A unit is the smallest testable part of an application.

In OOP, the smallest unit is a method, which may belong to a base/super class, abstract class or derived/child class.

Unit testing is typically done by software developers to ensure that the code other developers have written meets software requirements and behaves as the developer intended.

Benefits of Applying Unit Test:

  • Using developer time for manual ad hoc testing is not a cost effective way of ensuring software quality. (Code coverage, breaking code by others, did I test all functionalities)
  • Human tester can and will make mistakes.. Even if Company have QA, discovering issues at QA side is more expensive than @ DEV side.
  • unit tests find problems early in the development cycle.
  • an automated unit test suite watches over your code in two dimensions: time and space
  • developers will be less afraid to change existing code
  • the development process becomes more flexible (Sending hotfixes more quickly)
  • Improves your project’s truck factor:

Truck factor is the minimum number of developers that if hit by a truck the project would grind to a halt. If you have one key developer who works on a subsystem that no-one else knows, then your truck factor is effectively “1”. Obviously, having truck factor of “1” is a risk on the project.

A comprehensive unit test suite improves truck factor because it makes it easier for a developer to take over a piece of code she is not intimately familiar with. A developer can start working on code written by others because the unit tests will guide the developer by pointing it out if she makes an error. Losing a key developer for any reason just before a release is less of a catastrophe if you have the safety net of unit tests.

  1. reduces the need for manual testing

Flavors of Unit testing:

  • Point unit tests exercise a small piece of functionality, e.g., just one method or methods of a class.
  • End-to-End tests exercise one feature across many, sometimes all, layers of an application; An ideal end-to-end test works like an end user story

Test Driven Programming model: Developer start developing test methods and basic functions that make test methods success, then enhance code at methods to make sure it will cover all expected/unexpected cases.

As a rule, before a developer checks in any code into the main source control repository, she should get the latest versions of the unit tests from the version control on her development machine and run all the tests locally. Only after all unit tests pass should the developer check in new code. This ensures that the source code in the version control system remains healthy at all times and can be used to rebuild the system at any time.

In real life, developers may accidentally check in code that does not work. The daily build should be your main line of defense against these bugs.

Real Word Facts:

Trusting unit tests blindly as in: "if unit tests pass, code is ready for production" is a recipe for shipping bugs. Even if we have a comprehensive unit test suite that is run frequently, bugs can still sneak in and some amount of manual testing by humans is needed.

What if Bug appear on Production:

We need to do "post mortem" analysis.. One of the following things turned out:

  1. We have 2 bugs: one inside actual Code and the other inside Unit test code.
  2. There is no unit test code that testing this functionality.
  3. Production environment is different from Test environment.

              Finally, If you have plenty of time at your project to apply Unit testing on it, the I would strongly recommend applying it.

How to solve XML configuration problem when you use unit testing

One of the first challenges I faced when I decided to use Unit Testing projects to test my Methods/Classes, is how to use XML configuration files with unit testing projects…

Basically the issue appear because my project using XML file as configuration file. And when I compile my Unite testing project, the compiler will copy only DLLs related to project being tested with no other resources including the configuration file(s) … this will cause all of my unit test methods to be failed because there is no configuration file available for Unit Test Project.

The solution is pretty easy… all what you have to do is to add the following attribute to Test Method header. This attribute will tell compiler to copy the file into Bin folder and things will go smooth.

[TestMethod(),DeploymentItem("XmlConfigurationFileName.XML")]

public void TestMethodName()

{

         //Method body goes here….

}

Test Run deployment issue: The location of the file or directory is not trusted… Error Message

Today I faced small problem while I am building Unit testing project to test my class library. in my class library I was using a DLL file located on a shared folder on my company’s network.. when I tried to run the test project I got this error message:
Test Run deployment issue: The location of the file or directory is not trusted

the solution of this issue was about adding the network path to my trusted sites.. to do so I followed the following steps:

1) open "Microsoft .NET Framework 2.0 Configuration" from administrative Tools
2) Go to "Runtime security Policy" –> Machine –> Code Groups –> All Code
3) right click –> New .. then give a name and description for the new group –> click Next
4) in the new form choose "URL" and then type your Network path. you can type it in 2 ways:

a) Y:\* –> for the whole folder
b) y:\AssemblyName.dll –> for single DLL

5) Choose Next… and then choose "Full trust"
6) click Next and then finished
7) finally close you VS and reopen it again
8) have fun in using unit testing 🙂

Thanks Microsoft for such a nice integration between tools:)