How to generate coverage report for http based integration tests?

2024/10/7 16:22:51

I am writing integration tests for a project in which I am making HTTP calls and testing whether they were successful or not.

Since I am not importing any module and not calling functions directly coverage.py report for this is 0%.

I want to know how can I generate coverage report for such integration HTTP request tests?

Answer

The recipe is pretty much this:

  1. Ensure the backend starts in code coverage mode
  2. Run the tests
  3. Ensure the backend coverage is written to file
  4. Read the coverage from file and append it to test run coverage

Example:

backend

Imagine you have a dummy backend server that responds with a "Hello World" page on GET requests:

# backend.py
from http.server import BaseHTTPRequestHandler, HTTPServerclass 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()

test

A simple test that makes an HTTP request and verifies the response contains "Hello World":

# tests/test_server.py
import requestsdef test_GET():resp = requests.get('http://127.0.0.1:8000')resp.raise_for_status()assert 'Hello World' in resp.text

Recipe

# 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 is the fixture provided by pytest-cov (docs).

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 itemtests/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 =============================
https://en.xdnf.cn/q/70227.html

Related Q&A

Does Webdriver support pagefactory for Python?

I was reading about page objects and design patterns on the Webdriver project site and came across pagefactory. It doesnt look like the Webdriver for Python API includes pagefactory. Is this true?

Truncated versus floored division in Python

To establish context, Im talking about integer arithmetic only, on large integers so going via floating point isnt an option, and using negative numbers so the difference between floored and truncated …

when restoring from a checkpoint, how can I change the data type of the parameters?

I have a pre-trained Tensorflow checkpoint, where the parameters are all of float32 data type.How can I load checkpoint parameters as float16? Or is there a way to modify data types of a checkpoint?F…

Opencv Python open dng format

I cant figure out how to open a dng file in opencv. The file was created when using the pro options of the Samsung Galaxy S7. The images that are created when using those options are a dng file as well…

VSCode: Set environment variables via script

I have a shell script env.sh containing statements like export ENV_VAR1 = 1. On Linux terminal, I can use . env.sh or source env.sh to set the environment variables. How to set the environment variable…

TensorFlow performance bottleneck on IteratorGetNext

While fiddling around with TensorFlow, I noticed that a relatively simple task (batching some of our 3D accelerometer data and taking the sum of each epoch) was having relatively poor performance. Here…

SQLAlchemy - How to access column names from ResultProxy and write to CSV headers

I am trying to use SQLAlchemy to establish a connection to a PostgreSQL Database, execute a SQL query and print the output of the file to a file in linux. from sqlalchemy import create_engine import ya…

Python Facebook API - cursor pagination

My question involves learning how to retrieve my entire list of friends using Facebooks Python API. The current result returns an object with limited number of friends and a link to the next page. How …

PyQt Irregularly Shaped Windows (e.g. A circular without a border/decorations)

How do I create an irregularly shaped window in PyQt?I found this C++ solution, however I am unsure of how to do that in Python.

default values for variable argument list in Python

Is it possible to set a default value for a variable argument list in Python 3?Something like:def do_it(*args=(2, 5, 21)):passI wonder that a variable argument list is of type tuple but no tuple is ac…