unittest runner生成test anything协议(tap)输出
simpletap的Python项目详细描述
simpletap是一个与unittest框架集成的测试运行程序 生成与tap(测试任何协议)兼容的输出。
simpletap已经在Python2.7下进行了广泛的测试,并被认为是生产就绪的。
还支持python 3.4-3.5,但还没有看到那么多的测试。
用法
在测试脚本中,而不是:
if__name__=="__main__":unittest.main()
使用:
if__name__=="__main__":fromsimpletapimportTAPTestRunnerunittest.main(testRunner=TAPTestRunner())
像这样的小测试用例:
importunittestclassIntegerArithmeticTestCase(unittest.TestCase):deftestAdd(self):# test method names begin 'test*'"test adding values"self.assertEqual((1+2),3)self.assertEqual(0+1,1)deftestMultiply(self):"test multiplying values"self.assertEqual((0*10),0)self.assertEqual((5*8),40)deftestFail(self):"a failing test"self.assertEqual(0,1)@unittest.expectedFailuredeftestExpectFail(self):"we saw this coming"self.assertEqual(0,1)@unittest.skipIf(True,"Skipping this one")deftestSkip(self):"pending a fix"self.assertEqual(0,1)deftestError(self):"oops something went wrong"no_such_variable+1# Oops!if__name__=="__main__":fromsimpletapimportTAPTestRunnerunittest.main(testRunner=TAPTestRunner())
当保存在名为test.py的文件中并执行时,将产生:
1..6ok1 - test.py: test adding values not ok2 - test.py: oops something went wrong # ERROR: NameError on file test.py line 30 in testError: 'no_such_variable + 1 # Oops!': # global name 'no_such_variable' is not defined skip 3 - test.py: we saw this coming # EXPECTED_FAILURE: AssertionError on file test.py line 21 in testExpectFail: 'self.assertEqual(0, 1)': # 0 != 1 not ok4 - test.py: a failing test # FAIL: AssertionError on file test.py line 16 in testFail: 'self.assertEqual(0, 1)': # 0 != 1 ok5 - test.py: test multiplying values skip 6 - test.py: pending a fix # SKIP: # Skipping this one
您还可以直接从命令行启动simpletap,这与使用unittest的方式非常相似:
python -m simpletap test.IntegerArithmeticTestCase
与标准的偏差
test anything协议的规范将跳过的测试视为ok。
在使用这个模块的过程中,我们发现将它们作为 以及作为关键字skip下的规范扩展的预期失败。