Unit Testing Questions with Answers

17 practice questions on Unit Testing (Software Testing & QA) with answers and step-by-step solutions. Try 10 samples below, then practise all 17 free in the AptiNest app.

Sample questions

Question 1Easy

In unit testing, an assertion is:

  1. A statement that checks actual output against the expected value
  2. A comment describing the test on every build
  3. A build configuration
  4. A database query

Answer: A. A statement that checks actual output against the expected value

Explanation

A failed assertion makes the test fail.

Step-by-step solution

  1. An assertion is a check inside a test.
  2. It compares the actual output with the expected value.
  3. If they differ, the assertion fails and the test fails.
  4. So the answer is option A.

Why the other options are wrong

  • B. A comment describes the test; it does not check anything.
  • C. A build configuration is set up in the build tool, not in assertions.
  • D. A database query fetches data; it is not an assertion.

Tip: An assertion is the line that decides pass or fail: actual value against expected value.

Question 2Easy

Which JUnit annotation marks a method as a test (JUnit 5)?

  1. @Run
  2. @Check
  3. @Test
  4. @Assert

Answer: C. @Test

Explanation

@Test from org.junit.jupiter.api marks a test method.

Step-by-step solution

  1. JUnit uses @Test.
  2. The others don't exist.
  3. So @Test.

Why the other options are wrong

  • A. Not a JUnit annotation.
  • B. Not a JUnit annotation.
  • D. Assertions are methods, not annotations.

Question 3Easy

Code coverage tools measure:

  1. How many bugs the code is known to have
  2. How fast the tests run on the server
  3. How many users run the code in production
  4. How much code the tests execute

Answer: D. How much code the tests execute

Explanation

Coverage shows which lines or branches run during testing, not the quality of the assertions.

Step-by-step solution

  1. It tracks execution.
  2. Not correctness.
  3. Executed code share.

Why the other options are wrong

  • A. Coverage does not count bugs.
  • B. Speed is separate.
  • C. Usage is a production metric.

Question 4Easy

What does the following pytest test report?

def add(a, b):
    return a + b

def test_add():
    assert add(2, 3) == 6
  1. The test passes
  2. A syntax error occurs
  3. The test is skipped
  4. The test fails because 5 is not equal to 6

Answer: D. The test fails because 5 is not equal to 6

Explanation

add(2, 3) returns 5, so assert 5 == 6 raises AssertionError and pytest marks the test as failed.

Step-by-step solution

  1. add returns 5.
  2. Assertion compares to 6.
  3. Mismatch, so failure.

Why the other options are wrong

  • A. 2 + 3 gives 5, so the assertion is false.
  • B. The code is valid Python.
  • C. Functions named test_ are collected, not skipped.

Question 5Medium

What is the correct order of the TDD cycle?

  1. Red, green, refactor
  2. Green, red, refactor
  3. Refactor, red, green
  4. Red, refactor, green

Answer: A. Red, green, refactor

Explanation

First write a failing test (red), make it pass (green), then clean the code (refactor).

Step-by-step solution

  1. Failing test first.
  2. Pass it.
  3. Clean up.

Why the other options are wrong

  • B. You can't start with passing code.
  • C. Refactor comes after passing.
  • D. Refactoring before green is unsafe.

Question 6Medium

In test-driven development (TDD), the correct cycle is:

  1. Red, green, refactor
  2. Green, red, deploy
  3. Refactor, red, green
  4. Code, release, test

Answer: A. Red, green, refactor

Explanation

Write a failing test, make it pass, then clean up the code.

Step-by-step solution

  1. TDD starts by writing a test that fails, which is red.
  2. Then the code is written just enough to make the test pass, which is green.
  3. Then the code is cleaned up without changing behaviour, which is refactor.
  4. So the cycle is red, green, refactor, option A.

Why the other options are wrong

  • B. Deploying is not part of the TDD cycle, and green comes after red.
  • C. Refactoring comes last, after a failing and then a passing test.
  • D. Writing code before the test and releasing it is the opposite of TDD.

Tip: TDD: red (failing test), green (make it pass), refactor (clean up).

Question 7Medium

Which statement about a mock object is correct?

  1. It replaces a dependency and can verify its calls
  2. It stores production data permanently in a large file
  3. It is the code that is under test
  4. It is a type of load generator tool

Answer: A. It replaces a dependency and can verify its calls

Explanation

A mock stands in for a collaborator such as an email service and lets the test assert interactions.

Step-by-step solution

  1. Replaces dependency.
  2. Records calls.
  3. Verify interactions.

Why the other options are wrong

  • B. Mocks usually hold canned responses only for tests.
  • C. The code under test is the SUT.
  • D. Mocks do not generate load.

Question 8Medium

A mock object in unit testing is used to:

  1. Increase code coverage artificially in most projects
  2. Generate the UI
  3. Replace a dependency and verify interactions with it
  4. Store the test report

Answer: C. Replace a dependency and verify interactions with it

Explanation

Mocks isolate the unit and can check that calls happened.

Step-by-step solution

  1. A mock stands in for a real dependency, such as a database or an API.
  2. It lets the test control the dependency's responses.
  3. It can also check that the unit called the dependency the right way.
  4. So the answer is option C.

Why the other options are wrong

  • A. A mock does not inflate coverage on its own; it is a tool for isolation.
  • B. A mock does not generate a UI.
  • D. A test report is stored by the test runner, not by a mock.

Tip: Mocks replace dependencies so a unit can be tested alone, and can verify the calls it made.

Question 9Hard

Which code could reach 100% line coverage but still miss a defect?

  1. A test that asserts every output carefully
  2. A test that runs the code but asserts nothing
  3. A suite that has a mutation score of 100 percent
  4. A test suite that also checks all boundaries

Answer: B. A test that runs the code but asserts nothing

Explanation

Coverage only shows that code ran. Without assertions a wrong result is not noticed.

Step-by-step solution

  1. Coverage counts execution.
  2. No assertion means no check.
  3. Defect slips by.

Why the other options are wrong

  • A. Assertions would catch the defect.
  • C. A perfect mutation score means strong tests.
  • D. Boundary tests help find defects.

Question 10Hard

What does this unittest-style test do?

from unittest.mock import Mock

svc = Mock()
svc.send.return_value = True
ok = svc.send("a@b.com")
svc.send.assert_called_once_with("b@c.com")
  1. It passes without any error being raised
  2. It raises TypeError because a Mock object is not callable
  3. AssertionError: the call used a different argument
  4. It returns True and nothing else happens

Answer: C. AssertionError: the call used a different argument

Explanation

The mock recorded a call with "a@b.com". assert_called_once_with("b@c.com") compares arguments and raises AssertionError.

Step-by-step solution

  1. send called with a@b.com.
  2. Assertion expects b@c.com.
  3. They differ, so it fails.

Why the other options are wrong

  • A. The call used a@b.com but the check expects b@c.com.
  • B. Mock allows any attribute and call.
  • D. assert_called_once_with raises when the check fails.

Practise all 17 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Software Testing & QA topics · All topics