近期重拾Python | Pytest测试框架,基于前人的轮子,学习一下pytest测试框架运行测试用例的灵活之处
fixture修饰器来标记固定的工厂函数,在其他函数,模块,类或整个工程调用它时会被激活并优先执行,通常会被用于完成预置处理和重复操作
在引用fixture是,可以配置对应方法参数,将公共配置,灵活的应用到对应待执行或不执行的testcase,在调用方面更加便捷灵活
fixture(scope="function", params=None, autouse=False, ids=None, name=None)
scope:可以理解成fixture的作用域,默认:function,还有class、module、package、session四个【常用】
scope="function" 当fixture没有参数时,默认scope="function"此时该fixture只对函数有效,毕竟实在每条case前都会执行
scope="module" 可以实现多个.py 跨文件共享前置,module 作用是整个.py 文件都会生效,在执行所有的case前只执行
scope="session" 可以实现多个.py 跨文件使用一个 session 来完成多个用例 autouse 默认:False,需要用例手动调用该fixture;手动调用方式为:用例调用时,参数写上函数名称就行;如果是True,所有作用域内的测试用例都会自动调用
@pytest.fixture() 默认参数为空,定义一个login函数,我们可以在testcase001、testcase003进行引用login函数看看效果
import pytest
@pytest.fixture()
def login():
print("\n登录请求,传入用户、密码参数")
def testcase_001(login):
print("\n先执行login函数,然后在执行testcase_001")
def testcase_002():
print("\n不执行login函数,直接执行testcase_002")
def testcase_003(login):
print("\n执行login函数,直接执行testcase_003")
if __name__ == '__main__':
pytest.main(["-s", "test_fix.py"])
控制台输出结果顺序如下
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- F:\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: F:\Python\Python38-32\python_code\Study\Python_Pytest\test_fixture
collecting ... collected 3 items
test_fix.py::testcase_001
登录请求,传入用户、密码参数
PASSED [ 33%]
先执行login函数,然后在执行testcase_001
test_fix.py::testcase_002 PASSED [ 66%]
不执行login函数,直接执行testcase_002
test_fix.py::testcase_003
登录请求,传入用户、密码参数
PASSED [100%]
执行login函数,直接执行testcase_003
============================== 3 passed in 0.02s ==============================
Process finished with exit code 0
剖析:@pytest.fixture(scope="module", autouse=True)
scope="module" 作用域所有测试用例前后,只执行一次
autouse=True 默认自动执行
剖析:@pytest.fixture(scope="function", autouse=False)
scope="function" 作用于每条测试用例前后,每条测试用例都得执行一次
autouse=False 手动调动执行,直接在测试用例引用该函数
# coding=utf-8
# authou:shichao
import pytest
from selenium import webdriver
import time
driver = webdriver.Chrome()
@pytest.fixture(scope="module", autouse=True)
def setup_class():
print("\n>>>scope=”module“类型为module针对全局文件有效, autouse=True,状态为开启,自动调用方式,执行setup;例如:打开浏览器,并且打开百度首页")
driver.maximize_window()
time.sleep(1)
URL = "https://www.baidu.com/"
driver.get(URL)
# 用yield关键字呼唤teardown操作
yield
print("\n>>>scope=”module“类型为module针对全局文件有效, autouse=True,状态为开启,自动调用方式,执行teardown;例如:最后关闭浏览器")
driver.quit()
@pytest.fixture(scope="function", autouse=False)
def setup_funtion():
print("\n>>>scope=”function“类型为function只针对函数有效, autouse=False,为关闭状态,手动调用方式;例如做:点击首页操作等")
time.sleep(2)
def testcase001():
print("\n>>>执行测试用例1")
driver.find_element_by_id("kw").send_keys(u"软件测试")
driver.find_element_by_xpath('//*[@id="su"]').click()
def testcase002(setup_funtion):
print("\n>>>执行测试用例2")
if __name__ == '__main__':
pytest.main(["-s", "test_fix_yield.py"])
控制台输出执行顺序如下
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- F:\Python\Python38-32\python.exe
cachedir: .pytest_cache
rootdir: F:\Python\Python38-32\python_code\Study\Python_Pytest\test_fixture
collecting ... collected 2 items
test_fix_yield.py::testcase001
>>>scope=”module“类型为module针对全局文件有效, autouse=True,状态为开启,自动调用方式,执行setup;例如:打开浏览器,并且打开百度首页
PASSED [ 50%]
>>>执行测试用例1
test_fix_yield.py::testcase002
>>>scope=”function“类型为function只针对函数有效, autouse=False,为关闭状态,手动调用方式;例如做:点击首页操作等
PASSED [100%]
>>>执行测试用例2
>>>scope=”module“类型为module针对全局文件有效, autouse=True,状态为开启,自动调用方式,执行teardown;例如:最后关闭浏览器
============================= 2 passed in 15.60s ==============================
Process finished with exit code 0
小小总结:autouse=True时不管你测试用例有没有引用对应的fixture函数,文件中对应的fixture会根据scope="module"或者scope="function"都会在每条测试用例前进行执行对应的前置脚本
autouse=False时像scope="function"函数类型的前置,你可以自定义决定哪条case执行前需要去引用该函数前置,就可以达到自定义配置
通过以上实例的调用方式以及执行顺序,大家看懂了吗?
1.测试文件的文件名必须以"test_"开头,或者以"_test"结尾
2.测试类命名必须以"Test"开头
3.测试函数名必须以"test"开头
4.测试类里面不能使用"__init__"方法