PyTest与单django模型创建

时间:2016-03-16 18:32:28

标签: python django pytest mixer

我使用混音器生成我的模型并使用pytest运行它们。 我想做的只是生成我的模型一次,并使用我生成的相同模型运行几个测试。

这就是我想要的:

代码

RawImage screenDisplay;
if(updateScreen){
camTexture.LoadImage(JPEG_VIDEO_STREAM);
screenDisplay.texture = camTexture;
updateScreen = false;
}

输出

var A = function() {

  var me = this;

  /**
   * [function description]
   * @param  {[type]} name [description]
   * @return {[type]}      [description]
   */
  var registerPrinter = function(name) {

    /**
     * [function description]
     * @param  {[type]} N [description]
     * @return {[type]}   [description]
     */
    me['print' + name] = function(N) {
      var i = 0;
      while (i < N) {
        console.log(name);
        i++;
      }
    };
  };

  registerPrinter('Thing');
  registerPrinter('Ball');
};

我在测试中所做的并不重要,但是,我希望所有这些都使用相同的生成import pytest from mixer.backend.django import mixer from weddings.models import Wedding @pytest.mark.django_db class TestProductApi(object): @pytest.fixture(scope="module") def wedding(self): wedding = mixer.blend( 'weddings.Wedding', ) mixer.cycle(12).blend( 'weddings.Product', wedding=wedding, ) mixer.cycle(4).blend( 'weddings.Product', wedding=wedding, is_visible=False, ) mixer.blend( 'weddings.Product', wedding=wedding, is_active=False, ) return wedding def test_number_one(self, wedding): print 'running test_number_one' print 'wedding.id == {}'.format(wedding.id) print 'Wedding.objects.all().count() == {}'.format(Wedding.objects.all().count()) print 'finished test_number_one' def test_number_two(self, wedding): print 'running test_number_two' print 'wedding.id == {}'.format(wedding.id) print 'Wedding.objects.all().count() == {}'.format(Wedding.objects.all().count()) print 'finished test_number_two' 对象运行。 当我运行测试时,每次创建测试时都会创建一个新的tests/weddings/api/test_products.pyTestProductApi.test_number_one 0% running test_number_one wedding.id == 1 Wedding.objects.all().count() == 1 finished test_number_one tests/weddings/api/test_products.pyTestProductApi.test_number_one ✓ 50% █████ tests/weddings/api/test_products.pyTestProductApi.test_number_two 50% █████ running test_number_two wedding.id == 1 Wedding.objects.all().count() == 0 finished test_number_two

以下是我使用的版本:
wedding
wedding
pytest==2.7.1
pytest-django==2.9.1
Django==1.8.8

编辑1 :我也尝试在我的灯具中添加一个示波器,但这项工作仅适用于第一次测试。如果我在第二次测试中进行查询,模型就不存在,即使夹具工作正常。

编辑2 :将mixer==5.3.1插入灯具并显示运行测试的结果。

1 个答案:

答案 0 :(得分:1)

这对我有用。试一试......

import pytest
from mixer.backend.django import mixer
from weddings.models import Wedding

@pytest.fixture(scope="class")
def wedding(self):
    wedding_obj = mixer.blend(
        'weddings.Wedding',
    ) 
    mixer.cycle(12).blend(
        'weddings.Product',
        wedding=wedding_obj,
    ) 

    mixer.cycle(4).blend(
        'weddings.Product',
        wedding=wedding_obj,
        is_visible=False,
    ) 

    mixer.blend(
        'weddings.Product',
        wedding=wedding_obj,
        is_active=False,
    ) 
    return wedding_obj

@pytest.mark.django_db
@pytest.mark.usefixtures("wedding")
class TestProductApi(object):

    def test_number_one(self, wedding):
        print 'running test_number_one'
        print 'wedding.id == {}'.format(wedding.id)
        print 'Wedding.objects.all().count() == {}'.format(Wedding.objects.all().count())
        print 'finished test_number_one'

    def test_number_two(self, wedding):
        print 'running test_number_two'
        print 'wedding.id == {}'.format(wedding.id)
        print 'Wedding.objects.all().count() == {}'.format(Wedding.objects.all().count())
        print 'finished test_number_two'