[英]Python tox and py.test: how to run just a single test rather than the whole test suite
我刚接触Django并进行测试,请耐心等待。
试图在终端上运行我在Django项目中开发的代码的新测试,但不幸的是我继承了几个测试失败(已经通过分析我之前的提交已经确认)。 我正在尝试运行/修复我的测试/代码。 没有修复所有的故障测试(至少现在不是)。 今天我们通过在主项目文件夹中运行tox
来运行测试,并且tox最终调用py.test
,使用不同的数据库(在开发/生产中我们使用Postgresql,但是对于我们使用SQLite的测试)。 这是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的答案是最接近的答案,但我必须适应一些事情:
最终命令如下所示:
tox -- folder1/folder2/file_name.py::ClassName::test_method_name
您应该准备tox.ini接受命令行参数并将它们传递给pytest:
[testenv:…]
commands =
py.test -n 4 {posargs}
之后,您可以传递少量或多个参数:
tox -e $TOXENV -- test1.py test2.py…
尝试
tox -e py27 -- test_file_name_here.py:TestClassName.test_method_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.