我有一个函数,我不想每次运行我的Flask-RESTFul API中的测试时都运行。这是设置的例子:使用pytest.fixture退出函数
class function(Resource):
def post(self):
print 'test'
do_action()
return {'success':True}
在我的测试
我要运行这个功能,却忽略do_action()。我将如何使用pytest做到这一点?
我有一个函数,我不想每次运行我的Flask-RESTFul API中的测试时都运行。这是设置的例子:使用pytest.fixture退出函数
class function(Resource):
def post(self):
print 'test'
do_action()
return {'success':True}
在我的测试
我要运行这个功能,却忽略do_action()。我将如何使用pytest做到这一点?
这似乎是一个很好的机会,mark测试
@pytest.mark.foo_test
class function(Resource):
def post(self):
print 'test'
do_action()
return {'success':True}
然后,如果你有
py.test -v -m foo_test
它只能运行这些测试标有“foo_test”
如果你打电话叫与
py.test -v -m "not foo_test"
它将运行没有标记为“foo_test”
您可以在您的测试嘲笑do_action
所有测试:
def test_post(resource, mocker):
m = mocker.patch.object(module_with_do_action, 'do_action')
resource.post()
assert m.call_count == 1
所以实际的功能不会在这个测试中被调用,用额外的好处 你可以检查post
实现是否正在调用该函数。
这需要安装(无耻插头)pytest-mocker至 。
您是否想在某些运行中完全跳过测试。或者你是否希望通过使用'do_action()'运行,然后在没有运行的情况下运行此测试? – shuttle87
我希望它完全跳过测试。 – Rob
这样的事情:import pytest @ pytest.fixture(scope =“function”,autouse = True) – Rob