一个与pytest一起使用的简单插件
pytest-elk-reporter的Python项目详细描述
Pytest麋鹿报告器
向ELK堆栈发送pytest测试结果的插件,带有额外的上下文数据
功能
- 在测试完成时,将每个测试结果报告到elasticsearch中
- 自动将上下文数据附加到每个测试:
- Git信息,如
branch
或last commit
等 - 所有詹金斯环境变量
- 用户名(如果可用)
- Git信息,如
- 使用所有上下文数据向Elastic报告每个会话的测试摘要
- 可以将任何用户数据附加到发送到Elastic的上下文中
要求
- 编写pytest测试
安装
您可以通过PyPI中的pip安装“pytest elk reporter”
pip install pytest-elk-reporter
弹性搜索配置
我们需要为即将使用的索引启用自动创建索引, 由于我们没有创建索引的代码,因此这是默认值
curl -X PUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d'{ "persistent": { "action.auto_create_index": "true" }}'
有关此ElasticSearch功能的详细信息,请检查index documention
用法
从命令行运行和配置
pytest --es-address 127.0.0.1:9200 # or if you need user/password to authenticate pytest --es-address my-elk-server.io:9200 --es-username fruch --es-password 'passwordsarenicetohave'
从代码配置
importpytest@pytest.fixture(scope='session',autouse=True)defconfigure_es(elk_reporter):# TODO: get cerdentials in more secure fashion programtically, maybe AWS secrects or the likes# or put them in plain-text in the code... what can ever go wrong...elk_reporter.es_address="my-elk-server.io:9200"elk_reporter.es_user='fruch'elk_reporter.es_password='passwordsarenicetohave'elk_reporter.es_index_name='test_data'
从pytest ini文件配置
# put this in pytest.ini / tox.ini / setup.cfg[pytest]es_address=my-elk-server.io:9200es_user=fruches_password=passwordsarenicetohavees_index_name=test_data
见pytest docs 有关如何使用.ini文件配置的详细信息
为整个会话收集上下文数据
例如,有了它,我将能够为每个版本构建一个仪表板
@pytest.fixture(scope="session",autouse=True)defreport_formal_version_to_elk(request):""" Append my own data specific, for example which of the code uner test is used """# TODO: take it programticly of of the code under test...my_data={"formal_version":"1.0.0-rc2"}elk=request.config.pluginmanager.get_plugin("elk-reporter-runtime")elk.session_data.update(**my_data)
收集特定测试的数据
deftest_my_service_and_collect_timings(request,elk_reporter):response=requests.get("http://my-server.io/api/do_something")assertresponse.status_code==200elk_reporter.append_test_data(request,{"do_something_response_time":response.elapsed.total_seconds()})# now doing response time per version dashboard quite easy# and yeah, it's not exactly real usable metric, it's just an example...
贡献
我们非常欢迎您的贡献。测试可以用^{
许可证
“pytest elk reporter”是根据MIT许可证的条款发布的免费开源软件
问题
如果您遇到任何问题,请file an issue连同详细说明。
谢谢
这个pytest插件是用Cookiecutter和@hackebrot的cookiecutter-pytest-plugin模板生成的。