在py.test中的每个测试之前和之后运行代码?

edA*_*a-y 46 python pytest

我想在我的测试套件中的每个测试之前和之后运行额外的设置和拆卸检查.我看过灯具但不确定它们是否是正确的方法.我需要在每次测试之前运行设置代码,我需要在每次测试后运行拆卸检查.

我的用例是检查没有正确清理的代码:它会留下临时文件.在我的设置中,我将检查文件,在拆解时我也检查文件.如果有额外的文件,我希望测试失败.

taw*_*mas 61

py.test灯具是一种技术上足够的方法来实现您的目的.

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

@pytest.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
Run Code Online (Sandbox Code Playgroud)

通过声明您的夹具autouse=True,将为同一模块中定义的每个测试功能自动调用它.

也就是说,有一点需要注意.在设置/拆解时断言是一种有争议的做法.我的印象是py.test主要作者不喜欢它(我也不喜欢它,所以这可能会影响我自己的感知),所以你可能会遇到一些问题或粗糙的边缘.

  • 阅读@ AugustinLaville评论的人请注意:答案已经过编辑,现在是最新的 (7认同)
  • 从 pytest 3.0 开始,正式支持内部装置的屈服。https://docs.pytest.org/en/latest/yieldfixture.html (2认同)

lmi*_*asf 21

您可以使用fixturein oder 来实现您想要的。

import pytest

@pytest.fixture(autouse=True)
def run_before_and_after_tests(tmpdir):
    """Fixture to execute asserts before and after a test is run"""
    # Setup: fill with any logic you want

    yield # this is where the testing happens

    # Teardown : fill with any logic you want
Run Code Online (Sandbox Code Playgroud)

详细说明

  1. @pytest.fixture(autouse=True)来自文档“有时,您可能希望在不显式声明函数参数或 usefixtures 装饰器的情况下自动调用夹具。” 因此,每次执行测试时都会运行此夹具。

  2. # Setup: fill with any logic you want,此逻辑将在每个测试实际运行之前执行。在您的情况下,您可以添加将在实际测试之前执行的断言语句。

  3. yield,如评论中所示,这是进行测试的地方

  4. # Teardown : fill with any logic you want,此逻辑将在每次测试后执行。无论测试期间发生什么,该逻辑都保证运行。

注意:pytest执行测试时失败的测试和错误是有区别的。失败表示测试以某种方式失败。错误表示您无法进行适当的测试。

考虑以下示例:

在测试运行之前断言失败 -> 错误

import pytest


@pytest.fixture(autouse=True)
def run_around_tests():
    assert False # This will generate an error when running tests
    yield
    assert True

def test():
    assert True

Run Code Online (Sandbox Code Playgroud)

测试运行后断言失败 -> 错误

import pytest


@pytest.fixture(autouse=True)
def run_around_tests():
    assert True
    yield
    assert False

def test():
    assert True
Run Code Online (Sandbox Code Playgroud)

测试失败 -> 失败

import pytest


@pytest.fixture(autouse=True)
def run_around_tests():
    assert True
    yield
    assert True

def test():
    assert Fail

Run Code Online (Sandbox Code Playgroud)

测试通过 -> PASSED

import pytest


@pytest.fixture(autouse=True)
def run_around_tests():
    assert True
    yield
    assert True

def test():
    assert True
Run Code Online (Sandbox Code Playgroud)


Okk*_*ken 10

夹具正是您想要的.这就是他们的设计目标.

无论您使用pytest样式灯具,还是设置拆卸(模块,类或方法级别)xUnit样式灯具,都取决于环境和个人品味.

根据您的描述,您似乎可以使用pytest autouse灯具.
或xUnit样式级别setup_function()/ teardown_function().

Pytest让你完全覆盖.这么多,或许这是一个信息消防软管.


Rob*_*rto 5

您可以使用装饰器,但可以通过编程方式使用,因此无需在每种方法中都放置装饰器。

我在下一个代码中假设了几件事:

测试方法都被命名为:“ 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])
Run Code Online (Sandbox Code Playgroud)

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

test1()
test2()
Run Code Online (Sandbox Code Playgroud)

输出应该是这样的:

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>
Run Code Online (Sandbox Code Playgroud)

如果将测试方法作为类方法,则该方法也有效。例如:

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)))
Run Code Online (Sandbox Code Playgroud)

呼叫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'>>
Run Code Online (Sandbox Code Playgroud)


Arv*_*ind 5

您可以使用Pytest的模块级设置/拆卸夹具.

这是链接

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
Run Code Online (Sandbox Code Playgroud)

它将setup_module在此测试之前和teardown_module测试完成后调用.

您可以在每个测试脚本中包含此fixture,以便为每个测试运行它.

如果你想使用目录中所有测试通用的东西你可以使用包/目录级别的fixtures鼻子框架

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 '''
Run Code Online (Sandbox Code Playgroud)