Skip to content

Unit test

A focused automated check of one chosen software unit, illustrated by an isolated option-payoff test and the limits of a passing result.

A unit test is an automated check of a small, deliberately chosen piece of software against an expected behaviour. In quant work, it can catch a faulty payoff calculation before that calculation is used throughout a portfolio.
The unit might be a function, a class or a small group of collaborators. The important choice is the boundary: what behaviour is being checked, which inputs are controlled and which dependencies are left outside. A test of connected data ingestion and valuation components answers a different question, covered by integration testing.

One payoff, one check

Consider a fictional European call option at expiry on one underlying unit. With expiry price STS_T and strike KK, both in USD per underlying unit, its cash-equivalent payoff is max⁡(ST−K,0)\max(S_T-K,0). This is an expiry payoff, before any contract multiplier; it is neither a pre-expiry option price nor profit after the premium.
For ST=105S_T=105 and K=100K=100, exercising buys an underlying unit for USD100 that is worth USD105. The expected payoff is therefore USD5. That contractual reasoning supplies the test's oracle: the expected answer comes from outside the implementation being checked.
This self-contained Python example uses the standard library's unittest. The inputs are controlled test data; no market feed, database or network is needed.
import unittest def call_payoff(spot_at_expiry: int, strike: int) -> int: """Return the terminal payoff for one underlying unit.""" return max(spot_at_expiry - strike, 0) class CallPayoffTests(unittest.TestCase): def test_in_the_money(self): self.assertEqual(call_payoff(105, 100), 5) suite = unittest.defaultTestLoader.loadTestsFromTestCase(CallPayoffTests) result = unittest.TestResult() suite.run(result) print(f"tests={result.testsRun}, failures={len(result.failures)}, " f"errors={len(result.errors)}") if not result.wasSuccessful(): raise AssertionError("payoff test failed") # tests=1, failures=0, errors=0
The test arranges known inputs, calls the function and compares the result with the expected value. assertEqual reports a failure when the values differ; an unexpected exception is recorded as an error. This run checked one case successfully. The example was executed with Python 3.14.4; the linked reference is the maintained Python 3.14 documentation.
An isolated test supplies an expiry price of USD105 and strike of USD100 to call_payoff, then compares actual USD5 with expected USD5. Market feeds, database connections and portfolio wiring are outside this test.
An isolated test supplies an expiry price of USD105 and strike of USD100 to call_payoff, then compares actual USD5 with expected USD5. Market feeds, database connections and portfolio wiring are outside this test.
Fictional one-unit terminal payoff: controlled inputs, a function call and an independently chosen expected result. The shaded boundary is the chosen scope of this test.
In a real project, the test should import the implementation used by the application. Keeping the function beside its test makes this demonstration easy to run, but testing a copied function does not protect a different production implementation from changes.

Decide what a pass means

This test uses Python integers and checks an exact contractual result, so exact equality is appropriate. It assumes valid nonnegative integer inputs; the compact function does not enforce that input contract.

For floating-point calculations with rounding or approximation error, specify a justified numerical tolerance. Python's math.isclose accepts relative and absolute tolerances; comparison near zero generally needs a positive absolute tolerance. Choose the band from the algorithm's error and the required financial precision. Widening it until a failing test passes can conceal a defect.
A useful payoff suite would also check the strike boundary, the out-of-the-money region and any promised rejection of invalid inputs. More broadly, a pass says that the selected implementation met the selected expectation for the selected case. It does not establish that every input works, that connected systems agree on units, or that a pricing model's assumptions fit a traded contract. Property-based testing can explore many inputs; independent model validation remains broader than software tests.
For more substantial numerical examples, Testing Numerical Code develops reference values and other oracles. The full lesson requires membership; its public preview shows the scope.

Practice: a passing bug

A developer replaces the function's return expression with spot_at_expiry - strike, dropping the floor at zero. Would the shown test detect the change? What happens for a new fictional case with expiry price USD88 and strike USD100, on the same one-unit basis?
Answer. The shown test still passes: USD105 minus USD100 is USD5 in both implementations. For the new case, the changed function returns negative USD12, whereas the contractual call payoff is USD0 because the holder can decline to exercise. An additional test expecting call_payoff(88, 100) to equal 0 would fail against that changed implementation. The first passing test supplied evidence for its chosen case, not complete correctness.

Sources

Last updated
  • software testing
  • python
  • quant software
  • numerical code
← Back to the glossaryBack to top ↑