如何为基于 http 的集成测试生成覆盖率报告? [英] How to generate coverage report for http based integration tests?
问题描述
我正在为一个项目编写集成测试,在该项目中我进行 HTTP 调用并测试它们是否成功.
I am writing integration tests for a project in which I am making HTTP calls and testing whether they were successful or not.
因为我没有导入任何模块,也没有直接调用函数,coverage.py 报告为 0%.
Since I am not importing any module and not calling functions directly coverage.py report for this is 0%.
我想知道如何为此类集成 HTTP 请求测试生成覆盖率报告?
I want to know how can I generate coverage report for such integration HTTP request tests?
推荐答案
配方大致如下:
- 确保后端以代码覆盖模式启动
- 运行测试
- 确保将后端覆盖率写入文件
- 从文件中读取覆盖率并将其附加到测试运行覆盖率
示例:
假设您有一个虚拟后端服务器,它在 GET 请求上以Hello World"页面进行响应:
Imagine you have a dummy backend server that responds with a "Hello World" page on GET requests:
# backend.py
from http.server import BaseHTTPRequestHandler, HTTPServer
class DummyHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write('<html><body><h1>Hello World</h1></body></html>'.encode())
if __name__ == '__main__':
HTTPServer(('127.0.0.1', 8000), DummyHandler).serve_forever()
测试
发出 HTTP 请求并验证响应是否包含Hello World"的简单测试:
test
A simple test that makes an HTTP request and verifies the response contains "Hello World":
# tests/test_server.py
import requests
def test_GET():
resp = requests.get('http://127.0.0.1:8000')
resp.raise_for_status()
assert 'Hello World' in resp.text
食谱
# tests/conftest.py
import os
import signal
import subprocess
import time
import coverage.data
import pytest
@pytest.fixture(autouse=True)
def run_backend(cov):
# 1.
env = os.environ.copy()
env['COVERAGE_FILE'] = '.coverage.backend'
serverproc = subprocess.Popen(['coverage', 'run', 'backend.py'], env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
preexec_fn=os.setsid)
time.sleep(3)
yield # 2.
# 3.
serverproc.send_signal(signal.SIGINT)
time.sleep(1)
# 4.
backendcov = coverage.data.CoverageData()
with open('.coverage.backend') as fp:
backendcov.read_fileobj(fp)
cov.data.update(backendcov)
cov
是 pytest-cov
(文档).
运行测试会将 backend.py
的覆盖率添加到整体覆盖率中,尽管仅选择了 tests
:
Running the test adds the coverage of backend.py
to the overall coverage, although only tests
selected:
$ pytest --cov=tests --cov-report term -vs
=============================== test session starts ===============================
platform linux -- Python 3.6.5, pytest-3.4.1, py-1.5.3, pluggy-0.6.0 --
/data/gentoo64/usr/bin/python3.6
cachedir: .pytest_cache
rootdir: /data/gentoo64/home/u0_a82/projects/stackoverflow/so-50689940, inifile:
plugins: mock-1.6.3, cov-2.5.1
collected 1 item
tests/test_server.py::test_GET PASSED
----------- coverage: platform linux, python 3.6.5-final-0 -----------
Name Stmts Miss Cover
------------------------------------------
backend.py 12 0 100%
tests/conftest.py 18 0 100%
tests/test_server.py 5 0 100%
------------------------------------------
TOTAL 35 0 100%
============================ 1 passed in 5.09 seconds =============================
这篇关于如何为基于 http 的集成测试生成覆盖率报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!