Can You Guess What Tests a Calculator Needs?
Can You Guess What Tests a Calculator Needs? Here's a challenge. Below is a complete Python calculator - 40 lines, four operations, a CLI interface. Before scrolling down, think about what tests you'd write. How many test cases do you need for full coverage? def add ( a , b ): return a + b def subtract ( a , b ): return a - b def multiply ( a , b ): return a * b def divide ( a , b ): if b == 0 : raise ValueError ( " Cannot divide by zero " ) return a / b def main (): print ( " Simple Calculator " ) print ( " Operations: +, -, *, / " ) a = float ( input ( " Enter first number: " )) op = input ( " Enter operation (+, -, *, /): " ) b = float ( input ( " Enter second number: " )) operations = { " + " : add , " - " : subtract , " * " : multiply , " / " : divide } if op not in operations : print ( f " Unknown operation: { op } " ) return result = operations [ op ]( a , b ) print ( f " { a } { op } { b } = { result } " ) Got your number? Most developers say 10-15 tests. Something like: test e
Continue reading on Dev.to Python
Opens in a new tab


