Top 50 Unit testing interview questions and answers

Posted by

Unit testing interview questions and answers

Table of Contents

1) What is smoking?

In unit testing, we use mocking. An object you want to test may have other complex object dependencies. You replace the other objects with mocks simulating the behavior of the real objects to isolate the conduct of an object that you wish to test. So mocking creates objects in simple words that simulate the behavior of real objects.

2) What is Stubbing?

A stub is a small program routine that replaces a longer program, perhaps loaded later or remotely located. For example, a Remote Procedure Call (RPC) program is compiled with stubs that replace the requested program. The stub accepts the request and then transfers it to the remote procedure (through another program). Once this procedure is done, the results or other status are returned to the bottom line, which transfers it to the application program.

3) What is Code Coverage?

Code coverage is a metric that determines the number of lines of code validated successfully by a testing process, which helps to analyze how software is verified in depth. Of course, the ultimate aim of any software company is the development of software products for businesses. But, in order to achieve that goal, companies must ensure that all the essential quality features of the software they develop are accurate, maintainable, effective, trustworthy, and safe.

4) Tell some brief about Unit testing?

A unit test is a way to test a unit, the smallest code in a system that can logically be isolated. This is a function, a subroutine, a procedure, or a property in most programming languages. The single part of the definition is significant. Author Michael Feathers says in his book “Working Effectively with Legacy Code” that tests are not unit tests when relying on external systems: “If you talk to the database, you talk through the network, touch the file system, you need a system configuration, or you cannot run it simultaneously with any other test.”

5) How to check timeout testing, write some code?

Here is the code from which we can check the timeout testing

@Test (timeout=10)
Public void infinity(){

6) What is Branch Coverage?

Branch coverage is a test method to ensure that all access codes can be executed at least once each of the potential branches is executed from each decision point. In other words, every branch is right and wrong. In addition, it helps validate all the branches in the code to ensure that no branch results in abnormal application behavior.

7) What is the Advantage of unit testing?

There are many advantages of unit testing; some are UT simplifies the debugging process. UT forces better code and design, whether you use C#, Python, Java, JavaScript, or PHP. It means that you have a well-defined and cohesive code. Using unit testing and good unit testing tools, the total cost of a project is reduced. Early detection of bugs means fewer late changes and easier problems than if they were made later.

8) Who can perform Unit Testing?

The developers usually perform unit testing during the development phase. At the same time, unit testing is usually done by automation engineers and QA experts if developers are occupied with other development tasks.

9) How can Unit test cases be avoided?

It can help avoid long classes, procedures, functions, etc., when you are working with unit test cases. No long code is necessary, but the functionality of each small component needs to be tested step by step. It makes it easy to develop big apps.

10) List some various coverage code techniques

Here is the list of coverage code technique

  • Condition coverage
  • Statement coverage
  • Decision coverage
  • Branch Coverage

11) What does the term Refactoring mean?

This technique is used if you want to revamp any existing code. It is usually done in small steps in which the code alteration is not the functionality or logic. It also helps to correct bugs.

12) Tell me about Unit Testing in brief.

Unit Testing is used to check the independent modules of a software app during the development phase. An independent module can be anything like procedure, function, etc. Unit testing is done by developers and testers together before the integration testing. They have to write unit test cases as well if needed.

13) What is the total number of phases in a Unit Test Case?

The working of a unit test case can be divided into 3 phases. At the first stage, it will initialize the specific module of a software app that you want to test. In the second stage, it will execute the test case. In the end, it will analyze the final output.

14) What are the various types of Unit Testing for a software app?

  • State-based Unit Testing
  • Interaction-based Unit Testing

15) What do you know about state-based Unit Testing?

If you want to check if the final output is right or not, then it becomes state-based.

16) Tell me about interaction-based unit testing in brief.

If you want to check the behavior of functions or procedures, whether they are invoked in the right way or not then it is interaction-based.

17) Have you ever used or worked on Unit test frameworks? If yes, name them.

Yes, I do have practical knowledge of unit test frameworks, Junit, and TestNG.

18) Tell me about the Junit testing framework.

Several test cases need to be executed repeatedly. If you need test cases for repeated execution then the Junit framework can help you.

19) Who can perform Unit Testing?

Unit Testing is generally done at the development phase so that it can be performed by developers. At the same time, if developers are occupied with other development tasks then unit testing is generally performed by automation engineers and QA experts.

20) What do you know about the term Refactoring?

If you want to revamp any existing code, then this technique is used. It is generally done in small steps where only the code is changed, not the functionality or the logic. It helps in bug fixing too.

21) What Is Unit Testing?

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.

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

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

Visual studio team edition test system

23) Can We Start With A Simple Nunit Example?

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.

24) How Can We Make Nunit Test Cases Data Driven?

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.

25) How Can We Do Unit Testing Using Vsts Test?

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.

26) How Can We Create Data Driven Unit Test Using Vsts Test?

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.

27) How Can We Do Automated Testing Using Vsts?

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.

28) How Can We Make The Automated Test Data Driven In Vsts?

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.

29) How Can We Do Coverage Testing Using Vsts?

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.

30) What Are Unit Tests?

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

31) What is Exploratory Testing?

Exploratory testing is a hands-on approach in which testers are involved in minimum planning and maximum test execution. The planning involves the creation of a test charter, a short declaration of the scope of a short (1 to 2 hour) time-boxed test effort, the objectives and possible approaches to be used. The test design and test execution activities are performed in parallel typically without formally documenting the test conditions, test cases or test scripts. This does not mean that other, more formal testing techniques will not be used. For example, the tester may decide to use boundary value analysis but will think through and test the most important boundary values without necessarily writing them down. Some notes will be written during the exploratory-testing session so that a report can be produced afterward.

32) What is “use case testing”?

In order to identify and execute the functional requirement of an application from start to finish “use case” is used and the techniques used to do this is known as “Use Case Testing.”

33) What is the difference between the STLC (Software Testing Life Cycle) and SDLC (Software Development Life Cycle)?

SDLC deals with development/coding of the software while STLC deales with validation and verification of the software

34) What is traceability matrix?

The relationship between test cases and requirements is shown with the help of a document. This document is known as a traceability matrix.

35) What is Equivalence partitioning testing?

Equivalence partitioning testing is a software testing technique which divides the application input test data into each partition at least once of equivalent data from which test cases can be derived. By this testing method, it reduces the time required for software testing.

36) What is white box testing and list the types of white box testing?

White box testing technique involves selection of test cases based on an analysis of the internal structure (Code coverage, branches coverage, paths coverage, condition coverage, etc.) of a component or system. It is also known as Code-Based testing or Structural testing. Different types of white box testing are

  • Statement Coverage
  • Decision Coverage

37) In white box testing, what do you verify?

In white box testing following steps are verified.

  • Verify the security holes in the code
  • Verify the incomplete or broken paths in the code
  • Verify the flow of structure according to the document specification
  • Verify the expected outputs
  • Verify all conditional loops in the code to check the complete functionality of the application
  • Verify the line by line coding and cover 100% testing

38) What is black box testing? What are the different black box testing techniques?

Black box testing is the software testing method which is used to test the software without knowing the internal structure of code or program. This testing is usually done to check the functionality of an application. The different black box testing techniques are

  • Equivalence Partitioning
  • Boundary value analysis
  • Cause-effect graphing

39) What is the difference between static and dynamic testing?

Static testing: During Static testing method, the code is not executed, and it is performed using the software documentation.

Dynamic testing: To perform this testing the code is required to be in an executable form.

40) What are verification and validation?

Verification is a process of evaluating software at the development phase. It helps you to decide whether the product of a given application satisfies the specified requirements. Validation is the process of evaluating software at the after the development process and to check whether it meets the customer requirements.

41) What are the different test levels?

There are four test levels

  • Unit/component/program/module testing
  • Integration testing
  • System testing
  • Acceptance testing

42) What is Integration testing?

Integration testing is a level of software testing process, where individual units of an application are combined and tested. It is usually performed after unit and functional testing.

43) What Test Plans consists of?

Test design, scope, test strategies, approach are various details that Test plan document consists of.

  • Test case identifier
  • Scope
  • Features to be tested
  • Features not to be tested
  • Test strategy & Test approach
  • Test deliverables
  • Responsibilities
  • Staffing and training
  • Risk and Contingencies

44) What is the difference between UAT (User Acceptance Testing) and System testing?

System Testing: System testing is finding defects when the system undergoes testing as a whole; it is also known as end-to-end testing. In such type of testing, the application suffers from beginning till the end.

UAT: User Acceptance Testing (UAT) involves running a product through a series of specific tests which determines whether the product will meet the needs of its users.

45) Mention the difference between Data Driven Testing and Retesting?

Retesting: It is a process of checking bugs that are actioned by the development team to verify that they are fixed.

Data Driven Testing (DDT): In data driven testing process, the application is tested with multiple test data. The application is tested with a different set of values.

46) What are the valuable steps to resolve issues while testing?

Record: Log and handle any problems which have happened
Report: Report the issues to higher level manager
Control: Define the issue management process

47) What is the difference between test scenarios, test cases, and test script?

Difference between test scenarios and test cases is that

Test Scenarios: A Test Scenario is any functionality that can be tested. It is also called Test Condition or Test Possibility.

Test Cases: It is a document that contains the steps that have to be executed; it has been planned earlier.

Test Script: It is written in a programming language and it’s a short program used to test part of the functionality of the software system. In other words a written set of steps that should be performed manually.

48) What is Latent defect?

Latent defect: This defect is an existing defect in the system which does not cause any failure as the exact set of conditions has never been met

49) What are the two parameters which can be useful to know the quality of test execution?

To know the quality of test execution, we can use two parameters

  • Defect reject ratio
  • Defect leakage ratio

50) What is the function of the software testing tool “phantom”?

Phantom is a freeware and is used for windows GUI automation scripting language. It allows us to take control of windows and functions automatically. It can simulate any combination of keystrokes and mouse clicks as well as menus, lists and more.

Related video:

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x