Created
November 8, 2017 21:02
-
-
Save rer145/046d611f363c8e6c5e95808193fd9fab to your computer and use it in GitHub Desktop.
How to do a simple unit test in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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