Now in today’s discussion, we’ll discuss all these things deeply with a demo. So, let me go ahead and create one new class library project as shown below.
Now, I’ll add NUnit Reference in here 1st from Nuget package manager as shown below.
Now, once it gets successfully installed, you can verify the same in references as well as shown below.
So, now we are good to start our testing as all required prerequisites installed successfully. So, now let’s go ahead and create our 1st test class. But before that let’s write a sample class which we want to test. So, below i have created one sample class with the name “Account”
Now, thats a very basic class which is keeping track of your account with balance amount. So, the same class we are going to test here with our new test class as shown below.
so, as you can see in the above screen shot i have annotated this test class with [TestFixture] to tell the compiler that treat this class as a test class and then I have also annotated the test method with [Test] attribute. Now, in this Test method, 1st thing is arrangement where in you will be calling the APIs of that class which you want to test, so for that 1st you need create one instance of that class. Once that is done, in the Act section i have called “DepositAmount” method with amount 20000. Then in the last section i have done the assertion with the amount which i have deposited.
Now, in order to run the Unit test, what you can do is right-click the test method and say “Run Unit Tests” as shown below in the screen shot.
Now, as soon as you do the same, it will open the test window with the following results as shown below in the screen shot.
so, here in the screen shot it is clearly indicating that test is passed. Now, let’s try to fail this test by just changing the Assertion statement over here.
Now, when i try to run the test, then it will fail.
Likewise we can test various edge cases. So, with this i am wrapping this session here itself. In the next session, we’ll try to test some more practical test cases based on the class. Till then stay tuned and Happy Coding.