我是Django的新手和測試,請耐心等待。Python tox和py.test:如何運行只是一個測試,而不是整個測試套件
試圖在終端上運行我在Django項目中開發的代碼的新測試,但不幸的是,我繼承了幾次測試失敗(已經通過分析我的提交之前已確認)。我試圖運行/修復我的測試/代碼。沒有修復所有令人失望的測試(至少現在不是)。今天,我們通過在主項目文件夾中運行tox
來運行測試,並且tox最終以不同的數據庫(在開發/生產中使用Postgresql,但對於我們使用SQLite的測試)調用py.test
。這裏是tox.ini
配置
[tox]
envlist = unit
skipsdist = True
[testenv:unit]
deps = -rrequirements/test.txt
commands =
bash -c 'TESTING_DB=db.sqlite3 python manage.py initdb --settings telessaude.settings.test'
py.test -n 4
passenv = *
setenv =
DJANGO_SETTINGS_MODULE=telessaude.settings.test
whitelist_externals =
/bin/bash
[flake8]
max-line-length = 110
[pytest]
setenv=
DJANGO_SETTINGS_MODULE=telessaude.settings.test
python_files = **/tests.py **/tests/*.py **/tests.py
norecursedirs = requirements .tox media
,這裏是我的測試中,位於~/project_name/servicos/tests.py
# encoding: utf-8
from fluxos.tests import BaseFluxoTestCase
from core.tests import BaseTestCase
from servicos.models import Estomatologia
class EstomatologiaTestCase(BaseTestCase):
def testa_formata_campos(self):
estomato = Estomatologia()
return_value = estomato.formata_campos()
self.assertEquals(return_value['comorbidades_paciente'], '')
...
我應該怎麼做或者TOX或py.test,僅運行這個新創建的測試?謝謝!
更新1:不幸的是建議的答案並沒有奏效。當我運行phd和Windsooon建議的單個測試時,tox似乎沒有運行任何測試。我寫了一個失敗的測試(故意),當我運行tox
命令所有測試,錯誤顯示出來:
def test_formata_campos_para_estomatologia(self):
estomatologia = Estomatologia()
retorno = formata_campos(estomatologia)
> self.assertIn('objetivos', retorno) E AssertionError: 'objetivos' not found in {'comorbidades_paciente': '', 'tempo_transcorrido': '', 'sinais_e_sintomas_locais': ''}
但是當我單獨只運行測試,測試通過!我得到這樣的結果:
tox -e py27 -- servicos.tests:FormatacaoDeCamposTestCase.test_formata_campos_para_estomatologia
py27 installed:
py27 runtests: PYTHONHASHSEED='3025337440'
______________________________________________________________ summary ______________________________________________________________
py27: commands succeeded
congratulations :)
周圍挖了互聯網,我發現我必須有{posargs}
否則TOX會忽略不管我提供給它。然而,只有我的命令的第二行會使用它。第一行將測試數據庫設置爲SQLite(用於測試的更快的數據庫集)。這可能是一個關於tox的錯誤嗎?任何想法如何解決這個問題?
更新2:從@phd答案是最接近的一個,但我不得不去適應一個幾件事情:
- 需要使用2冒號標記,而不是一個
- 必由之路使用2個冒號標記來代替方法名稱
- 也必須刪除「-e」參數,因爲該環境未在我的tox.ini文件中設置。
最終命令如下:
tox -- folder1/folder2/file_name.py::ClassName::test_method_name
我懷疑這是可行的。 'tox'把參數放在哪裏? – phd
你的意思是什麼? – Windsooon
tox應該把'test_file_name_here.py:TestClassName.test_method_name'放在哪裏? – phd