如何在每个测试用例中运行静态方法

2024-10-03 00:30:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个清理数据库和报表的功能。我希望每次运行测试(问题)时都运行这个函数,不管是运行一个测试(类中的方法)还是整个类。你知道吗

我的脚本是静态方法

@staticmethod
def startScript():
     removeData()
     removeReport()

My example test:

test.py
class myTesting(unittest.TestCase):
def test_add_new_user(self, name, elem1, elem2):
code my test.

如何在每次运行测试时添加一个脚本来运行它?谢谢你的帮助


Tags: 方法函数pytest功能脚本数据库报表
1条回答
网友
1楼 · 发布于 2024-10-03 00:30:55

将其作为固定装置添加到函数中

https://docs.pytest.org/en/latest/fixture.html

@pytest.fixture
def startScript():
     removeData()
     removeReport()

或将其添加到类中的setup\u方法中:

def setup_method(self, method):
    startScript();

第三种方式: 将静态函数添加到确认测试.py在作用域中,此文件应与测试.py地址:

@pytest.fixture(autouse=True, scope='function'):
def startScript():
    removeData()
    removeReport()

更多信息确认测试.py地址:

https://docs.pytest.org/en/2.7.3/plugins.html?highlight=re

相关问题 更多 >