【问题标题】:pytest fixture is not getting called in classpytest 夹具没有在课堂上被调用
【发布时间】:2017-03-20 19:27:16
【问题描述】:

我最近开始研究一个 python 项目。在那个项目中,我正在使用pytest 编写测试用例。在那,我尝试使用pytest-fixtures并理解了它的基本概念。

但在特定情况下,当我尝试在 class 中使用这些固定装置时,我发现使用 fixture 有困难。

示例代码:

import pytest

from flask.ext.testing import TestCase

from flask_application import app

@pytest.fixture(scope="module")
def some_fix():
    return "yes"

class TestDirectoryInit(TestCase):
    def create_app(self):
        return app

    def test_one(self, some_fix):
        assert some_fix == "yes"

当我运行这个测试时,它给我一个错误:

TypeError: test_one() 只接受 2 个参数(给定 1 个)

但是当我把这段代码改成这样的时候:

@pytest.fixture(scope="module")
def some_fix():
    return "yes"

class TestDirectoryInit():

    def test_one(self, some_fix):
        assert some_fix == "yes"

现在这个测试用例正在通过。我无法理解为什么它由于扩展了 TestCase 类而表现不同。

任何有用的建议将不胜感激!谢谢!

【问题讨论】:

    标签: python unit-testing pytest fixtures


    【解决方案1】:

    flask.ext.testing.TestCaseunittest.TestCase 的子类。

    如果您希望能够将 pytest 固定装置与 unittest 一起使用,请阅读以下内容:

    Mixing pytest fixtures into unittest

    【讨论】:

    • TestDirectoryInit 类中,我正在为api调用编写测试用例。为此,我使用unittest.TestCase。你能建议我其他一些不使用unittest.TestCase 的好方法吗?
    • 你可以单独使用pytest,它是unittest的替代品
    • 您能为此编写一个小示例代码吗?通过导入 Unittest.Testcase,我能够发出如下请求:self.client.post()。我们如何使用 pytest 做到这一点?
    • 对不起,我不是烧瓶用户,也不知道如何为它编写测试,但是,看看我发现了什么:pytest-flask.readthedocs.io/en/latest/features.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 2021-12-22
    • 2019-05-13
    相关资源
    最近更新 更多