Last active
December 18, 2015 07:09
-
-
Save kannokanno/5744418 to your computer and use it in GitHub Desktop.
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
import unittest | |
def plus(a, b): | |
return a + b | |
def minus(a, b): | |
return a - b | |
class TestDataInTestMethod(unittest.TestCase): | |
def test_plus(self): | |
testdata_integers = [(1, 2), (-1, 2), (0, 0)] | |
for a, b in testdata_integers: | |
self.assertEqual(plus(a, b), a + b) | |
def test_minus(self): | |
testdata_integers = [(1, 2), (-1, 2), (0, 0)] | |
for a, b in testdata_integers: | |
self.assertEqual(minus(a, b), a - b) | |
class TestDataHelperMethod(unittest.TestCase): | |
def test_plus(self): | |
for a, b in self.__testdata_integers(): | |
self.assertEqual(plus(a, b), a + b) | |
def test_minus(self): | |
for a, b in self.__testdata_integers(): | |
self.assertEqual(minus(a, b), a - b) | |
def __testdata_integers(self): | |
return [(1, 2), (-1, 2), (0, 0)] | |
class TestDataInSetUp(unittest.TestCase): | |
def setUp(self): | |
self.__testdata_integers = [(1, 2), (-1, 2), (0, 0)] | |
def test_plus(self): | |
for a, b in self.__testdata_integers: | |
self.assertEqual(plus(a, b), a + b) | |
def test_minus(self): | |
for a, b in self.__testdata_integers: | |
self.assertEqual(minus(a, b), a - b) | |
# see https://pypi.python.org/pypi/unittest-data-provider/1.0.0 | |
def data_provider(fn_data_provider): | |
def test_decorator(fn): | |
def repl(self, *args): | |
for i in fn_data_provider(): | |
try: | |
fn(self, *i) | |
except AssertionError: | |
print "Assertion error caught with data set ", i | |
raise | |
return repl | |
return test_decorator | |
class TestDataByDecorator(unittest.TestCase): | |
__testdata_integers = lambda: [(1, 2), (-1, 2), (0, 0)] | |
@data_provider(__testdata_integers) | |
def test_plus(self, a, b): | |
self.assertEqual(plus(a, b), a + b) | |
@data_provider(__testdata_integers) | |
def test_minus(self, a, b): | |
self.assertEqual(minus(a, b), a - b) | |
class TestDataByDecorator2(unittest.TestCase): | |
__testdata_integers = lambda: [[1, 2, 3], [-1, 2, 1], [0, 0, 0]] | |
@data_provider(__testdata_integers) | |
def test_plus(self, a, b, expected): | |
self.assertEqual(plus(a, b), expected) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment