
A unit test is a small piece of code that checks one unit of your program, usually a function or a class, in isolation. You run it automatically, and it tells you in seconds whether that unit still does what it should. Useful tests do more than pass: they catch real bugs, explain how the code should behave and give you the confidence to improve it.
Automated testing is as old as programming, but the modern style comes from Kent Beck. In 1994 he described SUnit, a small testing framework for Smalltalk, and in 1997 he and Erich Gamma wrote JUnit for Java. Its design spread to almost every language under the name xUnit. In the early 2000s Beck popularized test-driven development, where you write the test before the code. Popular tools today include pytest for Python, JUnit 5 for Java and Jest or Vitest for JavaScript.
Structure each test in three parts. Arrange prepares the inputs and objects, Act calls the code under test once, and Assert checks the result. A test of a discount function in pytest might look like this:
def test_ten_percent_discount_reduces_price():
cart = Cart(total=200)
cart.apply_discount(10)
assert cart.total == 180
Give tests descriptive names that state the expected behavior. When test_ten_percent_discount_reduces_price fails, the report already tells you what broke. Names like test1 or test_cart tell you nothing.
A test should talk to your code through its public interface, the way real callers do. If you check private helper functions or the exact sequence of internal calls, every refactoring breaks the tests even when the program still works correctly. Ask yourself: if I rewrite this function in a cleaner way and the output stays the same, should the test fail? If the answer is no, the test is checking the right thing. This also pushes you toward clean and readable code, because code that is hard to test is usually doing too much.
Bugs rarely hide in the most obvious input. For each unit, cover several kinds of cases:
Databases, payment services, the clock and the file system make tests slow and unpredictable, so replace them with test doubles. A stub returns fixed answers, a fake is a simple working version such as an in-memory database, and a mock records calls so you can check that they happened. Use them only at the edges of your system. If a test needs ten mocks to run, it is checking your wiring rather than your logic, and the code probably needs a simpler design.
Test-driven development, or TDD, turns testing into a short design loop:
Seeing the test fail first proves it can detect a problem. You do not have to use TDD for everything, but it works especially well for logic with clear rules, such as calculations, parsing and validation.
Run your tests before every commit, and let a continuous integration service run them automatically for every pull request, so broken code never reaches the main branch. When a test fails, read the message carefully: a good test points you straight to the problem and makes it much easier to debug your code. Treat test code like production code: keep it tidy and delete tests that no longer make sense. A small, trusted suite that runs in seconds is worth far more than a huge one that nobody runs.
Grade 10 Math: Functions and Reasoning - Quiz
1170 questions