如何進行Django單元測試


如何進行Django單元測試

Django的單元測試使用python的unittest模塊,這個模塊使用基於類的方法來定義測試。類名為django.test.TestCase,繼承於python的unittest.TestCase。

 

執行目錄下所有的測試(所有的test*.py文件):運行測試的時候,測試程序會在所有以test開頭的文件中查找所有的test cases(inittest.TestCase的子類),自動建立測試集然后運行測試。

$ python manage.py test

執行animals項目下tests包里的測試:

$ python manage.py testanimals.tests

執行animals項目里的test測試:

$ python manage.py testanimals

單獨執行某個test case:

$ python manage.py testanimals.tests.AnimalTestCase

單獨執行某個測試方法:

$ python manage.py testanimals.tests.AnimalTestCase.test_animals_can_speak

為測試文件提供路徑:

$ python manage.py testanimals/

通配測試文件名:

$ python manage.py test--pattern="tests_*.py"

啟用warnings提醒:

$ python -Wall manage.py test


免責聲明!

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



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