Skip to content

Instantly share code, notes, and snippets.

@rer145
Created November 8, 2017 21:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rer145/046d611f363c8e6c5e95808193fd9fab to your computer and use it in GitHub Desktop.
How to do a simple unit test in python
# Our function to test
def above_freezing(celcius):
""" (int) -> boolean
Return True if celcius is above zero, and False if not
"""
return celcius > 0
# Our test module
import unittest
class TestAboveFreezing(unittest.TestCase):
# Function to test when celcius is above zero
def test_above_freezing_above(self):
expected = True
actual = above_freezing(5.2)
self.assertEqual(expected, actual, "The temperature is above freezing.")
# Function to test when celcius is below zero
def test_above_freezing_below(self):
expected = False
actual = above_freezing(-2)
self.assertEqual(expected, actual, "The temperature is below freezing.")
# Function to test when celcius is equal to zero
def test_above_freezing_at_zero(self):
expected = False
actual = above_freezing(0)
self.assertEqual(expected, actual, "The temperature is at the freezing mark.")
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment