Skip to content

test: add smoke tests and CI for example scripts - #87

Merged
endolith merged 1 commit into
masterfrom
tests/example-scripts
Aug 4, 2026
Merged

test: add smoke tests and CI for example scripts#87
endolith merged 1 commit into
masterfrom
tests/example-scripts

Conversation

@kilo-code-bot

@kilo-code-bot kilo-code-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Background

The example scripts reproduce published voting-theory results (Merrill 1984, Weber 1977) and are excluded from the pytest suite (--ignore=examples), so they are never executed in CI. #65 is about to refactor them for joblib parallelism; this PR establishes automated checks for them first, so the parallelization can be verified before and after.

What this PR changes

  • tests/test_examples.py — runs each parallelized example at a reduced election count with an injected seed, asserting it completes and emits tabulated output; plus a reference-value check for weber_1977_effectiveness_table against its documented results (within Monte Carlo tolerance). Skipped when joblib/matplotlib/tabulate aren't installed.
  • ci: smoke-test-examples job in python-package.yml — installs joblib, matplotlib, tabulate and runs the new tests.

Tests

The new smoke tests themselves (11 tests, ~1 min). CI for them is added in the same commit since it exists to run them (AGENTS.md).

Note: PR #65 (parallelization) is rebased on top of this branch.

Summary by CodeRabbit

  • Tests

    • Added automated smoke tests for ten stochastic examples.
    • Verified examples run successfully, produce expected output, and remain reproducible with deterministic settings.
    • Added checks comparing documented example results against reference values.
  • Chores

    • Added continuous integration coverage for example tests using Python 3.12 and headless plotting.
    • Included optional dependencies required to run the example test suite.

@what-the-diff

what-the-diff Bot commented Aug 4, 2026

Copy link
Copy Markdown

PR Summary

  • New workflow job for testing: A new task named smoke-test-examples has been added to the automatic testing system. It runs test scenarios on our sample scripts, helping us make sure they work as expected.
  • Preparation of Python environment: This task also ensures that an environment with Python version 3.12 is set up with all the necessary packages, such as joblib, matplotlib, and tabulate, to properly run the tests.
  • Introduction of a new test file: A file named tests/test_examples.py has been added. This file includes the tests for our script examples, helping confirm that they execute successfully and produce the correct output.
  • Parameterized test function: A function named test_example_runs has been developed to verify that the chosen sample scripts function without any errors.
  • Validation against a known reference: A particular test named test_weber_effectiveness_close_to_reference cross-verifies the results of one of the script examples with a pre-existing known reference. This helps to ensure the results returned are within acceptable error margins.

@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Added subprocess tests for ten stochastic examples. The tests use deterministic seeds, reduced election counts, optional dependency checks, headless Matplotlib, output markers, and reference-table validation. CI runs the tests with Python 3.12.

Changes

Example simulation validation

Layer / File(s) Summary
Test configuration
tests/test_examples.py
Defines example paths, optional dependency handling, seeds, election counts, affected scripts, and expected output markers.
Example execution and CI wiring
tests/test_examples.py, .github/workflows/python-package.yml
Runs examples in isolated subprocesses, checks expected output, and adds a CI smoke-test job with required dependencies and MPLBACKEND=Agg.
Weber effectiveness validation
tests/test_examples.py
Parses documented reference tables and compares example results with configured tolerances and excluded rows.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • endolith/elsim#65: Modifies the same example scripts and adds related example test coverage.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the added smoke tests and CI workflow for example scripts.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch tests/example-scripts

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.37%. Comparing base (08a69e1) to head (2275f30).

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #87   +/-   ##
=======================================
  Coverage   96.37%   96.37%           
=======================================
  Files          17       17           
  Lines         496      496           
=======================================
  Hits          478      478           
  Misses         18       18           
Flag Coverage Δ
no-numba 95.76% <ø> (ø)
numba 88.10% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/test_examples.py`:
- Around line 90-93: Update test_example_runs to validate each script’s
tabulated output rather than only asserting non-empty stdout. Define expected
table header or row patterns keyed by AFFECTED_SCRIPTS entries, then assert the
corresponding pattern appears in stdout after running _run_example, while
allowing unrelated lines such as timing output.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 4a59ad76-02cf-4eb3-a6bf-d18dc7b0422c

📥 Commits

Reviewing files that changed from the base of the PR and between 08a69e1 and 5136f56.

📒 Files selected for processing (2)
  • .github/workflows/python-package.yml
  • tests/test_examples.py

Comment thread tests/test_examples.py Outdated
@kilo-code-bot
kilo-code-bot Bot force-pushed the tests/example-scripts branch from 5136f56 to 2044bb9 Compare August 4, 2026 05:32

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/test_examples.py`:
- Around line 19-20: Update the module documentation in tests/test_examples.py
to list tabulate alongside joblib and matplotlib as a required optional examples
dependency, matching the module-level skip condition that checks tabulate
availability.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a0d654d4-9a29-4b14-89c1-f1a1cd3aa1c3

📥 Commits

Reviewing files that changed from the base of the PR and between 5136f56 and 2044bb9.

📒 Files selected for processing (2)
  • .github/workflows/python-package.yml
  • tests/test_examples.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • .github/workflows/python-package.yml

Comment thread tests/test_examples.py Outdated
@kilo-code-bot

kilo-code-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

Addressed CodeRabbit's smoke-test comment: test_example_runs now asserts a per-script expected table pattern (e.g. '| Method' for the Merrill tables, 'Vote-for-half' for weber effectiveness, '| Standard | Borda | Approval |' for weber table 4) appears in stdout, instead of only requiring non-empty output — so a regression that drops the tabulate output fails the test even if timing/progress lines remain. Folded into the test commit; #65 was rebased on top.

@endolith
endolith force-pushed the tests/example-scripts branch from 452caad to ee83a32 Compare August 4, 2026 05:43
@kilo-code-bot
kilo-code-bot Bot force-pushed the tests/example-scripts branch from ee83a32 to 123bdf0 Compare August 4, 2026 06:07

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/test_examples.py`:
- Around line 169-175: Update the comparison logic around `comparable` to
iterate over every expected row except those in `cfg['skip']`, asserting each
exists in `actual` before value comparison. Validate that each corresponding row
has the same number of values as its expected row before comparing indexed
values, replacing the truncating `zip` behavior while preserving the existing
tolerance checks and error context.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a9eb0cab-cf34-449d-87e4-a570e6142ffc

📥 Commits

Reviewing files that changed from the base of the PR and between ee83a32 and 123bdf0.

📒 Files selected for processing (2)
  • .github/workflows/python-package.yml
  • tests/test_examples.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • .github/workflows/python-package.yml

Comment thread tests/test_examples.py Outdated
@kilo-code-bot

kilo-code-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

Strengthened the tests to check actual output values, not just completion: the six scripts with documented Typical result tables (merrill table_1/2/3/4, weber effectiveness, weber table_4) are now run on the real parallel path and every table cell is compared against the published reference (parsed from each script's docstring, so one source of truth). Tolerances are chosen so a legitimate run's MC error (~1 pp at 2,000–3,000 elections) never flakes, while an effective sample of only batch_size=100 elections — exactly what an RNG-sharing/duplication regression produces — is far outside tolerance. Verified: a forced same-seed-every-batch run deviates 5.9 pp from the reference and fails the test, whereas the legit runs pass. 16 tests total.

@endolith

endolith commented Aug 4, 2026

Copy link
Copy Markdown
Owner

We are also checking some against the values from the original papers while checking others against the typical output of the existing scripts, because mine never matched the original papers exactly. See #88

The example scripts are stochastic Monte Carlo verification scripts that
were never executed by CI. Run each one in full (runpy) and read its
computed table variable directly instead of parsing printed output; the
tests are marked slow and skipped by default (pytest -m slow).

For the six scripts with published reference results, compare the output
values against hardcoded reference tables from the papers
(REFERENCE_VALUES). Tolerances accommodate Monte Carlo error plus the
scripts' measured deviations from the published tables; note that
merrill_1984_table_2 and table_4 deviate from the papers by up to ~5 pp
and ~9 pp respectively, so those two get looser tolerances and cannot
detect an RNG-duplication regression the way the others do.

The CI job runs the tests with pytest -m slow.
@kilo-code-bot
kilo-code-bot Bot force-pushed the tests/example-scripts branch from a890057 to 2275f30 Compare August 4, 2026 17:54
@endolith
endolith merged commit 6e08e9e into master Aug 4, 2026
20 checks passed
@endolith
endolith deleted the tests/example-scripts branch August 4, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant