#!/usr/bin/python3
import unittest
def calc(expression):
return eval(expression)
def add_test(name, asserts):
def test_method(asserts):
def fn(self):
left, right = asserts.split('=')
expected = str(calc(left))
self.assertEqual(expected, right)
return fn
d = {'test1': test_method(asserts)}
cls = type(name, (unittest.TestCase,), d)
globals()[name] = cls
if __name__ == '__main__':
for i, t in enumerate([
"1+2=3",
"3*5*6=90"]):
add_test("Test%d" % i, t)
unittest.main()
python type函数
最新推荐文章于 2024-01-24 06:00:00 发布