Skip to main content

HTTP_TESTING

Description

This project aims to help to create e2e tests, by chaining http calls and verifications on target pages. IMPORTANT: Since only http calls involved, no javascript will run.

Concept

This project is built on pytest.

Each .py file in test represents several tests scenario on one target site, we can provide the site's hostname as a local variable, the framework know how to construct http call from that.

Each test function in the .py test file represent a scenario of test which is consisted by several steps. For example, test user's account page, first, we authenticate the client by post user's name and password, then access the account page to verify some values. Each step is described by calling the variable check which is a pytest fixture.

Tutorial

Install

pip install pytest-httptesting

Create test suite

# test/test_example.py

from http_testing.assertion_elements.cookies_assertion import Cookie
from http_testing.assertions import Assertions, NegativeAssertions
from http_testing.page_checker import PageChecker
from http_testing.validators import Regex

host = "www.google.com"  # mandatory: used in the `check` fixture
scheme = "https"  # "https" by default
port = None  # None by default


def test_scenario_one(check: PageChecker):
    check(
        title="Senario One",
        path="/",
        should_find=Assertions(
            status_code=200,
            content=["<title>Google</title>"],
            headers={"Content-Type": "text/html; charset=ISO-8859-1"},
            cookies=[Cookie(name="AEC", value=Regex(r".*"))],
        ),
        should_not_find=NegativeAssertions(
            status_code=400,
            content=["groot"],
            headers={"nooooo": ""},
            cookies=[Cookie(name="nop", value="a")],
        ),
    )

Run test

$ pytest test --tb=no --no-header -v  # traceback is disabled because it is not very useful to anayse the functional error
============= test session starts =============
collected 1 item

test/test_example.py::test_scenario_one PASSED

============= 1 passed in 0.16s =============

Debug

In case of error, a temporary file will be generated, as shown in the short test summary info. It is a json file concluding response content, status code, headers and cookies.

$ pytest test --tb=no --no-header -v
============= test session starts =============
collected 1 item

test/test_example.py::test_scenario_one FAILED

============= short test summary info =============
FAILED test/test_example.py::test_scenario_one - AssertionError: Senario One - 'Content-Typessss':'text/html; charset=ISO-8859-1' not found in headers on page 'https://www.google.com/' - please check file '/tmp/tmptaowd2u5'
============= 1 failed in 1.22s =============

Advanced

Customize the http client configuration

It is possible to create a fixture http_client_config to override the default configuration, like adding headers and cookies to the http client.

@pytest.fixture
def http_client_config():
    return HttpClientConfiguration(
        trust_env=False,
        verify=False,
        cookies={"cookie_1": "cookie_value_1"},
        headers={"header_1": "header_value_1"},
    )

Customize the base url

It is possible to create a fixture base_url to override the default construction of base url.

from httpx import URL
@pytest.fixture
def base_url() -> URL:
    return URL("https://www.google.com")

Metadata

Release files for pytest-httptesting 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-httptesting 0.2.0
File Size Uploaded Provenance Actions
pytest_httptesting-0.2.0.tar.gz 8.6 kB Without provenance Details for pytest_httptesting-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-httptesting 0.2.0
File Interpreter ABI Platform Actions
pytest_httptesting-0.2.0-py3-none-any.whl Python 3 none any Details for pytest_httptesting-0.2.0-py3-none-any.whl

Total release size: 20.9 kB

Release files / pytest_httptesting-0.2.0.tar.gz

Download URL pytest_httptesting-0.2.0.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
36c377bf8aaebb7446ea40e051e269e2d7bf34351c9b4f897078492537001393
BLAKE2b-256 checksum
How to use checksums
0c1d560e689e2cd47b4f88993b85ef45ef9de13ec1fb4b5878275853ff01cb61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.9.5 Linux/5.4.72-microsoft-standard-WSL2

Release files / pytest_httptesting-0.2.0-py3-none-any.whl

Download URL pytest_httptesting-0.2.0-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6d61207dba2f18c6237701ae4e1a0d88761bc0deee69ef82e17498815982859f
BLAKE2b-256 checksum
How to use checksums
7ee879992482d2825573e34e2451f8230a16d73daa9ea5f7973421d473d70926
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.9.5 Linux/5.4.72-microsoft-standard-WSL2

Security for pytest-httptesting 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page