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 Terms
Explore related glossary terms to deepen your understanding.
Related Resources
Dive deeper with these related interview prep pages.
Free QA career tools, no account needed
Instant and private, everything runs in your browser. Try them before you sign up.
QA Resume Checker
Instant 0-100 score on automation keywords, impact, and ATS formatting.
QA Cover Letter Generator
A tailored 3-paragraph QA cover letter from your resume and a job post.
QA Application Tracker
Drag-and-drop kanban to track every QA application from Applied to Offer.
QA Take-Home Test Generator
A realistic take-home assignment with a scenario, tasks, and a rubric.
QA LinkedIn Headline Generator
A recruiter-searchable headline, About section, and skills list.
QA STAR Story Builder
Structure a QA behavioral answer with the STAR method and instant checks.
QA Bug Report Generator
Build a clean, reproducible bug report for Markdown, Jira, or plain text.
Boundary Value Analysis Generator
Generate boundary value and equivalence partitioning test cases from a range.
QA Metrics Calculator
Calculate DRE, defect leakage, defect density, and pass rate with interpretation.
QA Test Plan Generator
Build a structured test plan (scope, approach, criteria, risks) in Markdown.
QA Salary Calculator
Estimate QA, SDET, and automation tester pay by level, market, and skills.
QA Offer Evaluator
See total comp, a counter range, and a ready-to-send negotiation message.
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 interviewCurious how it works first? See the QA mock interview