python setup和teardown的使用

说明

1、setup/teardown:每种方法将执行一次,无论是类内还是类外。

2、Setup:方法运行前执行,表示前置条件。

必须在每个用例执行前执行一次。

3、Teardown:方法运行后才能执行,表示资源释放。

每次用例执行后都会执行一次。

实例

#file_name:test_setup.py
importpytest
defsetup():
print("...类外setup...")
deftest_create():
print("类外test_create")
deftest_view():
print("类外test_view")
classTestSetupClass:
defsetup(self):
print("...类内setup...")
deftest_create(self):
print("类内test_create")
deftest_view(self):
print("类内test_view")
defteardown(self):
print("...类内teardown...")
defteardown():
print("...类外teardown...")
if__name__=='__main__':
pytest.main(['-vs','test_setup.py'])

以上就是python setup和teardown的使用,希望对大家有所帮助。更多Python学习指路:Python基础教程

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。