PyTest:Django事务提交失败

时间:2017-02-06 12:07:26

标签: python django python-2.7 sqlalchemy pytest

我正在使用Pytest在我的django项目中实现单元测试,该项目将MySql作为后端。

结合这些,我正在利用SQLAlchemy进行数据生成。

我有一个python函数 call_my_flow(),根据条件执行两个不同的流程。第一个流使用sqlalchemy连接,第二个流使用django连接进行数据库插入。

我已经使用pytest编写了两个单元测试来检查两个流程。

第一个流程(使用sqlalchemy连接):按照预期在数据库和pytest中运行流程事务处理。

第二个流(使用django数据库连接):事务提交失败,从而导致测试失败。

演示代码:

import pytest
from myflow import call_my_flow

@pytest.fixture(scope="class")
@pytest.mark.django_db(transaction=False)
def setup_my_flow():
    call_my_flow()    

@pytest.mark.usefixtures("setup_my_flow")
class TestGenerateOrder(object):
    @pytest.fixture(autouse=True)
    def setuporder(self):
        self.first_count = 2
        self.second_count = 5    

    @pytest.mark.order1
    @pytest.mark.django_db
    def test_first_flow_count(self):
        db_count = get_first_count()
        assert db_count == self.first_count    

    @pytest.mark.order2
    @pytest.mark.django_db
    def test_second_flow_count(self):
        db_count = get_second_count()
        assert db_count == self.second_count

请建议同样的解决方案。

0 个答案:

没有答案
相关问题