Skip to content

Instantly share code, notes, and snippets.

@pocc
Created September 2, 2018 01:12
Show Gist options
  • Save pocc/7afff083de9c9feded04cec532aad17c to your computer and use it in GitHub Desktop.
Save pocc/7afff083de9c9feded04cec532aad17c to your computer and use it in GitHub Desktop.
Unittest practice with arithmetic functions
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Unittest practice
This program does basic arithmetic and provides a test class to test it.
# Running
python3 -m unittest test_simple_calculator.py
"""
import unittest
def add(arg1, arg2):
"""Return the sum of two numbers"""
return arg1 + arg2
def subtract(arg1, arg2):
"""Return the difference of two numbers"""
return arg1 - arg2
def multiply(arg1, arg2):
"""Return the product of two numbers"""
return arg1 * arg2
def divide(arg1, arg2):
"""Return the quotient of two numbers"""
return arg1 / arg2
class TestSimpleCalculator(unittest.TestCase):
"""Test simple calculator functions."""
def test_add_positives(self):
self.assertEqual(add(2, 1), 3)
def test_add_negatives(self):
self.assertEqual(add(-2, -1), -3)
def test_subtract_positives(self):
self.assertEqual(subtract(2, 1), 1)
def test_subtract_negatives(self):
self.assertEqual(subtract(-2, -1), -1)
def test_multiply_positives(self):
self.assertEqual(multiply(2, 1), 2)
def test_multiply_negatives(self):
self.assertEqual(multiply(-2, -1), 2)
def test_divide_positives(self):
self.assertEqual(multiply(2, 1), 2)
def test_divide_negatives(self):
self.assertEqual(multiply(-2, -1), 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment