生成json测试报告
pytest-json的Python项目详细描述
pytest json是生成json的py.test的插件 测试结果报告
这个Pytest插件是用Cookiecutter和 @hackebrot的Cookiecutter-pytest-plugin模板。
要求
- python 2.7、3.3、3.4、3.5
- py.test 2.7或更新版本
用法
$ py.test --json=report.json
或者您可以在ini文件中设置报告路径:
[pytest] json_report = report.json
命令行选项将覆盖ini文件
有一个可选标志用于将生成的报告规范化为 jsonapi。这是为了 余烬数据和其他:
$ py.test --json=report.json --jsonapi
或者可以在ini文件中设置jsonapi标志:
[pytest] jsonapi = anything
注意,ini变量的值可以是任何值。只有在场才会导致 这是真的。
添加到环境中
您可以在fixture中修改request.config._json_environment。
@pytest.fixture(scope='session',autouse=True):defextra_json_environment(request):request.config._json_environment.append(('herp','derp'))
为每个测试阶段添加元数据
通过stage_metadata添加的元数据显示在 生成的json对象中的stage
# conftest.py@pytest.hookimpl(tryfirst=True,hookwrapper=True)defpytest_runtest_makereport(item,call):outcome=yieldreport=outcome.get_result()ifreport.when=='call':report.stage_metadata={'foo':'bar'}elifreport.when=='setup':report.stage_metadata={'hoof':'doof'}elifreport.when=='teardown':report.stage_metadata={'herp':'derp'}
每个测试添加元数据
添加在测试本身上的元数据由数组表示。避免 冲突等。设置^{tt4}的任何阶段(设置、拆卸、调用)$ 报表的元数据将附加到数组中。通过添加元数据 test_metadata结束于 生成的json对象
# conftest.py@pytest.hookimpl(tryfirst=True,hookwrapper=True)defpytest_runtest_makereport(item,call):outcome=yieldreport=outcome.get_result()# only add this during call instead of during any stageifreport.when=='call':report.test_metadata='whatever'
与pytest html兼容
为了避免pytest html出现问题,pytest json使用 request.config._json_environment而不是request.config._environment
此外,pytest json忽略报表上的extra字段。
示例json
输出的格式化示例可以在example.json中找到
实际输出没有格式化,但这是通过jq传递的 可读性。
jsonapi输出的格式化示例可以在example_jsonapi.json中找到
贡献
我们非常欢迎您的贡献。测试可以用tox运行,请确保 在提交请求之前,覆盖范围至少保持不变。
许可证
“pytest json”是根据MIT许可证的条款发布的免费开源软件
问题
如果您遇到任何问题,请file an issue连同详细说明。