NUnit


What Are Unit Tests?
A unit test is nothing more than the code wrapper around the application code that permits test tools to execute them for fail-pass conditions.

NUnit
  • NUnit is a unit-testing framework for all .Net languages. 
  •  It’s open source and free; it’s being actively developed, and it meshes well with .NET.
  • Unit tests in NUnit are nothing more than methods in a class. The key is that these methods and classes are decorated with custom attributes.
  • The TestFixture attribute identifies this class to NUnit as a class that contains tests.
  • The Test attribute identifies the Create Transformer method as a unit test. It’s a very simple test: All it does is make sure that a new instance of a particular class can be created without raising an exception.
  • NUnit tests can also check the results of an operation
  • It doesn’t matter whether you keep your tests in the same assembly as the actual code or in a completely different project. 
  • You don't need to do anything beyond adding the attributes to tell NUnit about your tests. 
  • NUnit uses .NET reflection to find tests; all you have to do is tell it which assembly to look in.
  • Reflection is the capability that lets .NET code retrieve information about other .NET code. NUnit uses reflection to find, and then execute, all of the tests in a particular assembly.
  • There are several ways to actually execute NUnit tests. One is via the graphical test runner 
             a. Green dots show successful tests
             b. The yellow progress bar indicates that I’ve temporarily set one of the tests in my test suite to be                     ignored because I’m working on it. (NUnit won’t show you all green unless every test is active                       and passed.)
             c. A red dot or bar would indicate a failing test. You can see that running the entire test suite of                          more than 300 tests took a bit less than three seconds on my development computer.
  1. Passed Test - Green color
  2. Failed Test - Red color
  3. Ignored Test - Yellow color
NUnit also has a command line version that lets you invoke a test suite without bringing up the graphical interface.
 
 
Two immediate benefits to working with NUnit
  • First, coding actually goes faster when you write the tests first. That seems paradoxical, but I think it’s a matter of mental focus. At any given time, I’m focused directly on whatever mini-milestone. I need to meet to make the next test work. I’m no longer tempted to go charging around the program, implementing a dozen things at once as long as I’m in the code.
  • Even more important is the feeling of security that this style of development can bring to the programmer. For example, have you ever released an application and worried that perhaps something had broken at the last minute when you made hurried code changes? That’s much less likely to happen when you use NUnit. By testing each piece of the application as you write it, and making sure that all of the tests always turn out right, you can gain confidence in your own work. Ultimately, that translates to delivering better code—and happier clients. I think those benefits are well worth the effort of learning to write tests first

What Should Be Tested?

  • This is a common and valid question. Typical test cases are:
  • Test for boundary conditions, e.g. our Calculator class only multiply signed integers, we can write a test for multiplying two big numbers and make sure our application code handles it.
  • Test for both success and failure.
  • Test for general functionality

What NUnit Framework is not?  
  • It is not Automated GUI tester.
  • It is not a scripting language, all test are written in .NET supported language e.g. C#, VC, VB.NET, J# etc.
  • It is not a benchmark tool.
  • Passing the entire unit test suite does not mean software is production ready.

Understanding Terms.

[TestFixture]
The [TestFixture] attribute denotes a class that holds automated NUnit tests. (Replace the word "Fixture" with "Class", it makes much more sense.)
[TestCase]
The [Test] attribute can be put on a method to denote it as an automated test to be invoked. Put this attribute on your new test method.
[Assert]
The Assert class has static methods and is located in the NUnit.Framework namespace. It's the bridge between your code and the NUnit framework, and its purpose is to declare that a specific assumption is
Supposed to exist. If the arguments that are passed into the Assert class turn out to be different than what we're asserting, NUnit will realize the test has failed and will alert us. We can optionally tell the Assert class what message to alert us with if the assertion fails. The Assert class has many methods, with the main one being Assert.IsTrue (some Boolean expression), which verifies a Boolean condition. But there are many other methods. This one verifies that an expected object or value is the same as the actual one:
1. Assert.AreEqual (expectedObject, actualObject, message);
Here's an example:
1. Assert.AreEqual (2, 1+1, "Math is broken");
[Setup]
Is generally used for any initialization purpose. Any codes that must be initialized or set prior to executing a test are put in functions marked with this attribute. As a consequence, it avoids the problem of code repetition in each test.
NUnit framework support following assertions:
Assert()
AssertEquals()
AssertNotNull()
AssertNotNull()
AssertNull()
AssertSame()
Fail()
You can use as many Assert statements in a method as you like. However, NUnit framework, will show a method as failed if even a single assertion fails, as expected. But what is important to remember is that if first assertion fails, next assertion will not be evaluated, hence you will have no knowledge about next assertion. Therefore it is recommended that there should only be one Assertion statement per test method. If you believe there should be more than one statement, create a separate test case method.
[Ignore]
This is the attribute which is used for the code that needs to be bypassed.
[ExceptionExpected]
This attribute is used to test methods that need to throw exception in some cases. The cases may be FileNotFoundException and others.
[TearDown]
This attribute denotes a method to be executed once after each test in your class has executed.

Conclusion

Its common practice to have one test class per tested class, one test project per tested project, and at least one test method per tested method.
Use the [SetUp] and [TearDown] attributes to reuse code in your tests, such as code for creating and initializing objects all your tests use.


Hope you enjoyed this ride about unit testing and NUnit.

References:

Comments

Popular posts from this blog

SQL Server 2016 TDE ( Transparent Data Encryption)

Setting up Dotnetnuke (DNN) to work with Active Directory

Programming !!!!!!!!!!!