At its most basic, a unit test is just a short program that runs a unit of code under different conditions and compares its output against expected results.
Consider the following calculation class:
import randomclass MyCalc: def __init__(self, a, b): self.a = a self.b = b def add(self): return self.a + self.b def mod_divide(self): if self.b == 0: raise ValueError("Cannot divide by zero") return (int(self.a / self.b), self.a % self.b) def rand_between(self): return ((random.random() * abs(self.a - self.b)) + min(self.a, self.b))
This class is initialized with two numbers and can then perform a variety of arithmetic methods on them.
Let's create a naive test for this function as follows:
from mycalc import MyCalcmc1 = MyCalc(1, ...