Vini.g.fer
Vini.g.fer

Reputation: 11919

Python tox and py.test: how to run just a single test rather than the whole test suite

I'm new to Django and testing so please bear with me.

Trying to run on the terminal a new test for code I developed in a Django project, but unfortunatly I inherited several test failures (already confirmed that by analyzing commits prior to mine). I'm trying to run/fix just my test/code. Not fix all the failling tests (at least not right now). Today we run the tests by just running tox in the main project folder, and tox ends up calling py.test, with a different database (in development/production we use Postgresql, but for tests we use SQLite). Here is the tox.ini configuration

[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

And here is my test, located at ~/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'], '')

    ...

What should I do with either tox or py.test to run just this newly created test? Thanks!


Update 1: Unfortunatly the suggested answers didn't worked. When I run a single test as suggested by phd and Windsooon, tox doesn't seem to be running any tests. I wrote a test that fails (on purpose) and when I run all tests with tox command, the error shows up:

    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': ''}

But when I run only the test individually, the test passes! I get this result:

tox -e py27 -- servicos.tests:FormatacaoDeCamposTestCase.test_formata_campos_para_estomatologia
py27 installed: 
py27 runtests: PYTHONHASHSEED='3025337440'
______________________________________________________________ summary ______________________________________________________________
  py27: commands succeeded
  congratulations :)

Digging around the internet, I found that I must have the {posargs} otherwise tox will ignore whatever I supply to it. However only the second line of my commands will use it. The first line will set the test database to SQLite (faster database set for testing). Can this be a bug on tox? Any idea on how to solve this?


Update 2: The answer from @phd was the closest one, but I had to adapt a few things:

Final command looks like this:

tox -- folder1/folder2/file_name.py::ClassName::test_method_name

Upvotes: 4

Views: 4711

Answers (2)

phd
phd

Reputation: 94676

You should prepare tox.ini to accept command line arguments and pass them to pytest:

[testenv:…]
commands =
    py.test -n 4 {posargs}

After that you can pass as little or as many arguments:

tox -e $TOXENV -- test1.py test2.py…

Upvotes: 5

Windsooon
Windsooon

Reputation: 7130

Try

tox -e py27 -- test_file_name_here.py:TestClassName.test_method_name

Upvotes: 3

Related Questions