skip to content
Ivan's wiki
Site Tools
Search
Tools
Show pagesource
Backlinks
Recent Changes
Media Manager
Sitemap
>
Recent Changes
Media Manager
Sitemap
swe:unit-test
This page is read only. You can view the source, but not change it.
# Unit test **[Unit test](https://en.wikipedia.org/wiki/Unit_testing)** is a test that verifies a single function or method in isolation from its dependencies. Dependencies are replaced with fakes (stubs, mocks) that return controlled values so the test is fast and deterministic. Integration tests and end-to-end tests verify the whole system but run slowly and are brittle. Unit tests alone cannot catch bugs that only appear when components interact, for example when one function passes data in a format the downstream component does not expect. Unit tests sit at the bottom of the test pyramid: cheap to write and run, so you have many of them. They isolate the code under test, making tests fast (microseconds) and deterministic rather than flaky. Unit tests and [integration tests](/swe/test-automation) complement each other. Dependencies are injected and replaced with fakes, so the test controls all inputs and outputs. ```py # Unit test with mocked dependency class PaymentProcessor: def __init__(self, gateway): self.gateway = gateway # injected, not hard-coded def charge(self, user_id, amount): return self.gateway.authorize(user_id, amount) # Mock replaces the real gateway class MockGateway: def authorize(self, user_id, amount): return {'status': 'approved'} def test_charge_succeeds(): processor = PaymentProcessor(MockGateway()) result = processor.charge('user123', 100) assert result['status'] == 'approved' ```
Page Tools
Show pagesource
Backlinks
Back to top