unittest: make unittest.main() work

Enables standalone test scripts to use the common idiom
  if __name__ == '__main__':
      unittest.main()
e.g. https://docs.python.org/3.4/library/unittest.html#basic-example
pull/38/merge
Tom Soulanille 2015-07-23 23:26:37 -07:00 zatwierdzone przez Paul Sokolovsky
rodzic 07830bd027
commit c98be9b0fc
3 zmienionych plików z 36 dodań i 5 usunięć

Wyświetl plik

@ -1,4 +1,4 @@
srctype = micropython-lib
type = module
version = 0.0.7
version = 0.0.8
author = Paul Sokolovsky

Wyświetl plik

@ -0,0 +1,20 @@
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()

Wyświetl plik

@ -116,8 +116,19 @@ def run_class(c, test_result):
def main(module="__main__"):
def test_cases(m):
for tn in dir(m):
c = getattr(m, tn)
if isinstance(c, object) and isinstance(c, type) and issubclass(c, TestCase):
yield c
m = __import__(module)
for tn in dir(m):
c = getattr(m, tn)
if isinstance(c, object) and issubclass(c, TestCase):
run_class(c)
suite = TestSuite()
for c in test_cases(m):
suite.addTest(c)
runner = TestRunner()
result = runner.run(suite)
msg = "Ran %d tests" % result.testsRun
if result.skippedNum > 0:
msg += " (%d skipped)" % result.skippedNum
print(msg)