kopia lustrzana https://github.com/micropython/micropython-lib
unittest: fill out asserts and test them
rodzic
edcf812ce7
commit
29a9f56372
|
@ -1,20 +1,64 @@
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
class TestStringMethods(unittest.TestCase):
|
class TestUnittestAssertions(unittest.TestCase):
|
||||||
|
|
||||||
def test_upper(self):
|
def testFail(self):
|
||||||
self.assertEqual('foo'.upper(), 'FOO')
|
with self.assertRaises(AssertionError):
|
||||||
|
self.fail('failure')
|
||||||
|
|
||||||
def test_isupper(self):
|
def testEqual(self):
|
||||||
self.assertTrue('FOO'.isupper())
|
self.assertEqual(0,0)
|
||||||
self.assertFalse('Foo'.isupper())
|
self.assertEqual([0,1,2], [0,1,2])
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
self.assertEqual(0,None)
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
self.assertEqual([0,1,2], [1,2,3])
|
||||||
|
|
||||||
def test_split(self):
|
def testNotEqual(self):
|
||||||
s = 'hello world'
|
self.assertNotEqual([0,1,2], [0,2,1])
|
||||||
self.assertEqual(s.split(), ['hello', 'world'])
|
with self.assertRaises(AssertionError):
|
||||||
# check that s.split fails when the separator is not a string
|
self.assertNotEqual(0,0)
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(AssertionError):
|
||||||
s.split(2)
|
self.assertNotEqual([0,1,2], [0,1,2])
|
||||||
|
|
||||||
|
def testIs(self):
|
||||||
|
self.assertIs(None, None)
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
self.assertIs([1,2,3], [1,2,3])
|
||||||
|
|
||||||
|
def testIsNot(self):
|
||||||
|
self.assertIsNot([1,2,3], [1,2,3])
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
self.assertIsNot(None, None)
|
||||||
|
|
||||||
|
def testTrue(self):
|
||||||
|
self.assertTrue(True)
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
self.assertTrue(False)
|
||||||
|
|
||||||
|
def testFalse(self):
|
||||||
|
self.assertFalse(False)
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
self.assertFalse(True)
|
||||||
|
|
||||||
|
def testIn(self):
|
||||||
|
self.assertIn('t', 'cat')
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
self.assertIn('x', 'cat')
|
||||||
|
|
||||||
|
def testIsInstance(self):
|
||||||
|
self.assertIsInstance('cat', str)
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
self.assertIsInstance(7, str)
|
||||||
|
|
||||||
|
def testRaises(self):
|
||||||
|
with self.assertRaises(ZeroDivisionError):
|
||||||
|
1/0
|
||||||
|
pass
|
||||||
|
|
||||||
|
@unittest.skip('test of skipping')
|
||||||
|
def testSkip(self):
|
||||||
|
self.assertFail('this should be skipped')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -12,7 +12,7 @@ class AssertRaisesContext:
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_value, tb):
|
def __exit__(self, exc_type, exc_value, tb):
|
||||||
if exc_type is None:
|
if exc_type is None:
|
||||||
assert False, "%r not raised" % exc
|
assert False, "%r not raised" % self.expected
|
||||||
if issubclass(exc_type, self.expected):
|
if issubclass(exc_type, self.expected):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
@ -28,15 +28,34 @@ class TestCase:
|
||||||
msg = "%r vs (expected) %r" % (x, y)
|
msg = "%r vs (expected) %r" % (x, y)
|
||||||
assert x == y, msg
|
assert x == y, msg
|
||||||
|
|
||||||
|
def assertNotEqual(self, x, y, msg=''):
|
||||||
|
if not msg:
|
||||||
|
msg = "%r not expected to be equal %r" % (x, y)
|
||||||
|
assert x != y, msg
|
||||||
|
|
||||||
def assertIs(self, x, y, msg=''):
|
def assertIs(self, x, y, msg=''):
|
||||||
if not msg:
|
if not msg:
|
||||||
msg = "%r is not %r" % (x, y)
|
msg = "%r is not %r" % (x, y)
|
||||||
assert x is y, msg
|
assert x is y, msg
|
||||||
|
|
||||||
|
def assertIsNot(self, x, y, msg=''):
|
||||||
|
if not msg:
|
||||||
|
msg = "%r is %r" % (x, y)
|
||||||
|
assert x is not y, msg
|
||||||
|
|
||||||
def assertTrue(self, x, msg=''):
|
def assertTrue(self, x, msg=''):
|
||||||
|
if not msg:
|
||||||
|
msg = "Expected %r to be True" % x
|
||||||
assert x, msg
|
assert x, msg
|
||||||
|
|
||||||
|
def assertFalse(self, x, msg=''):
|
||||||
|
if not msg:
|
||||||
|
msg = "Expected %r to be False" % x
|
||||||
|
assert not x, msg
|
||||||
|
|
||||||
def assertIn(self, x, y, msg=''):
|
def assertIn(self, x, y, msg=''):
|
||||||
|
if not msg:
|
||||||
|
msg = "Expected %r to be in %r" % (x, y)
|
||||||
assert x in y, msg
|
assert x in y, msg
|
||||||
|
|
||||||
def assertIsInstance(self, x, y, msg=''):
|
def assertIsInstance(self, x, y, msg=''):
|
||||||
|
@ -54,9 +73,6 @@ class TestCase:
|
||||||
return
|
return
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def assertFalse(self, x, msg=''):
|
|
||||||
assert not x, msg
|
|
||||||
|
|
||||||
|
|
||||||
def skip(msg):
|
def skip(msg):
|
||||||
def _decor(fun):
|
def _decor(fun):
|
||||||
|
|
Ładowanie…
Reference in New Issue