我是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