Python 如何在所有测试和拆卸完成后调用一次安装程序

Python 如何在所有测试和拆卸完成后调用一次安装程序,python,unit-testing,pytest,Python,Unit Testing,Pytest,我用pytest编写了一系列测试。目录dir下都有。例如: dir/test_base.py dir/test_something.py dir/test_something2.py ... 其中代码的简化版本如下: test_base.py import pytest class TestBase: def setup_module(module): assert False def teardown_module(module): assert Fa

我用pytest编写了一系列测试。目录
dir
下都有。例如:

dir/test_base.py
dir/test_something.py
dir/test_something2.py
...
其中代码的简化版本如下:

test_base.py

import pytest

class TestBase:
   def setup_module(module):
      assert False

   def teardown_module(module):
      assert False
import pytest
from test_base import TestBase

class TestSomething(TestBase):
   def test_dummy():
       pass
import pytest
from test_base import TestBase

class TestSomethingElse(TestBase):
   def test_dummy2():
       pass
test\u something.py

import pytest

class TestBase:
   def setup_module(module):
      assert False

   def teardown_module(module):
      assert False
import pytest
from test_base import TestBase

class TestSomething(TestBase):
   def test_dummy():
       pass
import pytest
from test_base import TestBase

class TestSomethingElse(TestBase):
   def test_dummy2():
       pass
test\u something2.py

import pytest

class TestBase:
   def setup_module(module):
      assert False

   def teardown_module(module):
      assert False
import pytest
from test_base import TestBase

class TestSomething(TestBase):
   def test_dummy():
       pass
import pytest
from test_base import TestBase

class TestSomethingElse(TestBase):
   def test_dummy2():
       pass
我所有的
test\u something*.py
文件都扩展了
test\u base.py
中的基类。现在我在
test\u base.py
中编写了
setup\u模块(module)
teardown\u模块(module)
方法。我希望在所有测试中调用一次setup_模块,并在所有测试完成后在最后调用
teardown_模块()


但是函数似乎没有被调用?我需要一些修饰符才能工作吗?

首先,将所有测试放在一个名为“测试”的模块中是一个很好的做法:


更多信息:

为定义最终(派生)测试的模块调用设置模块/拆卸模块。这也允许自定义设置。如果您只有一个设置模块,可以将其放入test_base.py并从其他位置导入。HTH.

设置模块
拆卸模块
放在模块级的类外。然后在测试中添加类

def setup_module(module):
    """..."""

def teardown_module(module):
    """..."""

class TestSomething:

    def test_dummy(self):
        """do some tests"""

有关更多信息,请参阅。

OP的要求是设置和拆卸每个模块时只执行一次,而不是每个模块执行一次。这可以通过组合使用
conftest.py
文件、
@pytest.fixture(scope=“session”)
并将fixture名称传递给每个测试函数来实现

这些将在中描述

下面是一个例子:

conftest.py

import pytest

@pytest.fixture(scope="session")
    def my_setup(request):
        print '\nDoing setup'
        def fin():
            print ("\nDoing teardown")
        request.addfinalizer(fin)
def test_dummy(my_setup):
    print '\ntest_dummy'
def test_dummy2(my_setup):
    print '\ntest_dummy2'

def test_dummy3(my_setup):
    print '\ntest_dummy3'
测试某物.py

import pytest

@pytest.fixture(scope="session")
    def my_setup(request):
        print '\nDoing setup'
        def fin():
            print ("\nDoing teardown")
        request.addfinalizer(fin)
def test_dummy(my_setup):
    print '\ntest_dummy'
def test_dummy2(my_setup):
    print '\ntest_dummy2'

def test_dummy3(my_setup):
    print '\ntest_dummy3'
测试一些东西2.py

import pytest

@pytest.fixture(scope="session")
    def my_setup(request):
        print '\nDoing setup'
        def fin():
            print ("\nDoing teardown")
        request.addfinalizer(fin)
def test_dummy(my_setup):
    print '\ntest_dummy'
def test_dummy2(my_setup):
    print '\ntest_dummy2'

def test_dummy3(my_setup):
    print '\ntest_dummy3'
运行py.test-s时的输出:

collected 3 items 

test_something.py 
Doing setup

test_dummy
.
test_something2.py 
test_dummy2
.
test_dummy3
.
Doing teardown
名称
conftest.py
很重要:你不能给这个文件一个不同的名称,并期望Pytest将它作为一个fixture源来查找

设置
scope=“session”
很重要。否则,将对每个测试模块重复设置和拆卸


如果您不想将fixture name my_setup作为参数传递给每个测试函数,您可以将测试函数放在一个类中,并将
pytest.mark.usefixtures
decorator应用于该类。

这个答案是否有理由不使用文档定义的模块级设置/拆卸来解决他的问题。您是否建议
设置\模块(module)
/
拆卸\模块(module)
功能不按文档说明的那样工作?我只是问您是否暗示那些文档说明的方法不工作,正如您在init中回答的那样。就这些。因为即使在你的链接中,你也引用了解释如何使用这些方法的文档,因为OPHi@GiacomoSpettoli已经被破坏了,谢谢你的回复。我实际上是想让setup_模块/teardown模块工作,而不是setup_类/teardown类。因为,我希望对所有测试只调用一次。将为派生基类的每个测试调用setup\u class/teardown\u类。HTHI-see…所以您应该尝试将这些方法放在_u__; init _; py.py中。这是我的第一个答案,但我改变了它,因为在没有最后一个约束的情况下,类方法的使用看起来更干净了。这就是你的意思吗?因为,这是行不通的…设置模块需要在模块级别定义,而不是在类级别。hi@hpk42这到底意味着什么?对不起,我是一个PythonNoob。“模块级”是指文件中的缩进级别0(与定义类的级别相同)。您当前在类定义中定义了设置模块。
conftest.py
文件中是否允许这些设置模块?