unit test interview questions

Part 1 – Unit testing interview questions (Basic)
  • Q1. What is smoking? …
  • Q2. What is Stubbing? …
  • Q3. What is Code Coverage? …
  • Q4. Tell some brief about Unit testing? …
  • Q5. How to check timeout testing, write some code? …
  • Q6. What is Branch Coverage? …
  • Q7. What is the Advantage of unit testing? …
  • Q8. Who can perform Unit Testing?

What Should You Unit Test? – C# Gotcha! Interview Questions

Properly setup the actions that apply to all the tests involved


The setup code should apply to all the tests:


Consider keeping the setup code minimal to preserve readability and maintainability.

Unit Testing Interview Questions for Freshers & Intermediates

Let us now try and answer unit testing interview questions here:

Ans:-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.

Ans:-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.


  • State-based Unit Testing
  • Interaction-based Unit Testing
  • Ans:-If you want to check if the final output is right or not, then it becomes state-based.

    Ans:-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.

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

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

    Ans:-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.

    Ans:-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.

    Ans:-Users are generally confused between 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 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.
  • Ans:-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 and continues until the deployment. When testing is not performed from the very first stage, it saves your time, efforts, and investments too.

    Ans:-Here is the sample code for your reference.

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

    Ans:-Here is the sample code for your reference.

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

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

    Ans:-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.

    Consider using factory functions in the tests

    Factories can:

  • help reduce the setup code, especially if you use dependency injection
  • make each test more readable, since the creation is a single function call that can be in the test itself instead of the setup
  • provide flexibility when creating new instances (setting an initial state, for example)
  • Theres a trade-off to find here between applying the DRY principle and readability.



    Factories are particularly useful when dealing with the DOM:



    The API documentation of the testing framework/library should be your bedside book!

    Having a good knowledge of the API can help you in reducing the size/complexity of your test code and, in general, help you during development. A simple example:



    The handy fit function used in the example above allows you to execute only one test without having to comment out all the tests below. fdescribe does the same for test suites. This could help save a lot of time when developing.

    More information on the Jasmine website.


    How do you write a unit test in a coding interview?

    Good unit tests should be reproducible and independent from external factors such as the environment or running order. Fast. Developers write unit tests so they can repeatedly run them and check that no bugs have been introduced.

    How do you prepare for a test interview?

    How to prepare for a software testing interview
    1. Be the authority on your work experiences.
    2. Write up a project sheet.
    3. Add Retrospective notes.
    4. Practise being interviewed.
    5. The interview itself.
    6. Do a Retrospective on the interview.
    7. Whatever the outcome, an interview is a win-win situation.
    8. Summary of this approach.

    Related Posts

    Leave a Reply

    Your email address will not be published.