相信用过robotframework或者python内置的单元测试框架unittest,应该就对setup和teardown不陌生了吧,一般他们的使用场景是什么呢?
    setup:一般用于执行用例前的准备工作,就像你要跟你女朋友求婚,你总不能空着手就去求吧
    teardown:一般用于执行完用例的”清洗”工作,例如求完婚,现场总要有人收拾吧!

    在pytest中,有模块级别的、类级的、方法级的、函数级的
    模块级(setup_module\teardown_module):对模块开始、结束生效
    类级(setup_class\teardown_class):对类中开始、结束生效
    方法级(setup_method\teardown_method):对类中方法的开始、结束生效
    函数级(setup_function\teardown_function):对函数开始、结束生效,注意不在类中!

    实例一:函数型

    1. import pytest
    2. def setup_module():
    3. print("setup_module")
    4. def teardown_module():
    5. print("teardown_module")
    6. def setup_function():
    7. print("setup_function")
    8. def teardown_function():
    9. print("teardown_function")
    10. def setup():
    11. print("setup")
    12. def teardown():
    13. print("teardown")
    14. def test_one():
    15. print("test_one")
    16. ——————————————————————————————————————————————————————————
    17. test_setup_and_teardowm.py
    18. #setup_module
    19. #setup_function
    20. #setup
    21. #test_one
    22. #.teardown
    23. #teardown_function
    24. #teardown_module

    上文代码可见:module级别>function级别>用例级别(setup)

    实例二:类+方法型

    1. class Testtwo:
    2. @classmethod
    3. def setup_class(cls):
    4. print("setup_class")
    5. @classmethod
    6. def teardown_class(cls):
    7. print("teardown_class")
    8. def setup_method(self):
    9. print("setup_method")
    10. def teardown_method(self):
    11. print("teardown_method")
    12. def setup(self):
    13. print("setup")
    14. def teardown(self):
    15. print("teardown")
    16. def test_two(self):
    17. print("test_two")
    18. ---------------------------------
    19. test_setup_and_teardowm.py
    20. #setup_class
    21. #setup_method
    22. #setup
    23. #test_two
    24. #.teardown
    25. #teardown_method
    26. #teardown_class

    可见在类中,类级>方法级>用例级