test_pastebin.py
Overview
`test_pastebin.py` is a test suite designed to verify the functionality and robustness of the Pastebin integration plugin used in the pytest testing framework. The file contains automated tests that cover various scenarios including:
Capturing and posting test results to a pastebin service for failed tests or all tests.
Handling non-ASCII characters in test output pastes.
Verifying the behavior of the pastebin plugin when interacting with the external pastebin service, particularly the URL format and network error handling.
This file uses pytest fixtures, monkeypatching, and mocking to simulate different behaviors and external conditions without relying on actual network calls during tests. It ensures that the plugin correctly formats and sends data and gracefully handles unexpected responses or errors.
Classes and Their Responsibilities
TestPasteCapture
This class contains tests focused on the integration of pytest test results with the pastebin plugin, particularly how test outputs are captured and sent depending on user options.
Methods:
pastebinlist(self, monkeypatch, request) -> list[str | bytes]
Pytest fixture that intercepts calls to the pastebin plugin'screate_new_pastemethod and collects the pasted contents in a list.Parameters:
monkeypatch: pytest's monkeypatch fixture for dynamic attribute patchingrequest: pytest's fixture to access test request context
Returns: List collecting strings or bytes that represent pasted content.
Usage: Used as a fixture in tests to verify what content is sent to the pastebin service.
test_failed(self, pytester: Pytester, pastebinlist) -> None
Tests that only failed test results are pasted when the--pastebin=failedoption is used.Uses
pytesterto create and run a test file with passing, failing, and skipped tests.Asserts that one paste was made containing the failed test code.
test_all(self, pytester: Pytester, pastebinlist) -> None
Tests that all test results (passed, failed, skipped) are pasted when --pastebin=all is used.Validates that the pasted content includes all test statuses and the final summary line.
test_non_ascii_paste_text(self, pytester: Pytester, pastebinlist) -> None
Ensures that non-ASCII characters (e.g., Unicode smileys) in test failure output are correctly handled and pasted without encoding issues.Creates a test with a unicode assertion failure and checks the output includes the unicode character.
TestPaste
This class tests the internal workings of the pastebin plugin’s network interaction and error handling when creating new pastes.
Fixtures:
pastebin(self, request)
Returns the pastebin plugin instance from pytest's plugin manager.mocked_urlopen_invalid(self, monkeypatch: MonkeyPatch)
Mocks urllib.request.urlopen to simulate a pastebin server response with an invalid URL format in the HTML response.
Returns a list capturing calls made to the mockedurlopen.mocked_urlopen(self, monkeypatch: MonkeyPatch)
Mocks urllib.request.urlopen to simulate a normal pastebin server response returning a valid URL.
Returns a list capturing calls made to the mockedurlopen.
Test Methods:
test_pastebin_invalid_url(self, pastebin, mocked_urlopen_invalid) -> None
Validates that when the pastebin server returns an unexpected URL format, the plugin returns an appropriate error string.test_pastebin_http_error(self, pastebin) -> None
Tests handling of an HTTP error (e.g., 400 Bad Request) raised during the paste creation request.
Uses mock.patch to simulate the HTTPError and asserts the error message returned.test_pastebin_url_error(self, pastebin) -> None
Tests handling of a URL error (e.g., network unreachable) during the paste creation request.test_create_new_paste(self, pastebin, mocked_urlopen) -> None
Confirms that a valid paste creation request sends the correct data fields (lexer=text,code=...,expiry=1week) and returns the expected paste URL.test_create_new_paste_failure(self, pastebin, monkeypatch: MonkeyPatch) -> None
Simulates a server response with unexpected content that does not match the expected URL format, verifying that the plugin returns a descriptive error message.
Important Implementation Details
The tests rely heavily on monkeypatching and mocking to simulate the behavior of the external pastebin network service (urllib.request.urlopen) and pytest plugin internals.
The pastebin plugin's method
create_new_pastereturns either a URL of the created paste or an error message string starting with "bad response:" when the response format is invalid or an error occurs.The content sent to the pastebin service includes the code block, lexer type (
text), and expiry period (1week).The tests cover edge cases including non-ASCII content encoding, malformed URLs in responses, and network errors, ensuring robustness of the pastebin integration.
Use of pytest's
pytesterfixture allows creation of temporary test modules and running pytest on them programmatically, capturing results to verify plugin behavior.
Interaction with Other System Components
pytest Core and Plugin System:
The tests interact with pytest's plugin manager to retrieve the pastebin plugin, and with the test collection and reporting system (pytester.inline_run) to run test files and capture outcomes.Pastebin Plugin (
pastebin):
The primary subject under test is the pastebin plugin, which this file verifies for correct paste creation and error handling.External Pastebin Service (Simulated):
Although actual network calls are mocked, the file tests the expected network interactions and response parsing logic of the plugin.
Usage Examples
Example: Testing failed test pastes
def test_failed(self, pytester: Pytester, pastebinlist):
# Create a test file with passing, failing, and skipped tests
testpath = pytester.makepyfile(
"""
import pytest
def test_pass():
pass
def test_fail():
assert 0
def test_skip():
pytest.skip("")
"""
)
# Run pytest with pastebin only on failed tests
reprec = pytester.inline_run(testpath, "--pastebin=failed")
# Confirm one paste created with failing test code content
assert len(pastebinlist) == 1
s = pastebinlist[0]
assert "def test_fail" in s
assert reprec.countoutcomes() == [1, 1, 1]
Diagram: Class Structure of test_pastebin.py
classDiagram
class TestPasteCapture {
+pastebinlist(monkeypatch, request) list[str|bytes]
+test_failed(pytester, pastebinlist)
+test_all(pytester, pastebinlist)
+test_non_ascii_paste_text(pytester, pastebinlist)
}
class TestPaste {
+pastebin(request)
+mocked_urlopen_invalid(monkeypatch) list
+mocked_urlopen(monkeypatch) list
+test_pastebin_invalid_url(pastebin, mocked_urlopen_invalid)
+test_pastebin_http_error(pastebin)
+test_pastebin_url_error(pastebin)
+test_create_new_paste(pastebin, mocked_urlopen)
+test_create_new_paste_failure(pastebin, monkeypatch)
}
Summary
`test_pastebin.py` is a comprehensive test module ensuring that the pytest pastebin plugin correctly captures test result outputs, formats them properly, sends them to the pastebin service, and handles failure cases gracefully. It uses pytest's advanced testing fixtures and mocking capabilities, simulating both pytest internals and external HTTP interactions to validate the plugin's behavior without requiring network connectivity.
This testing file is crucial to maintaining reliability and user trust in the pastebin plugin's integration and error resilience within the pytest ecosystem.