桂林市电力建设公司网站,野望王绩翻译,如何搭建自己得网站,住房公积金个体工商户前言
request 是 pytest 的内置 fixture #xff0c; 为请求对象提供对请求测试上下文的访问权#xff0c;并且在fixture被间接参数化的情况下具有可选的“param”属性。这是官方文档对request的描述#xff0c;可参考的文档不多。 一、FixtureRequest
Fixtur…前言
request 是 pytest 的内置 fixture 为请求对象提供对请求测试上下文的访问权并且在fixture被间接参数化的情况下具有可选的“param”属性。这是官方文档对request的描述可参考的文档不多。 一、FixtureRequest
FixtureRequest 是来自 fixture 或者 测试用例的请求它有访问测试上下文的权限, FixtureRequest_pytest.fixtures pytest documentation。
class FixtureRequest请求对象提供对请求的测试上下文的访问并且具有可选的 param 属性以防设备被间接参数化。fixturename正在为其执行此请求的 fixture 名称。scope作用域字符串“function”、“class”、“module”、“session”之一。fixturenames此请求中所有活动状态的 fixture 的名称。node基础集合节点取决于当前请求范围。config与此请求关联的 pytest 配置对象。function如果请求具有每个函数范围则测试函数对象。cls类可以是None其中收集了测试函数。instance在其上收集测试函数的实例可以是None。module收集测试函数的Python模块对象。fspath收集此测试的测试模块的文件系统路径。keywords基础节点的关键字/标记词典。sessionPytest会话对象。addfinalizer(finalizer: 添加finalizer/teardown函数以便在请求的测试上下文中的最后一个测试完成执行后调用。applymarker(marker)对单个测试函数调用应用标记。
如果不希望在所有函数调用上都有关键字/标记则此方法非常有用。
参数
marker -- A _pytest.mark.MarkDecorator 调用创建的对象 pytest.mark.NAME(...) .raiseerror(msg: Optional[str]) 使用给定的消息引发FixtureLookupError。getfixturevalue(argname: str) 动态运行命名的fixture函数。
如果可能建议通过函数参数声明fixtures。但是如果您只能在测试设置时决定是否使用另一个fixture那么您可以使用此函数在fixture或测试函数体中检索它。引发pytest.FixtureLookupError -- 如果找不到给定的固定装置。 折叠 二、request.param
前面讲fixture参数化的时候有接触到 request.param 用于获取测试的请求参数以下示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import pytest # 测试数据 test_data [user1, user2] pytest.fixture(paramstest_data) def register_users(request): # 获取当前的测试数据 user request.param print(\n拿着这个账号去注册%s%user) result success return user, result def test_register(register_users): user, result register_users print(在测试用例里面里面获取到当前测试数据%s%user) print(result) assert result success
此案例里面我们可以在fixture参数化的时候通过request.param获取到测试的请求参数但是在用例里面用 request.param 却不能获取到测试的请求参数 1 2 def test_register_x(register_users, request): print(request.param)
这样运行会抛异常FixtureRequest object has no attribute param 1 2 3 4 5 6 7 8 9 10 #拿着这个账号去注册user1 F register_users (user1, success) request FixtureRequest for Function test_register_x[user1] def test_register_x(register_users, request): print(request.param) E AttributeError: FixtureRequest object has no attribute param D:\test_x7.py:27: AttributeError 三、request.config
request.config 是获取测试的配置文件参数这个在前面讲命令行参数的时候有用到过.
在 conftest.py 写一个 hook函数 pytest_addoption 的作用是用于获取命令行参数request.config 用于读取测试的配置数据 1 2 3 4 5 6 7 8 import pytest def pytest_addoption(parser): parser.addoption( --cmdopt, actionstore, defaulttype1, helpmy option: type1 or type2 ) pytest.fixture def cmdopt(request): return request.config.getoption(--cmdopt)
于是在测试用例里面可以通过 request.config 来获取到配置参数也可以通过自己定义的 cmdopt 来获取。 1 2 3 4 5 6 import pytest def test_answer_1(request): type request.config.getoption(--cmdopt) print(获取到命令行参数%s % type) def test_answer_2(cmdopt): print(获取到命令行参数%s % cmdopt) 四、request.module
fixture 函数可以通过接受 request 对象来反向获取请求中的测试函数、类或模块上下文进一步扩展之前的 smtp fixture示例让我们从fixture的测试模块读取可选的服务器URL 这是官方文档的一个示例 1 2 3 4 5 6 7 8 9 # conftest.py pytest.fixture(scopemodule) def smtp(request): server getattr(request.module, smtpserver, smtp.qq.com) print(fixture 获取到的server :%s %server) smtp smtplib.SMTP(server, 587, timeout5) yield smtp print(完成 %s (%s) % (smtp, server)) smtp.close()
我们使用request.module属性来从测试模块中选择性地获取smtpserver属性 快速创建另一个测试模块在其模块名称空间中实际设置服务器URL新建一个test_anothersmtp.py文件输入以下代码 1 2 3 4 5 # test_anothersmtp.py smtpserver mail.python.org def test_showhelo(smtp): print(case showhelo)
这时候运行用例会获取到 test_anothersmtp.py 里面定义的 smtpserver 1 2 3 4 5 6 7 8 9 10 11 test session starts platform win32 -- Python 3.6.6, pytest-4.5.0, py-1.9.0, pluggy-0.13.1 Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucketbucket_type rootdir: D:\ rerunfailures-9.1, xdist-2.1.0 collected 1 item ..\..\..\..\module2\test_anothersmtp.py fixture 获取到的server :mail.python.org case showhelo .完成 smtplib.SMTP object at 0x000001D00754CB00 (mail.python.org) 1 passed in 0.64 seconds
用例里面没定义 smtpserver 的话会用默认属性 smtp.qq.com 五、request的相关成员对象
在conftest.py 写一个fixture 可以获取到request的一些成员对象相关信息 1 2 3 4 5 6 7 8 9 10 11 12 13 # conftest.py pytest.fixture(autouseTrue) def print_request(request): print(\nrequest start) print(request.module) print(request.function) print(request.cls) print(request.fspath) print(request.fixturenames) print(request.fixturename) print(request.scope) print(\nrequest end)
使用命令行pytest -s text_x.py运行用例,会看到打印的结果 test_1.py request start module web.cases.module2.test_1 from D:\\web\\cases\\module2\\test_1.py function test_answer_1 at 0x0000012D1C9FD9D8 None D:\web\cases\module2\test_1.py [_verify_url, base_url, __pytest_repeat_step_number, show_request, request] show_request function request end 获取到命令行参数type1 . request start module web.cases.module2.test_1 from D:\\web\\cases\\module2\\test_1.py function test_answer_2 at 0x0000012D1C9FD730 None D:\web\cases\module2\test_1.py [_verify_url, base_url, __pytest_repeat_step_number, show_request, cmdopt, request] show_request function request end 在打印测试用例的详细日志的时候还是很有用的。
总结
感谢每一个认真阅读我文章的人
作为一位过来人也是希望大家少走一些弯路如果你不想再体验一次学习时找不到资料没人解答问题坚持几天便放弃的感受的话在这里我给大家分享一些自动化测试的学习资源希望能给你前进的路上带来帮助。 文档获取方式 加入我的软件测试交流群1007119548免费获取~同行大佬一起学术交流每晚都有大佬直播分享技术知识点
这份文档对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库这个仓库也陪伴我走过了最艰难的路程希望也能帮助到你 以上均可以分享只需要你搜索vx公众号程序员雨果即可免费领取