Skip to content

Instantly share code, notes, and snippets.

@kostasdizas
Last active August 29, 2015 14:09
Show Gist options
  • Save kostasdizas/b69c136206e049396e38 to your computer and use it in GitHub Desktop.
Save kostasdizas/b69c136206e049396e38 to your computer and use it in GitHub Desktop.
CE151 Assignment Testing
import unittest
from unittest.mock import patch
from io import StringIO
import ass1
def ex_run(func, inputs):
print("-" * 50)
print("running: {0} with inputs {1}".format(func.__name__, ", ".join(inputs)))
ass1.input = lambda x: input(x + "\n")
with patch("sys.stdin", StringIO("\n".join(inputs))), patch("sys.stdout", new_callable=StringIO) as mocked_out:
func()
return(mocked_out.getvalue())
class Exercise1(unittest.TestCase):
def setUp(self):
self.ex_run = lambda x: ex_run(ass1.ex1, x)
def test1(self):
print(self.ex_run(['5', '12']))
def test2(self):
print(self.ex_run(['1.5', '2']))
def test3(self):
print(self.ex_run(['-1', '-1']))
class Exercise2(unittest.TestCase):
def setUp(self):
self.ex_run = lambda x: ex_run(ass1.ex2, x)
def test1(self):
print(self.ex_run(['14']))
def test2(self):
print(self.ex_run(['-14']))
class Exercise3(unittest.TestCase):
def setUp(self):
self.ex_run = lambda x: ex_run(ass1.ex3, x)
def test1(self):
print(self.ex_run(['-1']))
def test2(self):
print(self.ex_run(['2']))
print(self.ex_run(['3']))
print(self.ex_run(['5']))
print(self.ex_run(['23']))
print(self.ex_run(['37']))
def test3(self):
print(self.ex_run(['1']))
print(self.ex_run(['4']))
print(self.ex_run(['9']))
print(self.ex_run(['12']))
print(self.ex_run(['35']))
class Exercise4(unittest.TestCase):
def setUp(self):
self.ex_run = lambda x: ex_run(ass1.ex4, x)
def test1(self):
print(self.ex_run(['0', '0']))
def test2(self):
print(self.ex_run(['3', '3']))
print(self.ex_run(['6', '4']))
print(self.ex_run(['7', '3']))
print(self.ex_run(['5', '1']))
print(self.ex_run(['3', '7']))
class Exercise5(unittest.TestCase):
def setUp(self):
self.ex_run = lambda x: ex_run(ass1.ex5, x)
def test1(self):
print(self.ex_run(['hi']))
def test2(self):
print(self.ex_run(['hi my name is bob']))
def test3(self):
print(self.ex_run(['I']))
class Exercise6(unittest.TestCase):
def setUp(self):
self.ex_run = lambda x: ex_run(ass1.ex6, x)
def test1(self):
print(self.ex_run(['aabbbaa']))
def test2(self):
print(self.ex_run(['aaeeAAA']))
def test3(self):
print(self.ex_run(['ioio']))
def test4(self):
print(self.ex_run(['I']))
def test5(self):
print(self.ex_run(['whtspp']))
if __name__ == '__main__':
unittest.main(verbosity=0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment