Skip to main content
Testing Fundamentals

What Is Unit Testing?

Unit testing is the practice of testing individual units of code — typically functions, methods, or classes — in isolation from their dependencies to verify they produce correct outputs for given inputs.

Free to start · 7-day trial on paid plans

In Depth

Unit tests are the fastest, cheapest, and most numerous tests in a well-structured test suite. They run in milliseconds, require no external infrastructure (no browsers, databases, or APIs), and pinpoint exactly which function is broken when they fail.

A good unit test follows the Arrange-Act-Assert pattern: set up the inputs, call the function, and verify the output. Dependencies are replaced with mocks or stubs to isolate the unit under test. For example, testing a price calculator function mocks the tax rate service rather than calling a real API.

Common unit testing frameworks include Jest and Vitest (JavaScript/TypeScript), pytest (Python), JUnit (Java), and NUnit (C#). Most frameworks include assertion libraries, mocking utilities, and code coverage reporting.

For QA engineers, unit testing knowledge is increasingly expected. Even if you don't write unit tests daily, understanding how they work helps you design better integration and E2E tests — and communicate more effectively with developers about coverage gaps.

Why Interviewers Ask About This

Unit testing is fundamental to the test pyramid. Interviewers ask about it to check whether you understand testing at every level, not just the UI. Knowing when unit tests are sufficient vs. when you need integration or E2E tests is a key skill.

Example Scenario

A shopping cart module has a calculateTotal function that applies discounts, tax, and shipping. Unit tests verify: no discount applies below minimum, percentage discount calculates correctly, tax rounds to 2 decimal places, and free shipping triggers at the right threshold. These 15 unit tests run in 200ms and catch a rounding bug before it reaches integration testing.

Interview Tip

Even if you primarily work at the E2E level, demonstrate that you understand unit testing principles. Discuss the test pyramid and explain how unit tests complement your automation work. Mention specific frameworks and the Arrange-Act-Assert pattern.

Related Resources

Dive deeper with these related interview prep pages.

Ready to Ace Your QA Interview?

Practice explaining unit testing and other key concepts with our AI interviewer.

Join 1,200+ QA engineers already practicing with AssertHired.

Start Your Free QA Interview

Free to start · 7-day trial on paid plans

Written by Aston Cook, Senior QA EngineerLast updated: March 2026