Python tox和py.test:如何运行单个测试而不是整个测试套件

时间:2017-07-05 14:33:19

标签: python django testing pytest tox

我是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的答案是最接近的答案,但我不得不改编一些事情:

  • 需要使用2个冒号而不是一个
  • 必须使用2个冒号而不是点来将方法名与类
  • 分开
  • 还必须删除“-e”参数,因为我的tox.ini文件中没有设置该环境。

最终命令如下所示:

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

2 个答案:

答案 0 :(得分:3)

您应该准备tox.ini以接受命令行参数并将它们传递给pytest:

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

之后你可以传递少量或多个参数:

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

答案 1 :(得分:2)

尝试

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="8dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    >

    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="4dp"
        app:layout_constraintBottom_toTopOf="@+id/search_button"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintVertical_chainStyle="spread_inside"
        app:layout_constraintVertical_weight="1"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="4dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:layout_marginStart="4dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:background="@android:drawable/dialog_holo_light_frame"
            android:paddingEnd="8dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:paddingStart="8dp"
            android:paddingTop="8dp">

            <LinearLayout
                android:id="@+id/frameLayout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:orientation="vertical"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <include layout="@layout/layout_subject_search" />

            </LinearLayout>


            <View
                android:id="@+id/view"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:background="?android:attr/listDivider"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/frameLayout" />

            <LinearLayout
                android:id="@+id/frameLayout1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:orientation="vertical"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view">

                <include layout="@layout/layout_qualification_search" />

            </LinearLayout>

            <View
                android:id="@+id/view2"
                android:layout_width="0dp"
                android:layout_height="2dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:background="?android:attr/listDivider"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/frameLayout1" />


            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:text="@string/search_by_location_label"
                android:textSize="15sp"
                android:textStyle="bold"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view2" />

            <Switch
                android:id="@+id/location_toggle_switch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_marginEnd="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="16dp"
                android:text=""
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view2" />

            <FrameLayout
                android:id="@+id/map_search_fragment"
                android:layout_width="0dp"
                android:layout_height="240dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/textView"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:background="@android:drawable/dialog_holo_light_frame"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/location_toggle_switch"
                app:layout_constraintVertical_bias="0.0">

            </FrameLayout>

        </android.support.constraint.ConstraintLayout>

    </ScrollView>

    <Button
        android:id="@+id/search_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp"
        android:text="@string/search_tutors_string"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/scrollView2"
        app:layout_constraintVertical_weight="0" />

    <ProgressBar
        android:id="@+id/search_tutors_progress_bar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:visibility="gone"/>


</android.support.constraint.ConstraintLayout>
相关问题