Skip to main content

HTTP_TESTING

Test Package version Supported Python versions

Description

This project aims to help to create e2e tests, by chaining http calls and verifications on target pages.

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.assertions import Assertions, NegativeAssertions
from http_testing.cookie import Cookie
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="Scenario 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")],
        ),
        timeout=10,  # you can pass additional kwargs to httpx request
    )

    assert check.previous_response.status_code == 200  # inspect previous response

Run test

$ pytest test --tb=short --no-header -v
============= 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. It is a json file concluding response content, status code, headers and cookies.

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

test/test_example.py::test_scenario_one FAILED
============= FAILURES =============
________ test_scenario_one _________
test/test_example.py:10: in test_scenario_one
    check(
http_testing/page_checker.py:62: in __call__
    should_find.check(assertion_data=assertion_data)
http_testing/assertions.py:33: in check
    assert assertion_data in checker
E   AssertionError: assert in check 'Senario One' - '<title>groot</title>' should found in content on page 'https://www.google.com/'
E         - please check file '/tmp/tmp3lp0d7oh'
============= short test summary info =============
FAILED test/test_example.py::test_scenario_one - AssertionError: assert in check 'Senario One' - '<title>groot</title>' should found in content on page 'https://www.google.com/'
============= 1 failed in 1.22s =============

Advanced

Customize the http client configuration

It is possible to create a fixture http_client to create your own http client.

@pytest.fixture
def http_client():
    with Client(verify=False, cookies={"cookie_1": "cookie_value_1"}) as client:
        yield client

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")

Release files for pytest-httptesting 0.10.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.10.0
File Size Uploaded
pytest_httptesting-0.10.0.tar.gz 27.4 kB Details

Built distribution (wheel)

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

Total release size: 40.6 kB

Release files / pytest_httptesting-0.10.0.tar.gz

Download URL pytest_httptesting-0.10.0.tar.gz
Size 27.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3d0755023b4e59486dc839275341bf3d115f7decdfbd896c560c3c92e85577f7
BLAKE2b-256 checksum
How to use checksums
4610c212f00ed574b8632475d46ca8c2a922c36a18a2f0613bed3d309ec27e95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

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

Download URL pytest_httptesting-0.10.0-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ef6cd845e9c141aa7a8045cd85e8fa94580e722ebafe721bdd3fd429f8348805
BLAKE2b-256 checksum
How to use checksums
34521f0a6441c25b1e45fbb6f00e95cf14dbe3570fb93f4245d4d0497272cf47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Release history Release notifications | RSS feed

This release

0.10.0 This release

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

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