Today's Offer - QA Certification Training - Enroll at Flat 10% Off.

- QA Testing Blogs -

Unit Testing Interview Questions and Answers

Unit testing is considered as a vital phase in software testing. It is not easy to write unit test cases for everyone, but it demands more practice, knowledge, and techniques. To know everything about Unit testing and tips for writing robust test cases, you should join the Unit Testing Certification program at JanBask Training. Once you are done with the training, go through this Unit Testing Interview Questions blog and evaluate your current skills level. Let us start with basic questions first then we will discuss questions for the advanced workforce too.

Unit Testing Interview Questions

QA curriculum

Unit Testing Interview Questions for Freshers & Intermediates

Q1). 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.

Q2). 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.

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

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

Q4). 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.

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

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

Q6). 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.

Q7). Tell me about 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.

Q8). 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.

Q9). 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.

Q10). How is unit testing different from Integration Testing?

Users are generally confused between the unit testing and integration testing. Here is a quick comparison between the two for your reference.

  • Unit Testing checks each module closely, either it is working perfectly or not. At the same time, integration testing works on tested units or modules. When tested units are combined together, testers have to check their performance in the real-time.
  • Unit Testing works on simple scenarios, and its scope is narrower. On the other hand, integration testing is suitable for complex scenarios.
  • Unit testing checks on the functionality of each module while integration testing will check how the system works as a whole. When both types of testing are used together, it makes a developer more confident in the software app.

Q11). For QA, what is the right time to start with Unit Testing?

Starting testing at the last phase is not effective but it should be performed day by day. Mostly, Unit testing starts at the development phase continues until the deployment. When testing is not performed from the very first stage, it saves your time, efforts, and investments too.

Q12). Write the code for testing exceptions when using Array List.

Here is the sample code for your reference.


@Test (expected = IndexOutOfBoundsException.class)
Public Void outOfBounds () {
New ArrayList<object>().get(1);
}

Q13). Write the code to check the testing timeout.

Here is the sample code for your reference.


@Test (timeout=100)
Public Void infinity() {
While (true);
}

Q14). What is the purpose of Unit testing for a software app?

It acts like documentation where the functionality of each individual component is recorded. Also, you can track quickly, what to test, and when.

Read: How To Become A Quality Assurance Tester?

Q15). What can be avoided using Unit test cases?

When you are working with unit test cases, it helps to avoid long classes, functions, procedures, etc. There is no need to write lengthy code but focus on testing functionality of each small component step by step. It will make the development of large apps easy.

QA Quiz

Unit Testing Interview Questions for Advanced Workforce

Q16). What is Faking?

It is a class that is good for implementing interfaces without any logic. It just returns good or bad based on the implementation findings.

Q17). What is Mocking?

It is a class that is suitable for exceptional handling, and it will give you a detailed idea of when a particular method was called. In case a method was not called by this class then you will be notified for the same.

Q18). What is Stubbing?

Stubs can set dynamic values when exceptions are thrown by methods. It works similar to mock classes but does not give any idea of either methods were called or not.

Q19). Highlight any two or three features of mocking.

It helps in working on interactions how different modules are connected to each other. Also, it tests the particular block of the code in isolation.

Q20). How to compute the cyclomatic complexity of the code for unit test cases?

You have to check a total number of decision points within the code to compute the cyclomatic complexity. In case, the value is higher, achieving the code coverage can be tough. So, try to keep the number of decision points as minimum as possible.

Q21). What makes it tough achieving the higher code coverage?

It is more decision points, big methods, and multiple conditional loops.

Q22). How to design a good unit test case? Share your past experiences or strategies you have used during your work.

  • A test case is easy to code, and developers or testers don’t have to put more time or efforts.
  • It is easy to read, more reliable, and can be executed much faster than your expectations.
  • It can interact with other test cases quickly and creates a suitable testing environment too.
  • Unit test cases have to satisfy certain conditions like it will not access network resources, any database, or file systems. It is completely free of external factors.

Q23). What are the best practices to perform Unit Testing?

Here are the steps that you should follow while performing the Unit Testing.

  • A developer will write or design test cases at the first stage that will help to check the functionality of each module independently.
  • The best unit testing practice is to copy and paste the code in the testing environment instead of using the natural environment.
  • You can use a unit test framework like Junit and TestNG for automating the testing process. These frameworks will help you verify either all test cases are written well or not. It will speed up the testing process to a larger extent.
  • So, the testing process can be divided into three categories broadly, designing test cases, reviewing, and executing test cases.

Q24). What is Code Coverage?

It will give you a complete idea of which extent an application has been tested. It will highlight the area of the code that has not been entertained by test cases yet. You can quickly take actions on the highlighted area and make your application more suitable for the deployment.

Q25). What Are the various code coverage techniques in software testing?

The code coverage techniques are given below for testing any software app.

  • Statement Coverage
  • Decision Coverage
  • Branch Coverage
  • Condition Coverage
  • Finite State Machine Coveragea

Q26). What are the different unit testing techniques in QA?

They can be given as:

  • White Box Testing
  • Black Box Testing
  • Grey Box Testing

Q27). Tell me about the Statement Coverage? What is the formula to compute the statement coverage percentage?

It will study all statements in the code and recommends the necessary actions that can be taken as per the requirement. You can use the given formula for the statement coverage in Unit Testing.

Read: List of Top 12 Software Performance Testing Tools to Help You The Most!

Statement coverage = (number of executed statements / total number of statements) x 100

Q28). Write the sample code to check the statement coverage for the specific test scenario.

It can be given as:


Prints (int a, int b) {   -------- Printsum is a function
int result =a+b;
if (result> 0)
Print ("positive",result)
Else
Print ("Negative", Result)
}    ----------- End of the Source Code

Q29). Tell me about the Decision Coverage.

There are Boolean expressions within a software app. It will report the True or False result for a Boolean Expression. It is tough to implement, so developers have to extra careful when working on the decision coverage. You can use the given formula for the Decision coverage in Unit Testing.


Decision Coverage = Number of decision  outcomes exercised / Total Number of Decision Outcomes

Q30). Tell me about the Branch Coverage.

It will help you in understanding the final output from each module. For example, if it is binary output, then it will check for the same. It will help you to check either all modules are tested once or not. You can use the given formula for the Branch coverage in Unit Testing.


Branch Coverage = Number of Executed Branches / Total Number of Branches

Q31). Tell me about the conditional coverage? Have you used the same?

As the name suggests, it is suitable for conditional statements. It will check how expressions are evaluated for any conditional statement. It does not give a guaranteed output still useful in different test scenarios. You can use the given formula for the Conditional coverage in Unit Testing.


Condition Coverage = Number of Executed Operands / Total Number of Operands

Q32). Can you tell me a few testing tools that you have used for the unit testing in the past?

Well, various tools can be used for the testing purpose. Here are a few popular ones that I have used in the past.

Can you tell me a few testing tools that you have used for the unit testing in the past?

Q33). What are the common myths associated with Unit Testing? Can you justify your answer?

unit testing interview question

These are common myths associated with the Unit Testing that there is no need to perform the testing and it is time-consuming too. But the truth is unit testing is a mandatory approach that makes things easier for developers and smoothens other testing approaches too.

Q34). List a few advantages that you have experienced with the Unit Testing.

Here is a list of benefits I did notice when I was working on the last testing project.

  • It is easy to test or maintain the code with unit testing.
  • The code can be reused multiple time with unit test cases.
  • Software is more reliable and ensures the best performance in the long run.
  • It is easy to debug the code in less time and budget.
  • Anyone can understand the functionality of each small individual component by checking the detailed documentation. 

Q35). Why did unit testing need to perform with other testing types?

It is not expected to highlight each error in the software program. Also, it cannot work on integration errors but check independent units. This is the reason why unit testing needs to combine with other testing types like integration or performance testing.

Q36). What are the best tactics you want to suggest to perform Unit Testing? 

  • Check that each unit is independent or not. If it is independent, only then it should be tested.
  • Try to keep naming conventions as simple as possible so that they can be understood by anyone.
  • As soon as you check a specific unit or module, fix the error immediately.
  • You should write a different test case for each independent unit to be tested.

QA Demo Class

Final Words:

The blog lists all important questions that may be asked by you when you appear for a Unit test Interview. Check them thoroughly and try to find answers on your own. You may be asked to write unit test cases at a higher level, so be prepared for that. If you don’t know how to write test cases practically then join the Unit Testing certification program at JanBask Training now and expand your knowledge base right away. Our experts will give you a depth idea of basic concepts, type of testing, testing methodologies at the basic level. Moving ahead, you will learn writing test cases and executing test scripts at the advanced level. So, enroll now and get certified!

Read: What is SDLC Phases? Software Development Life Cycle Models

    Janbask Training

    JanBask Training is a leading Global Online Training Provider through Live Sessions. The Live classes provide a blended approach of hands on experience along with theoretical knowledge which is driven by certified professionals.


Trending Courses

AWS

  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

8 days 14 Dec 2019

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

9 days 15 Dec 2019

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

9 days 15 Dec 2019

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

-0 day 06 Dec 2019

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

14 days 20 Dec 2019

Course for testing

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

18 days 24 Dec 2019

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

3 days 09 Dec 2019

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

-0 day 06 Dec 2019

SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

3 days 09 Dec 2019

Comments

Search Posts

Reset

Receive Latest Materials and Offers on QA Testing Course

Interviews