2015-12-01 70 views
1

我有一个函数,我不想每次运行我的Flask-RESTFul API中的测试时都运行。这是设置的例子:使用pytest.fixture退出函数

class function(Resource): 
    def post(self): 
     print 'test' 
     do_action() 
     return {'success':True} 
在我的测试

我要运行这个功能,却忽略do_action()。我将如何使用pytest做到这一点?

+0

您是否想在某些运行中完全跳过测试。或者你是否希望通过使用'do_action()'运行,然后在没有运行的情况下运行此测试? – shuttle87

+0

我希望它完全跳过测试。 – Rob

+0

这样的事情:import pytest @ pytest.fixture(scope =“function”,autouse = True) – Rob

回答

1

这似乎是一个很好的机会,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”

+0

我想运行测试,我只是不想做do_action(),这很有趣。 – Rob

+0

@Rob每次运行测试套件时,您是否都想用'do_action()'和没有'do_action()'运行测试用例?或者如果根据调用测试套件的方式调用'do_action()',是否要切换? – shuttle87

+0

切换如何调用它。 – Rob

1

您可以在您的测试嘲笑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至 。