第一条用例跑出来什么
(每道题开头都有同一段:被测模块 c_to_f / length,和一个静音跑测试的 run(),它交回「跑了几条/挂了几条/出错几条」。)
运行下面这段程序,看一条通过的用例给出的三个数:
import io, unittest
LEN = {"m": 1.0, "km": 1000.0, "cm": 0.01}
def c_to_f(c):
return c * 9 / 5 + 32
def length(v, src, to):
for u in (src, to):
if u not in LEN:
raise ValueError("不认识的单位:" + u)
return v * LEN[src] / LEN[to]
def run(*cases):
suite = unittest.TestSuite()
for c in cases:
suite.addTests(unittest.defaultTestLoader.loadTestsFromTestCase(c))
r = unittest.TextTestRunner(stream=io.StringIO()).run(suite)
return str(r.testsRun) + "/" + str(len(r.failures)) + "/" + str(len(r.errors))
class TestTemp(unittest.TestCase):
def test_37(self):
self.assertEqual(c_to_f(37), 98.6)
print(run(TestTemp))
全部评论