Vin*_*fer 4 python testing django pytest tox
我是 Django 和测试的新手,所以请耐心等待。
试图在终端上运行我在 Django 项目中开发的代码的新测试,但不幸的是我继承了几个测试失败(已经通过分析我之前的提交确认了这一点)。我正在尝试运行/修复我的测试/代码。不修复所有失败的测试(至少现在不是)。今天,我们通过tox
在主项目文件夹中运行来运行测试,tox 最终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
Run Code Online (Sandbox Code Playgroud)
这是我的测试,位于 ~/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'], '')
...
Run Code Online (Sandbox Code Playgroud)
我应该如何使用 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': ''}
Run Code Online (Sandbox Code Playgroud)
但是当我只单独运行测试时,测试通过了!我得到这个结果:
tox -e py27 -- servicos.tests:FormatacaoDeCamposTestCase.test_formata_campos_para_estomatologia
py27 installed:
py27 runtests: PYTHONHASHSEED='3025337440'
______________________________________________________________ summary ______________________________________________________________
py27: commands succeeded
congratulations :)
Run Code Online (Sandbox Code Playgroud)
在互联网上挖掘,我发现我必须拥有{posargs}
否则毒物会忽略我提供给它的任何东西。但是,只有我的命令的第二行会使用它。第一行将测试数据库设置为 SQLite(用于测试的更快的数据库集)。这可能是毒药的错误吗?关于如何解决这个问题的任何想法?
更新 2:@phd 的答案是最接近的,但我不得不调整一些事情:
最终命令如下所示:
tox -- folder1/folder2/file_name.py::ClassName::test_method_name
Run Code Online (Sandbox Code Playgroud)
您应该准备 tox.ini 以接受命令行参数并将它们传递给 pytest:
[testenv:…]
commands =
py.test -n 4 {posargs}
Run Code Online (Sandbox Code Playgroud)
之后,您可以传递尽可能少或尽可能多的参数:
tox -e $TOXENV -- test1.py test2.py…
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2517 次 |
最近记录: |