unittest之TextTestRunner


unittest中,當要運行用例並生成報告時,有多種方式。現在我們就來將最原始的一種方式:TextTestRunner()

該方式會將測試文件寫到一個文件中。

示例:

# 時間戳
time_stamp = time.strftime("%Y%m%d%H%M%S")
report_dir = os.path.join(os.path.dirname(__file__), "report", (time_stamp + ".txt"))

if __name__ == '__main__':
    suite = unittest.TestLoader().discover(start_dir=r".\test_collections_b", pattern='test*.py', top_level_dir=None)
    with open(file=report_dir, mode="a", encoding="utf-8") as file:
        runner = unittest.TextTestRunner(stream=file, descriptions=True, verbosity=2)
        runner.run(suite)

在示例中:
1、我們生成了一個時間戳:time_stamp,用於區分不同的測試報告文件,且可以看到測試報告的生成信息。
2、report_dir為測試報告文件的完整路徑,文件名為time_stamp
3、在window中,os.mknod()無法生成一個空的文件,那么我們就打開測試報告文件時使用追加模式(mode="a")
這樣在文件不存在時,將會生成一個新的文件。
4、實例化一個TextTestRunner對象,TextTestRunner()主要參數:
a,stream:文件流。
b,verbosity:報告詳細等級。
5,TextTestRunner對象調用run方法(傳入套件)

此種方式較老,落伍了!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM