Skip to main content

http_testing framework on top of pytest

Project description

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

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_pattern=r".*")],
        ),
        should_not_find=NegativeAssertions(
            status_code=400,
            content=["groot"],
            headers={"nooooo": ""},
            cookies=[Cookie(name="nop", value_pattern="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-Typesss':'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

We can 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"},
    )

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_httptesting-0.1.3.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_httptesting-0.1.3-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file pytest_httptesting-0.1.3.tar.gz.

File metadata

  • Download URL: pytest_httptesting-0.1.3.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.9.5 Linux/5.4.72-microsoft-standard-WSL2

File hashes

Hashes for pytest_httptesting-0.1.3.tar.gz
Algorithm Hash digest
SHA256 52201a8295242965ca916e91302fb58ff3ff54abd1497e14f13eb54bf769dac1
MD5 2281af29dc5985bba7dc5755942ee4cc
BLAKE2b-256 2831d1aa75d0c192da6b8e7f6cf5b20921f210c1d4b499e9b9e6808eeb7534cd

See more details on using hashes here.

File details

Details for the file pytest_httptesting-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pytest_httptesting-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.9.5 Linux/5.4.72-microsoft-standard-WSL2

File hashes

Hashes for pytest_httptesting-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d2dd8cb5b5f678a9cfec3d8cc15efd7b0425bf78ab3bacdee3e1043bdeb4fa0c
MD5 a37bdc402d69493705d5595ce0d91173
BLAKE2b-256 3ec9d5904e6d0000781fa152dd50aa50e19bbaea8e37d49969b878a9b48d55c9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page