2014-03-25 111 views
22

我想在测试套件中的每个测试之前和之后运行额外的安装和拆卸检查。我看过灯具,但不知道他们是否是正确的方法。我需要在每次测试之前运行设置代码,并且我需要在每次测试之后运行拆卸检查。在py.test的每个测试之前和之后运行代码?

我的用例是检查没有正确清理的代码:它会留下临时文件。在我的设置中,我将检查这些文件,并在拆解过程中检查这些文件。如果有额外的文件,我想测试失败。

回答

3

您可以使用的装饰,但编程,所以你不需要把装饰每种方法。

我假设几件事情在下面的代码:

的测试方法都命名,如:“的testXXX()” 装饰器被添加到测试方法都实现了相同的模块。

def test1(): 
    print ("Testing hello world") 

def test2(): 
    print ("Testing hello world 2") 

#This is the decorator 
class TestChecker(object): 
    def __init__(self, testfn, *args, **kwargs): 
     self.testfn = testfn 

    def pretest(self): 
     print ('precheck %s' % str(self.testfn)) 
    def posttest(self): 
     print ('postcheck %s' % str(self.testfn)) 
    def __call__(self): 
     self.pretest() 
     self.testfn() 
     self.posttest() 


for fn in dir() : 
    if fn.startswith('test'): 
     locals()[fn] = TestChecker(locals()[fn]) 

现在,如果你调用的测试方法...

test1() 
test2() 

输出应该是这样的:

precheck <function test1 at 0x10078cc20> 
Testing hello world 
postcheck <function test1 at 0x10078cc20> 
precheck <function test2 at 0x10078ccb0> 
Testing hello world 2 
postcheck <function test2 at 0x10078ccb0> 

如果你有测试方法类的方法,该方法是也有效。例如:

class TestClass(object): 
    @classmethod 
    def my_test(cls): 
     print ("Testing from class method") 

for fn in dir(TestClass) : 
    if not fn.startswith('__'): 
     setattr(TestClass, fn, TestChecker(getattr(TestClass, fn))) 

TestClass.my_test()呼叫将打印:

precheck <bound method type.my_test of <class '__main__.TestClass'>> 
Testing from class method 
postcheck <bound method type.my_test of <class '__main__.TestClass'>> 
+0

这看起来好像可能适用于免费的功能。我也有班级fucntions(虽然我试图摆脱所有的测试类)。 –

+0

它也适用于类方法,我已经更新了我的答案。 – Roberto

18

py.test灯具要实现你的目的技术上足够的方法。

你只需要定义一个这样的夹具:

@pytest.yield_fixture(autouse=True) 
def run_around_tests(): 
    # Code that will run before your test, for example: 
    files_before = # ... do something to check the existing files 
    # A test function will be run at this point 
    yield 
    # Code that will run after your test, for example: 
    files_after = # ... do something to check the existing files 
    assert files_before == files_after 

通过与autouse=True声明你的灯具,它会自动调用了同样的模块定义的测试功能。

这就是说,有一个警告。断言设置/拆卸是一个有争议的做法。我的印象是,py.test的主要作者不喜欢它(我也不喜欢它,所以这可能会使我自己的感觉变成颜色),所以当你前进时,你可能遇到一些问题或粗糙的边缘。

+1

注意:此语法自3.0起折旧。使用'@ pytest.fixture'。 –

+0

这里是与上一条评论相关的[链接](https://docs.pytest.org/en/latest/fixture.html#fixture-finalization-executing-teardown-code)。 – Paolo

4

您可以使用Pytest的Module level setup/teardown Fixtures。

这里的链接

http://pytest.org/latest/xunit_setup.html

其工作原理如下:

def setup_module(module): 
    """ setup any state specific to the execution of the given module.""" 

def teardown_module(module): 
    """ teardown any state that was previously setup with a setup_module 
    method.""" 

Test_Class(): 
     def test_01(): 
      #test 1 Code 

它将调用setup_module测试前和测试teardown_module完成后。

您可以在每个测试脚本中包含此夹具,以便为每个测试运行该夹具。

如果你想使用的东西是共同的所有测试目录中您可以使用包/目录级别赛事的鼻子框架

http://pythontesting.net/framework/nose/nose-fixture-reference/#package

__init__.py文件的包,你可以包括以下

 def setup_package(): 
     '''Set up your environment for test package''' 

    def teardown_package(): 
     '''revert the state ''' 
7

灯具正是你想要的。 这就是他们的设计目的。

无论您使用pytest风格灯具,或设置拆卸(模块,类或方法级别)的xUnit风格的灯具,依赖于环境和个人喜好。

从您所描述的内容来看,您似乎可以使用pytest autouse fixtures
或xUnit风格功能级别setup_function()/teardown_function()

Pytest已经完全覆盖。如此之多,以至于它可能是信息的消防软管。

相关问题