Django1.11中的單元測(cè)試默認(rèn)情況下都是放在相應(yīng)app的tests.py文件下的。django的TestCase是從python的unittest.TestCase繼承過(guò)來(lái)的,所有后者有的函數(shù)前者都有。
在django中運(yùn)行單元測(cè)試可以用pyhton manage.py test這樣是運(yùn)行project中所有的單元測(cè)試。
python manage.py test appname這樣是運(yùn)行這個(gè)app中的所有單元測(cè)試
python manage.py test appname.testclassname這樣是運(yùn)行app中某個(gè)測(cè)試類的所有的測(cè)試
pyhton manage.py test appname.testclassname.testfunctionname這樣是運(yùn)行app中某個(gè)測(cè)試類里面某個(gè)測(cè)試方法。
在django中如果要想讓django想上面那樣運(yùn)行單元測(cè)試,必須將測(cè)試文件命名為tests.py,否則會(huì)報(bào)錯(cuò)。
我嘗試了一下直接到tests.py文件目錄下運(yùn)行直接運(yùn)行tests.py就像普通的python的單元測(cè)試運(yùn)行一樣,結(jié)果報(bào)錯(cuò)沒(méi):ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.上網(wǎng)查了一下解決方法:http://blog.csdn.net/mooninday/archive/2008/10/17/3092023.aspx。但是第一種方法好像不行,第二種方法沒(méi)試。
參考:http://docs.djangoproject.com/en/dev/topics/testing/#topics-testing