Unittest Framework Interview Questions & Answers

Unittest Framework Interview Questions

Are you preparing for Unittest Framework job interview? Need Some interview question and answers to clear the interview and get your desired job in first attempt? Then we the Wisdomjobs have provided you with the complete details about the Unittest Framework Interview Question and Answers on our site page. The unittest unit testing framework was initially encouraged by JUnit and has a alike flavor as major unit testing frameworks in other languages. If you are well familiar with the Unittest Framework topics then there are various leading companies that offer various job roles Unit test case Implementation, Android Automation Testing with Python, Software Developer, NET developer, and many other roles too. To know more on Unittest Framework interview question and answers and various Unittest Framework job roles visit our site Wisdomjobs Unittest Framework page.

Unittest Framework Interview Questions And Answers

Unittest Framework Interview Questions
    1. Question 1. What Is Unit Testing?

      Answer :

      Unit testing is validation and verification methodology where the developers test the individual units of source code. Some key points to be remembered from: -

      • A unit is the smallest part in the application which can be tested. So it can be either a method. Function or class.
      • These tests are conducted during development.
      • Unit test belongs to the white box testing category.

    2. Question 2. What Are The Different Ways By Which You Can Do Unit Testing With .net?

      Answer :

      There are 2 primary / accepted ways of doing unit testing:-

      • NUNIT
      • Visual studio team edition test system

    3. Question 3. Can We Start With A Simple Nunit Example?

      Answer :

      Ok so let’s say we want to do unit testing for a simple class ‘clsInvoiceCalculation’ shown below. This class calculates total cost by taking per product cost and number of products as input.

      public class clsInvoiceCalculation
          {
              public int CalculateCost(int intPerProductCost, int intNumberOfProducts)
              {
                  return intPerProductCost * intNumberOfProducts;
              }
      }

      Let’s say we want to execute the below test case on the above class using NUNIT.

    4. Question 4. How Can We Make Nunit Test Cases Data Driven?

      Answer :

      In the previous questions we had hardcoded the test data in the NUNIT test case itself. But in real time you would like to have test data inputs coming from a XML file or a database, in other words you would like to create data driven test cases. In order to create data driven in NUNIT we can attribute the unit test method with ‘TestCaseSource’ attribute as shown in the below figure. In ‘TestCaseSource’ we need to provide the function which will return the test case data, in this case it is ‘TestCases’.

      Below is the code snippet which provides dynamic data to unit test method. The function which is providing the dynamic data should return ‘IEnumerable’. NUNIT provides something called as ‘TestCaseData’ class. This class defines the test case data for NUNIT. The class provides ways by which you can specify input and the expected output from the test case. Finally to return test case by test case we need to use ‘yield’ keyword with for each. You can see the below code snippet to understand how yield works.

    5. Question 5. How Can We Do Unit Testing Using Vsts Test?

      Answer :

      In order to do unit testing using VSTS right click on the visual studio solution explorer and select add new project. In the test project right click and select add new test you should be popped with a dialog box as shown in the below figure. From the dialog box select ‘Basic Unit test’.

      In order to create a function which will execute our unit test you need to attribute the function with “[TestMethod]”. If you remember in Nunit it was ‘[Test]’. The assert function does not change.

      Once you have done the unit testing coding compile the same, click on test, run and select ‘Tests in the current context”.

      Once you do the previous test, the test starts running from pending to in progress and finally showing the results.

       

    6. Question 6. How Can We Create Data Driven Unit Test Using Vsts Test?

      Answer :

      Creating data driven unit test is pretty simple in VSTS. First create a simple table with test data values. For instance you can see in the below figure we have created a table with 3 fields i.e. 2 input fields and 1 expected value. To consume this table apply ‘DataSource’ attribute with a proper database connection string as shown in the below figure. To get field data from the table we can use the ‘DataRow’ with index.

      We have 3 test cases in the table, so when we executed the above test’s we got the below test results in our visual studio IDE result window.

    7. Question 7. How Can We Do Automated Testing Using Vsts?

      Answer :

      In order to do automate testing in VSTS we need use the ‘Web test’ template. So click on add new test and select ‘Web test’.

      Once you have selected web test browser will open with a record, pause and stop button as shown in the below figure. Once you start posting and requesting, the URL recorder starts recording every request and response as shown in the below figure. On the right you can see how the recorder has recorded all the get and posts.

      Below is a snapshot of simple login screen which is recorded. There are two requests, the first request is for the login page and the second request is when you post the login page with userid and password. You can see the values i.e. ‘Admin’ and ‘Admin’ in userid and password textboxes.

    8. Question 8. How Can We Make The Automated Test Data Driven In Vsts?

      Answer :

      • Once you have created the web test, right click on the web test and click add data source.
      • Once you have added the data source you can then specify the database fields as inputs to the text boxes.
      • We need to perform one more step to ensure that the data driven test runs fine. Right click on the testrunconfig file and select one per data row.
      • Once you are done you can run the test and see how VSTS picks up row by row test cases and executes the test. 

    9. Question 9. How Can We Do Coverage Testing Using Vsts?

      Answer :

      Code coverage is a 3 steps process as shown below. The first step is to enable code coverage. So right click on the ‘.testrunconfig’ file in the solution explorer.

      The next step is to select the assembly / DLL which we want to monitor for code coverage.

      Once you run the test, right click on the test results and select code coverage results. You will be shown a details result.

    10. Question 10. What Is Junit?

      Answer :

      Java + unit testing = Junit

      1. Junit is open source testing framework developed for unit testing java code and is now the default framework for testing Java development.
      2. It has been developed by Erich Gamma and Kent Beck.
      3. It is an application programming interface for developing test cases in java which is part of the XUnit Family.
      4. It helps the developer to write and execute repeatable automated tests.
      5. Eclipse IDE comes with both Junit and it’s plug-in for working with Junit.
      6. Junit also has been ported to various other languages like PHP, Python, C++ etc.

    11. Question 11. Who Should Use Junit, Developers Or Testers?

      Answer :

      Used by developers to implement unit tests in Java.  Junit is designed for unit testing, which is really a coding process, not a testing process. But many testers or QA engineers are also required to use Junit for unit testing.

    12. Question 12. Why Do You Use Junit To Test Your Code?

      Answer :

      Junit provides a framework to achieve all the following-:

      1. Test early and often automated testing.
      2. Junit tests can be integrated with the build so regression testing can be done at unit level.
      3. Test Code reusage.
      4. Also when there is a transfer Junit tests act as a document for the unit tests.

    13. Question 13. What Are Unit Tests?

      Answer :

      A unit test is nothing more than the code wrapper around the application code that can be executed to view pass – fail results.

    14. Question 14. When Are Unit Tests Written In Development Cycle?

      Answer :

      Tests are written before the code during development in order to help coders write the best code. Test-driven development is the ideal way to create a bug free code. When all tests pass, you know you are done! Whenever a test fails or a bug is reported, we must  first write the necessary unit test(s) to expose the bug(s), and then fix them. This makes it almost impossible for that particular bug to resurface later. 

    15. Question 15. How To Write A Simple Junit Test Class?

      Answer :

      To write a test case, follow these steps:

      1. Define a subclass of TestCase.
      2. Override the setUp() method to initialize object(s) under test.
      3. Optionally override the tearDown() method to release object(s) under test.

      Define one or more public testXYZ() methods that exercise the object(s) under test and assert expected results.

    16. Question 16. What Is Junit Testcase?

      Answer :

      JUnit TestCase is the base class, junit.framework.TestCase, that allows you to create a test case.

      A test case defines the fixture to run multiple tests. To define a test case

      • mplement a subclass of TestCase
      • efine instance variables that store the state of the fixture
      • nitialize the fixture state by overriding setUp
      • lean-up after a test by overriding tearDown

      Each test runs in its own fixture so there can be no side effects among test runs.

    17. Question 17. What Is Junit Testsuite?

      Answer :

      JUnit TestSuite is a container class under package junit.framework.TestSuite. It allows us to group multiple test cases into a collection and run them together.

    18. Question 18. What Is Junit Test Fixture?

      Answer :

      A test fixture is a fixed state of a set of objects used as a baseline for running tests. Their purpose is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable. Examples of fixtures:

      • Loading a database with a specific, known set of data
      • Copying a specific known set of files
      • Preparation of input data and setup/creation of fake or mock objects

      If a group of tests shares the same fixtures, you should write a separate setup code to create the common test fixture. If a group of tests requires different test fixtures, you can write code inside the test method to create its own test fixture.

    19. Question 19. What Happens If A Junit Test Method Is Declared As “private”?

      Answer :

      If a Junit test method is declared as “private”, the compilation will pass ok. But the execution will fail. This is because Junit requires that all test methods must be declared as “public”.

    20. Question 20. What Happens If A Junit Test Method Is Declared To Return “string”?

      Answer :

      If a Junit test method is declared to return “String”, the compilation will pass ok. But the execution will fail. This is because Junit requires that all test methods must be declared to return “void”.

    21. Question 21. Why Not Just Use System.out.println () For Unit Testing?

      Answer :

      Debugging the code using system.out.println() will lead to manual scanning of the whole output every time the program is run to ensure the code is doing the expected operations. Moreover, in the long run, it takes lesser time to code Junit methods and test them on our files.

    22. Question 22. The Methods Get () And Set () Should Be Tested For Which Conditions?

      Answer :

      Unit tests performed on java code should be designed to target areas that might break. Since the set() and get() methods on simple data types are unlikely to break, there is no need to test them explicitly. On the other hand, set() and get() methods on complex data types are vulnerable to break. So they should be tested.

    23. Question 23. For Which Conditions, The Methods Get () And Set () Can Be Left Out For Testing?

      Answer :

      You should do this test to check if a property has already been set (in the constructor) at the point you wish to call getX(). In this case you must test the constructor, and not the getX() method. This kind of test is especially useful if you have multiple constructors.

    24. Question 24. Do You Need To Write A Test Class For Every Class That Needs To Be Tested?

      Answer :

      No. We need not write an independent test class for every class that needs to be tested. If there is a small group of tests sharing a common test fixture, you may move those tests to a new test class. If you have two groups of tests that you think you’d like to execute separately from one another, it is wise to place them in separate test classes.

    25. Question 25. How Do You Test A “protected” Method?

      Answer :

      A protected method can only be accessed within the same package where the class is defined. So, testing a protected method of a target class means we need to define your test class in the same package as the target class.

    26. Question 26. How Do You Test A “private” Method?

      Answer :

      A private method only be accessed within the same class. So there is no way to test a “private” method of a target class from any test class. A way out is that you can perform unit testing manually or can change your method from “private” to “protected”.

    27. Question 27. Do You Need To Write A Main () Method Compulsorily In A Junit Test Case Class?

      Answer :

      No. But still developers write the main() method in a JUnit test case class to call a JUnit test runner to run all tests defined in this class like:

      public static void main(String[] args) {
         junit.textui.TestRunner.run(Calculator.class);
      }

      Since you can call a JUnit runner to run a test case class as a system command, explicit main() for a Junit test case is not recommended. junit.textui.TestRunner.run() method takes the test class name as its argument. This method automatically finds all class methods whose name starts with test. Thus it will result in below mentioned findings:

      1. testCreateLogFile()
      2. testExists()
      3. testGetChildList()

      It will execute each of the 3 methods in unpredictable sequence (hence test case methods should be independent of each other) and give the result in console.

    28. Question 28. What Happens If A Test Method Throws An Exception?

      Answer :

      If you write a test method that throws an exception by itself or by the method being tested, the JUnit runner will declare that test as fail.

      The example test below is designed to let the test fail by throwing the uncaught IndexOutOfBoundsException exception:

      import org.junit.*;
      import java.util.*;
      public class UnexpectedExceptionTest2 {
      // throw any unexpected exception
      @Test public void testGet() throws Exception {
            ArrayList emptyList = new ArrayList();
            Exception anyException = null; // don't catch any exception
            Object o = emptyList.get(1); }
      }

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Unittest Framework Tutorial