pastebin.py


Overview

The `pastebin.py` file provides functionality to submit failure or entire test session information from pytest runs to an external pastebin service (specifically [bpaste.net](https://bpa.st)). This integration aids developers and testers by automatically uploading verbose test output — such as failure tracebacks or full test session logs — to a web-accessible paste service, allowing easier sharing and debugging.

The file implements pytest plugin hooks and CLI options to enable users to select which information to send ("failed" tests only, or "all" terminal output). It captures test output during runs, creates pastes programmatically via HTTP POST requests, and reports URLs to users in the terminal summary.


Detailed Explanation of Classes, Functions, and Methods

Constants and Keys


pytest_addoption(parser: Parser) -> None

**Purpose:** Adds the `--pastebin` command-line option to pytest.

**Parameters:**

**Behavior:**

**Usage Example:**

pytest --pastebin=failed
pytest --pastebin=all

pytest_configure(config: Config) -> None

**Purpose:** Pytest hook called after command-line options have been parsed and pytest initialized.

**Parameters:**

**Behavior:**

**Implementation Details:**


pytest_unconfigure(config: Config) -> None

**Purpose:** Pytest hook called during cleanup/unloading of plugins.

**Parameters:**

**Behavior:**


create_new_paste(contents: str | bytes) -> str

**Purpose:** Uploads a given string or bytes content to the bpaste.net pastebin service and returns the resulting paste URL.

**Parameters:**

**Returns:**

**Implementation Details:**

**Usage Example:**

url = create_new_paste("This is a test paste")
print(f"Paste created at: {url}")

pytest_terminal_summary(terminalreporter: TerminalReporter) -> None

**Purpose:** Pytest hook to add additional summaries at the end of test runs in the terminal.

**Parameters:**

**Behavior:**

**Implementation Details:**


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram: Class and Function Structure

flowchart TD
    A[pytest_addoption(parser)]
    B[pytest_configure(config)]
    C[pytest_unconfigure(config)]
    D[create_new_paste(contents)]
    E[pytest_terminal_summary(terminalreporter)]

    A -->|adds CLI option| B
    B -->|patch terminal reporter write| C
    C -->|send paste with terminal output| D
    E -->|send paste for each failed test| D

Summary

This `pastebin.py` file implements a pytest plugin that improves the test debugging experience by automatically uploading test failure summaries or full session logs to an online pastebin service. It cleanly integrates with pytest’s plugin system and terminal output mechanisms, providing users with easy sharing of verbose test outputs via URLs printed at the end of test runs. The implementation balances simplicity with robustness, handling network errors gracefully and supporting two modes of operation controlled by a CLI option.


Example Usage

Run pytest with pastebin integration for failed tests only:

pytest --pastebin=failed

After test completion, URLs to pastes of failure details will be printed in the terminal.

Run pytest with full terminal output uploaded:

pytest --pastebin=all

The entire test session output is captured and uploaded, with a single URL printed at the end.


End of Documentation for pastebin.py