pytest Plugin

pytest plugin: UI assertions backed by LayoutLens.

Registered automatically via the pytest11 entry point when layoutlens is installed. The deterministic assertions (assert_a11y, assert_layout) run keyless — no API key, no LLM, fully reproducible — so they can gate CI on any fork with no secrets. The LLM assertion (assert_ui) skips rather than fails when no API key is configured, so the same suite runs everywhere.

Usage:

def test_landing_page(layoutlens):
    layoutlens.assert_a11y("landing.html")                    # axe, keyless
    layoutlens.assert_layout("landing.html", viewport="mobile")  # keyless
    layoutlens.assert_ui("landing.html", "Is the CTA above the fold?")

Options: --layoutlens-model (default gpt-4o-mini), --layoutlens-no-llm (make assert_ui skip unconditionally — a fast, free, deterministic-only CI lane).

class layoutlens.pytest_plugin.LayoutLensFixture(config)[source]

Sync assertion helpers over a shared LayoutLens client.

Parameters:

config (pytest.Config)

assert_a11y(source, compliance_level='AA', viewport='desktop')[source]

Assert the page has no axe-core WCAG violations (keyless).

Parameters:
  • source (str) – URL or local HTML path to audit.

  • compliance_level (str) – WCAG conformance level to check.

  • viewport (str) – Named viewport used for rendering.

Returns:

Successful deterministic accessibility result.

Raises:

pytest.fail.Exception – Listing each violated rule, its impact, and the first affected selectors.

Return type:

AnalysisResult

assert_layout(source, viewport='desktop')[source]

Assert the deterministic layout scan measures no defects (keyless).

Parameters:
  • source (str) – URL or local HTML path to scan.

  • viewport (str) – Named viewport used for rendering.

Returns:

Successful deterministic layout result.

Raises:

pytest.fail.Exception – Listing each measured defect with its selector, measured values, and violated threshold.

Return type:

AnalysisResult

assert_ui(source, question, min_confidence=0.0, viewport='desktop')[source]

Assert the vision model answers question with a leading “yes”.

Skips (never fails) when --layoutlens-no-llm is set or no API key is configured, so keyless environments still run the rest of the suite.

Parameters:
  • source (str) – URL, local HTML path, or image to analyze.

  • question (str) – Exact UI question sent to the model.

  • min_confidence (float) – Minimum accepted confidence score.

  • viewport (str) – Named viewport used for rendering.

Returns:

Successful model-backed analysis result.

Raises:

pytest.fail.Exception – When the model answers “no” (or an unparseable answer), with the model’s reasoning attached.

Return type:

AnalysisResult

scan_layout(source, viewport='desktop')[source]

Return the raw deterministic layout report dict (no assertion).

Parameters:
Return type:

dict[str, Any]

audit_a11y(source, viewport='desktop')[source]

Return the raw axe report dict (no assertion).

Parameters:
Return type:

dict[str, Any]