帮我写一套企业般的Httprunner测试框架整体代码
时间: 2024-04-20 15:26:06 浏览: 85
当然,以下是一个简单的企业级Httprunner测试框架的整体代码示例:
```python
# conftest.py
import pytest
from py.xml import html
from datetime import datetime
@pytest.mark.optionalhook
def pytest_html_results_summary(prefix, summary, postfix):
prefix.extend([html.p("测试执行时间:%s" % datetime.now())])
# tests/conftest.py
import pytest
from httprunner import HttpRunner
@pytest.fixture(scope="session")
def prepare_session(request):
# 在测试会话开始前执行的操作
runner = HttpRunner()
runner.run("path/to/suite.yml")
request.config.cache.set("runner", runner)
@pytest.fixture(scope="function")
def prepare_function(request):
# 在每个测试用例执行前执行的操作
runner = request.config.cache.get("runner")
testcase = runner.testcase_parser.get_testcase(request.node.name)
testcase["variables"]["env"] = "test"
testcase["variables"]["token"] = "xxx"
runner.run_testcase(testcase)
# tests/test_example.py
def test_example(prepare_session, prepare_function):
pass
# tests/test_another_example.py
def test_another_example(prepare_session, prepare_function):
pass
# test_suite.yml
config:
name: "Example Test Suite"
variables:
- env: ""
- token: ""
testcases:
- name: "Example Test Case"
variables:
- env: "${env}"
- token: "${token}"
request:
url: "http://example.com/api/endpoint"
method: "POST"
headers:
Content-Type: "application/json"
json:
key: value
validate:
- eq: [status_code, 200]
- eq: [body.success, true]
```
以上是一个简单的企业级Httprunner测试框架的代码示例。你可以根据实际需求进行适当的修改和扩展,包括配置、测试用例、前置后置处理等部分。
阅读全文