1、基本用法:用于检查代码的运行结果是否正确
注意:方法必须用test开头
def calc(express):
try:
ret=eval(express)
except:
return
else:
return ret
import unittest
class Test_calc(unittest.TestCase):
def test_add(self):
express="1+1"
ret=calc(express)
self.assertEqual(2,ret,"%s测试不通过"%express)
def test_sub(self):
express="1-1"
ret=calc(express)
self.assertEqual(0,ret,"%s测试不通过"%express)
def test_multi(self):
express="1*3"
ret=calc(express)
self.assertEqual(2,ret,"%s测试不通过"%express)
def test_divi(self):
express="1/1"
ret=calc(express)
self.assertEqual(1,ret,"%s测试不通过"%express)
def test_type(self):
express = "1+1"
ret = calc(express)
self.assertIsInstance(ret,str,"类型不一致")
if __name__ == '__main__':
unittest.main()
结果: