【发布时间】: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