how to make a calculator 1


preview

I have these tests by the end of the chapter

 1import random
 2import src.calculator
 3import unittest
 4
 5
 6def a_random_number():
 7    return random.triangular(-1000.0, 1000.0)
 8
 9
10class TestCalculator(unittest.TestCase):
11
12    random_first_number = a_random_number()
13    random_second_number = a_random_number()
14
15    def test_addition(self):
16        self.assertEqual(
17            src.calculator.add(
18                self.random_first_number,
19                self.random_second_number
20            ),
21            self.random_first_number+self.random_second_number
22        )
23
24    def test_subtraction(self):
25        self.assertEqual(
26            src.calculator.subtract(
27                self.random_first_number,
28                self.random_second_number
29            ),
30            self.random_first_number-self.random_second_number
31        )
32
33    def test_multiplication(self):
34        self.assertEqual(
35            src.calculator.multiply(
36                self.random_first_number,
37                self.random_second_number
38            ),
39            self.random_first_number*self.random_second_number
40        )
41
42    def test_division(self):
43        self.assertEqual(
44            src.calculator.divide(
45                self.random_first_number,
46                self.random_second_number
47            ),
48            self.random_first_number/self.random_second_number
49        )
50
51
52# Exceptions seen
53# AssertionError
54# NameError
55# AttributeError
56# TypeError

start the project

  • I name this project calculator

  • I open a terminal

  • I use uv to make a directory for the project and initialize it

    terminal
    uv init calculator
    

    the terminal shows

    then goes back to the command line.

  • I use mkdir to make a folder named src

    mkdir src
    

    the terminal goes back to the command line.

  • I use the mv program to change the name of main.py to calculator.py and move it to the src folder

    terminal
    touch src/calculator.py
    rmdir src/calculator
    
    terminal
    New-Item src/calculator.py
    Remove-Item src/calculator
    

    the terminal goes back to the command line.

  • I make a directory for the tests

    terminal
    mkdir tests
    

    the terminal goes back to the command line.

  • I make the tests directory a Python package

    Danger

    use 2 underscores (__) before and after init for __init__.py not _init_.py

    terminal
    touch tests/__init__.py
    
    terminal
    New-Item tests/__init__.py
    

    the terminal goes back to the command line.

  • I make a Python file for the tests in the tests directory

    touch tests/test_calculator.py
    
    New-Item tests/test_calculator.py
    

    the terminal goes back to the command line.

  • I open test_calculator.py

  • I add the first failing test to test_calculator.py

    1import unittest
    2
    3
    4class TestCalculator(unittest.TestCase):
    5
    6    def test_failure(self):
    7        self.assertFalse(True)
    
  • I use echo to add pytest to requirements.txt

    terminal
    echo "pytest" > requirements.txt
    
  • I add pytest-watcher to the requirements file

    terminal
    echo "pytest-watcher" >> requirements.txt
    

    the terminal goes back to the command line.

  • I use Out-File to add pytest to requirements.txt

    terminal
    "pytest" | Out-File requirements.txt -Encoding UTF8
    
  • I add pytest-watcher to the requirements file

    terminal
    "pytest-watcher" >> requirements.txt
    
  • I use uv to install pytest-watcher with the requirements file

    terminal
    uv add --requirement requirements.txt
    

    the terminal shows that it installed pytest-watcher and its dependencies.

  • I use pytest-watcher to run the tests automatically

    terminal
    uv run pytest-watcher . --now --patterns "**/*.py"
    

    the terminal is my friend, and shows AssertionError

    terminal
    ======================== FAILURES ========================
    _____________________ TestCalculator.test_failure ________________________
    
    self = <tests.test_calculator.TestCalculator testMethod=test_failure>
    
        def test_failure(self):
    >       self.assertFalse(True)
    E       AssertionError: True is not false
    
    tests/test_calculator.py:7: AssertionError
    ================ short test summary info =================
    FAILED tests/test_calculator.py::TestCalculator::test_failure - AssertionError: True is not false
    =================== 1 failed in X.YZs ====================
    

    because True is NOT False.

    if the terminal does not show the same error, then check if

    • your tests/__init__.py has two underscores (__) before and after init for __init__.py not _init_.py

    • you ran echo "pytest-watcher" >> requirements.txt, to add pytest-watcher to the requirements file

    and try uv run pytest-watcher . --now --patterns "**/*.py" again

  • I hold ctrl (Windows/Linux) or option/command (MacOS) on the keyboard and use the mouse to click on tests/test_calculator.py:7 to put the cursor on line 7

  • I add AssertionError to the list of Exceptions seen, in test_calculator.py

  • I change True to False in the assertion

    the test passes.


  • I add a TODO list to keep track of the work for the program

     1import unittest
     2
     3
     4class TestCalculator(unittest.TestCase):
     5
     6    def test_failure(self):
     7        self.assertFalse(False)
     8
     9
    10# TODO
    11# test addition
    12# test subtraction
    13# test multiplication
    14# test division
    15
    16
    17# Exceptions seen
    18# AssertionError
    

test_addition

RED: make it fail


  • I change test_failure to test_addition then change `assertFalse`_ to `assertEqual`_

    1import unittest
    2
    3
    4class TestCalculator(unittest.TestCase):
    5
    6    def test_addition(self):
    7        self.assertEqual(src.calculator.add(0, 1), 1)
    
    • the assertEqual method from AssertionError checks if the two things in parentheses are the same. It is like the statement assert x == y or asking is x equal to y?

    • I think of

      self.assertEqual(src.calculator.add(0, 1), 1)
      

      as

      self.assertEqual(reality, my_expectation)
      

      where

      • reality is src.calculator.add(0, 1)

      • my_expectation is 1 because 0 + 1 is 1

      in other words, self.assertEqual(src.calculator.add(0, 1), 1) checks if the result of calling src.calculator.add with 0 and 1 as input is equal to 1

    the terminal is my friend, and shows NameError

    terminal
    NameError: name 'src' is not defined
    

    because I do not have a definition for src in test_calculator.py


GREEN: make it pass


  • I add NameError to the list of Exceptions seen, in test_calculator.py

  • I add an import statement at the top of the file for the calculator module

    1import src.calculator
    2import unittest
    3
    4
    5class TestCalculator(unittest.TestCase):
    

    the terminal is my friend, and shows AttributeError

    terminal
    AttributeError: module 'src.calculator' has no attribute 'add'
    

    I think of src.calculator.add as an address, add is something (an attribute) in the empty calculator.py file from the src folder

  • I add AttributeError to the list of Exceptions seen, in test_calculator.py

  • I open calculator.py from the src folder, then add the name

    1add
    

    the terminal is my friend, and shows NameError

    terminal
    NameError: name 'add' is not defined
    

    I have to tell Python what the name add means

  • I point it to None to define it

    1add = None
    

    the terminal is my friend, and shows TypeError

    terminal
    TypeError: 'NoneType' object is not callable
    

    because the add variable is now a name for None, and I cannot call None like a function.

  • I add TypeError to the list of Exceptions seen, in test_calculator.py

  • I use the def keyword in calculator.py to make add a function so it is callable

    1def add():
    2    return None
    

    the terminal is my friend, and shows TypeError

    terminal
    TypeError: add() takes 0 positional arguments but 2 were given
    

    the definition of add does not allow it take input, and 2 were given in the call - 0 and 1

  • I add names in the parentheses to make the function take 2 inputs

    1def add(first_input, second_input):
    2    return None
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: None != 1
    

    the add function returns None, the assertion expects 1

  • I make the return statement give the test what it wants

    1def add(first_input, second_input):
    2    return 1
    

    the test passes, time for a victory lap!


REFACTOR: make it better


The add function passes the test, and does not do what I want because it always returns 1. I want it to return the result of a calculation with the inputs

  • I add another assertion to show the problem with the function in test_calculator.py

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: 1 != 2
    

    the function returns 1, the assertion expects 2

  • I change the return statement in calculator.py

    1def add(first_input, second_input):
    2    return 2
    

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: 2 != 1
    

    this makes the assertion that was passing before - src.calculator.add(0, 1) fail. If the test sends

    • 0 and 2 to src.calculator.add it returns 2

    • 0 and 1 to src.calculator.add it returns 2

    I need a solution that can make the two tests pass. The function should return

    • 2 if the test sends 0 and 2 to src.calculator.add because 0 + 2 == 2

    • 1 if the test sends 0 and 1 to src.calculator.add because 0 + 1 == 1

  • I make the function return the result of adding the two inputs

    1def add(first_input, second_input):
    2    return first_input + second_input
    

    the two tests are passing

  • I add another test to make sure the function works for other numbers in test_calculator.py

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: 3 != 2
    
  • I change the expectation in the test

    the test passes.

  • I add another test with a different number for the first input

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: 4 != 3
    
  • I change the expectation to match reality

    the test passes. The add function looks good so far.

  • I add an assertion with bigger numbers

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: 912468 != 4
    
  • I change the expectation to match reality

    the test passes.

  • I add another assertion with a negative number

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: -1 != 912468
    
  • I change the expectation to match reality

    the test passes.

  • I try another assertion with two negative numbers

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: -5 != -1
    
  • I make the expectation match reality

    the test passes. The add function can handle positive and negative whole numbers

  • I add an assertion with floats (binary floating point decimal numbers)

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: 1.1 != -5
    
  • I change the expectation

    the test passes.

  • I add another assertion

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: 0.30000000000000004 != 1.1
    

    whoa!

  • I change the expectation

    the test passes. Why is the result “0.30000000000000004” and not “0.3”?

  • I add another assertion

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: -5.555499999999999 != 0.30000000000000004
    

    whaaaaat?!

  • I change the expectation

    the test passes. Why is the result “-5.555499999999999” not “-5.5555”?

  • I just did the same kind of calculation 10 times, there is a better way to do this thanks to Substitution. I can use variable a letter or a name for the numbers, so that I can have one test for all possible numbers, it is called a variable.

    A variable is a name that is used for values that change. For example, in the tests so far, I have

    src.calculator.add(0, 1) is 0 + 1 is 1
    src.calculator.add(0, 2) is 0 + 2 is 2
    src.calculator.add(0, 3) is 0 + 3 is 3
    src.calculator.add(1, 3) is 1 + 3 is 4
    src.calculator.add(123456, 789012)  is 123456 +789012 is 912468
    src.calculator.add(-2, -3) is -2 + -3 is -5
    src.calculator.add(-1, 0) is -1 + 0 is -1
    src.calculator.add(0.1, 1) is 0.1 + 1 is 1.1
    src.calculator.add(0.1, 0.2) is 0.1 + 0.2 is 0.30000000000000004
    src.calculator.add(0.1234, -5.6789) is 0.1234 + -5.6789 is -5.555499999999999
    

    all of these lines can be written using x as the name of the first number and y as the name for the second number, like this

    src.calculator.add(x, y) is x + y is x + y
    

    I add names to the first assertion

    the test is still green.

  • I do the same thing to the next assertion

    still green.

  • I do the next one

    green.

  • then the next one

    still green.

  • then the next

    the test is still green.

  • on to the next assertion

    still green.

  • and the next

    green.

  • more?

    still green.

  • I add variables to the next assertion

    the test is still green.

  • ah, the last one

    still green.


how to use random numbers

All the assertions in test_addition do the same thing

  • point first_number to a value

  • point second_number to a value

  • call src.calculator.add with first_number and second_number as input

  • check if the result of src.calculator.add(first_number, second_number) is the same as first_number + second_number

I want to use random numbers for first_number and second_number to make sure that the add function always returns the result of adding the two numbers without knowing what the numbers are.

I can do this with the random module from The Python Standard Library, it is a module from The Python Standard Library that is used to make fake random numbers

  • I add an import statement for the random module at the top of test_calculator.py

    1import random
    2import src.calculator
    3import unittest
    
  • I use a random value for first_number in the first assertion

    green.

    random.triangular returns a random float that could be any number from -0.1 to 1.0 in this case, I can also use random.randint if I want a random integer

  • I want to see the test fail to be sure everything works as expected. I change the expectation in the first assertion

    I use (ctrl/command+s (Windows & Linux/MacOS)) a few times to run the tests and the terminal is my friend, and shows AssertionError with random values that look like this

    terminal
    AssertionError: -X.YZABCDEFGHIJKLM != A.BCDEFGHIJKLMNOPQ
    

    the letters are for random numbers

  • I change the expectation of the assertion back to the right calculation

    the test is green again

  • I remove the commented line then use a random value for second_number

    the test is still green.

  • I remove the commented line and the other assertions because they are covered by the one that uses random numbers. I do not need them anymore

    still green.

  • I use the Rename Symbol feature to change the name of variables to say what they are

    green.

  • There is some repetition, If I want to use a different range of random numbers for the test, I have to make a change in more than one place. For example

    still green.

  • I add a function to remove the repetition

     1import random
     2import src.calculator
     3import unittest
     4
     5
     6def a_random_number():
     7    return random.triangular(-10.0, 10.0)
     8
     9
    10class TestCalculator(unittest.TestCase):
    
  • I use the new function to get random values for the random_first_number and random_second_number variables

    the test is still green.

  • I remove the commented lines

  • I change the expectation in the assertion to make sure the test works

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: R.STUVWXYZABCDEFG != H.IJKLMNOPQRSTUVWX
    
  • I undo the change

    the test is green again

  • I now only need to change the range of random numbers for the test in one place

    the test is still green.

  • I can use any range of numbers the computer can handle, for example

    the terminal is my friend, and shows OverflowError

    terminal
    OverflowError: (34, 'Numerical result out of range')
    

    because the numbers are too big

    • ** is the symbol for raise to the power (exponents)

    • 10.0**100000 is how to write 10.0 raised to the power of 100,000

    I make the range smaller

    the test is still green, though the test takes a little longer to run

  • I remove test addition from the TODO list

I can use a variable to remove repetition


test_subtraction

RED: make it fail


I add a test for subtraction in test_calculator.py

12    def test_addition(self):
13        random_first_number = a_random_number()
14        random_second_number = a_random_number()
15
16        self.assertEqual(
17            src.calculator.add(
18                random_first_number,
19                random_second_number
20            ),
21            random_first_number+random_second_number
22        )
23
24    def test_subtraction(self):
25        random_first_number = a_random_number()
26        random_second_number = a_random_number()
27
28        self.assertEqual(
29            src.calculator.subtract(
30                random_first_number,
31                random_second_number
32            ),
33            random_first_number-random_second_number
34        )
35
36
37# TODO

the terminal is my friend, and shows AttributeError

terminal
AttributeError: module 'src.calculator' has no attribute 'subtract'

calculator.py in the src folder does not have anything named subtract in it


GREEN: make it pass


  • I add the name to calculator.py

    1def add(first_input, second_input):
    2    return first_input + second_input
    3
    4
    5subtract
    

    the terminal is my friend, and shows NameError

    terminal
    NameError: name 'subtract' is not defined
    
  • I point subtract to None

    the terminal is my friend, and shows TypeError

    terminal
    TypeError: 'NoneType' object is not callable
    

    I have seen this before

  • I change subtract to a function to make it callable

    the terminal is my friend, and shows TypeError

    terminal
    TypeError: subtract() takes 0 positional arguments but 2 were given
    
  • I make subtract take inputs

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: None != XYZ.ABCDEFGHIJKLMNOP
    

    subtract returns None, the assertion expects

    • random_first_number-random_second_number which is

    • first_input-second_input

    • the difference between the two numbers

  • I make the subtract function return the difference between the inputs

    the test passes. SUCCESS!

  • I remove test subtraction from the TODO list in test_calculator.py


test_multiplication

RED: make it fail


I add a failing test for multiplication in test_calculator.py

24      def test_subtraction(self):
25          random_first_number = a_random_number()
26          random_second_number = a_random_number()
27
28          self.assertEqual(
29              src.calculator.subtract(
30                  random_first_number,
31                  random_second_number
32              ),
33              random_first_number-random_second_number
34          )
35
36      def test_multiplication(self):
37          random_first_number = a_random_number()
38          random_second_number = a_random_number()
39
40          self.assertEqual(
41              src.calculator.multiply(
42                  random_first_number,
43                  random_second_number
44              ),
45              random_first_number*random_second_number
46          )
47
48
49  # TODO

the terminal is my friend, and shows AttributeError

terminal
AttributeError: module 'src.calculator' has no attribute 'multiply'

GREEN: make it pass


  • Using what I know so far, I add a function to calculator.py

    the test passes.

  • I remove test_multiplication from the TODO list in test_calculator.py

Note

  • * is the symbol for multiplication

  • ** is the symbol for raise to the power (exponent)


test_division

RED: make it fail


Time for division. I add a new test to test_calculator.py

36    def test_multiplication(self):
37        random_first_number = a_random_number()
38        random_second_number = a_random_number()
39
40        self.assertEqual(
41            src.calculator.multiply(
42                random_first_number,
43                random_second_number
44            ),
45            random_first_number*random_second_number
46        )
47
48    def test_division(self):
49        random_first_number = a_random_number()
50        random_second_number = a_random_number()
51
52        self.assertEqual(
53            src.calculator.divide(
54                random_first_number,
55                random_second_number
56            ),
57            random_first_number/random_second_number
58        )
59
60
61# TODO
62# test division

the terminal is my friend, and shows AttributeError

terminal
AttributeError: module 'src.calculator' has no attribute 'divide'

GREEN: make it pass


  • I add the function to calculator.py

    the test passes.

  • I remove the TODO list from test_calculator.py


REFACTOR: make it better





All the tests are passing, though they all look the same, there has to be a better way.

The random_first_number and random_second_number variables are made once as object attributes and used later in each test with self.random_first_number and self.random_second_number, the same way I use unittest.TestCase assert methods like assertEqual method with self.assertEqual


test_calculator_tests

Since everything is green, I can write the program that makes the tests pass without looking at the tests


RED: make it fail


  • I close test_calculator.py

  • I delete all the text in calculator.py, the terminal is my friend, and shows 4 failures

    
    
    caption:

    terminal

    FAILED … - AttributeError: module ‘src.calculator’ has no attribute ‘add’ FAILED … - AttributeError: module ‘src.calculator’ has no attribute ‘divide’ FAILED … - AttributeError: module ‘src.calculator’ has no attribute ‘multiply’ FAILED … - AttributeError: module ‘src.calculator’ has no attribute ‘subtract’ =========================== 4 failed in X.YZs ============================

  • I start with the last AttributeError

    terminal
    AttributeError: module 'src.calculator' has no attribute 'subtract'
    

What other Exceptions do you think are raised as I go along?


GREEN: make it pass


  • I add the name to calculator.py

    1subtract
    

    the terminal is my friend, and shows NameError

    terminal
    NameError: name 'subtract' is not defined
    
  • I point it to None to define it

    1subtract = None
    

    the terminal is my friend, and shows TypeError

    terminal
    TypeError: 'NoneType' object is not callable
    
  • I change subtract to a function

    1def subtract():
    2    return None
    

    the terminal is my friend, and shows TypeError

    terminal
    TypeError: subtract() takes 0 positional arguments but 2 were given
    
  • I add positional arguments to the function

    1def subtract(first_input, second_input):
    2    return None
    

    the terminal is my friend, and shows AssertionError with random numbers

    terminal
    AssertionError: None != XYZ.ABCDEFGHIJKLMN
    
  • I change the return statement to see the difference between the inputs and expected output, remember the identity function?

    1def subtract(first_input, second_input):
    2    return first_input, second_input
    

    the terminal is my friend, and shows AssertionError with random numbers that look like this

    terminal
    AssertionError: (XYZ.ABCDEFGHIJKLMN, YZA.BCDEFGHIJKLMNO) != ZAB.CDEFGHIJKLMNOP
    

    the name of the function is subtract and the assertion expects the difference between the two inputs

  • I make the return statement match the expectation

    1def subtract(first_input, second_input):
    2    return first_input - second_input
    

    the terminal is my friend, and shows AttributeError

    terminal
    AttributeError: module 'src.calculator' has no attribute 'multiply'
    

  • I add a function

    1def subtract(first_input, second_input):
    2    return first_input - second_input
    3
    4
    5def multiply():
    6    return None
    

    the terminal is my friend, and shows TypeError

    terminal
    TypeError: multiply() takes 0 positional arguments but 2 were given
    

    I add 2 names for the positional arguments

    the terminal is my friend, and shows AssertionError

    terminal
    AssertionError: None != XY.ZABCDEFGHIJKLM
    
  • I change the return statement to see the difference between the inputs and the expected output, this is the identity function again

    the terminal is my friend, and shows AssertionError with random numbers that look like this

    terminal
    AssertionError: (XYZ.ABCDEFGHIJKLMNO, -YZA.BCDEFGHIJKLMNOPQ) != -ZAB.CDEFGHIJKLMNOPQR
    
  • I change it to the multiplication of the inputs to match the name of the function

    the terminal is my friend, and shows AttributeError

    terminal
    AttributeError: module 'src.calculator' has no attribute 'divide'
    

  • I add another function

    the terminal is my friend, and shows AssertionError with random numbers that look like this

    terminal
    AssertionError: (-XYZ.ABCDEFGHIJKLMNO, YZA.BCDEFGHIJKLMNOPQ) != -ZAB.CDEFGHIJKLMNOPQR
    
  • I change the return statement to give the test what it wants

    the terminal is my friend, and shows AttributeError

    terminal
    AttributeError: module 'src.calculator' has no attribute 'add'
    

  • the return statement of the last 3 functions matched their names, I do the same thing for the new one

     1def subtract(first_input, second_input):
     2    return first_input - second_input
     3
     4
     5def multiply(first_input, second_input):
     6    return first_input * second_input
     7
     8
     9def divide(first_input, second_input):
    10    return first_input / second_input
    11
    12
    13def add(first_input, second_input):
    14    return first_input + second_input
    

    and all the tests are passing with no random failures, or are they?


close the project

  • I close calculator.py

  • I click in the terminal where the tests are running

  • I use q on the keyboard to leave the tests. The terminal goes back to the command line.

  • I change directory to the parent of calculator

    terminal
    cd ..
    

    the terminal is my friend, and shows

    .../pumping_python
    

    I am back in the pumping_python directory.


review

I wrote these tests for a program that can add, subtract, multiply and divide

I also saw these Exceptions


code from the chapter

Do you want to see all the CODE I typed in this chapter?


what is next?

Would you like to see a better way to write test_why_use_a_function?