Automated Testing Part – 2

Hi Friends,

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.

1st

Now, I’ll add NUnit Reference in here 1st from Nuget package manager as shown below.

2nd

Now, once it gets successfully installed, you can verify the same in references as well as shown below.

3rd

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

12th

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.

7th

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.

8th

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.

9th

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.

10th

Now, when i try to run the test, then it will fail.

11th

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.

Thanks,
Rahul

940 total views, 2 views today

Automated Testing Part – 1

Hi Friends,

Today, in this discussion, we’ll discuss more about testing rather we’ll delve in automated testing and understand why it is mandatory for software development process. If you are following AGILE model in software development, then you might be knowing that client always focuses on testing of existing features plus the new features which is getting developed. Writing good test cases and meaningful test cases which are covering your code end to end gives you confidence that you have written robust code which won’t fail in any circumstances. Now, let’s understand some of the strengths of the Automated Testing.

  • You can run these test cases any time
  • Very quick while running
  • You can run them again and again
  • Also it relies on logical assertions.

Now, Let’s look at the different type of Tests.

  • Unit Test:- Unit Tests are about testing your logic in small chunks rather testing your logic in isolation.
  • Integration Test:- Means testing things together.
  • User Interface Tests:- So, these tests execute via UI and testing things at user like behavior of the application when load increases likewise.

So, now without wasting time, let’s begin our discussion with Unit Testing:- So, as I said that Unit Testing is all about testing the thing in isolation. So, basically unit test focuses on the behavior of class like how it will be utilized by other classes. So, it makes one thing very clear that we’ll test only public API, we won’t test private methods. So, private methods or behaviors will be tested via public class members once it is exposed to it. So, ideally unit testing is best for testing complex and detailed behavior. Now, it also help us to catch the defect at the lowest level itself. Now, let’s check few best practices of writing Unit Tests.

  • Testing Boundary Values:- Obviously, whichever testing book you refer, they will stretch upon this point.
  • Happy Path and Negative Test cases:- Write test cases for both scenarios and test them equally.
  • Find a bug and Write test case to fix the same.
  • Write Test cases to test almost every part of your code

Now, basically Tests are divided into 3 fundamental Units. They are:-

  • Arrange:- Under this we basically create an instance of class being tested. If it requires any kind of initialization, we’ll also do it here.
  • Act:- In this stage, we will call the functionality which we are interested in testing.
  • Assert:- In this phase, we basically compare our outcomes with expected behavior, based on the outcome, we come on conclusion whether test passed or failed.

Testing Frameworks:-

Now, Testing Frameworks basically provide a platform to write your tests, organize them, then provides the ability to do assertions on the test result. So, the test code which we write will use this framework and use testing attributes and assertion methods. Now, associated to this, there is one more concept which is closely related, often designated as “Test Runners“. So, a test runner will understand the framework being used, it will locate, enumerate and run tests for us. It will also give the snapshot of these tests in different flavors.  Below i have mentioned few of the available Testing framework under Microsoft ecosystem:-

  • MSTest
  • xUnit.Net
  • NUnit

Now, below is the screen shot for the same which is having the reference for testing framework in my solution. Now, here i will be using Microsoft Testing Framework for running my tests.

1st

I’m wrapping this session here itself. In the next session we’ll start doing Demo with test classes and Test Methods. So, till then stay tuned and happy coding.

Thanks,
Rahul
Happy Coding

977 total views, no views today