W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
本任務(wù)最初的目的只是為了在測試過程中使用簡單的命令行運行不同的函數(shù),類似運行 “python test_test.py” 運行整個測試,運行 “python test_test.py debug” 來運行測試但是不收集運行結(jié)果,請看如下的代碼:
import unittest
import sys
class Tests(unittest.TestCase):
def testAddOnePlusOne(self):
assert 1 == 2
def main():
unittest.TextTestRunner().run(test_suite())
def test_suite():
return unittest.makeSuite(Tests, 'test')
def debug():
test_suite().debug()
if __name__ == '__main__':
if len(sys.argv) > 1:
globals()[sys.argv[1]]()
else:
main()
這里如果在命令行中直接運行 “python cookbook_8.py” 就會執(zhí)行 “main()”;如果在命令行中運行 “python cookbook_8.py debug” 會執(zhí)行 “debug()”。
“globals()” 返回的是當(dāng)前全局變量的引用。如果有其它的需求,可以充分利用本任務(wù)來延伸!
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: