Skip to content

Run the two assertions that sit inside pytest.raises blocks - #924

Open
dylanpulver wants to merge 1 commit into
simonw:mainfrom
dylanpulver:fix/assertions-inside-pytest-raises
Open

dylanpulver wants to merge 1 commit into
simonw:mainfrom
dylanpulver:fix/assertions-inside-pytest-raises

Conversation

@dylanpulver

@dylanpulver dylanpulver commented Oct 31, 2026 •

Copy link
Copy Markdown

Two tests put their assertion inside the with pytest.raises(...) block, after the call that raises:

def test_convert_output_multiple_column_error(fresh_db):
    table = fresh_db.table("table")
    with pytest.raises(ValueError) as excinfo:
        table.convert(["title", "other"], lambda v: v, output="out")
        assert "output= can only be used with a single column" in str(excinfo.value)

Neither has ever run. The body has to raise for the test to pass, and an assert can only raise AssertionError, so the raiser always precedes the assertion. The effect is that pytest.raises pins the exception type while the message goes unchecked.

The other one is test_create_error_if_invalid_self_referential_foreign_keys, which checks ex.value.args.

Both assertions pass once they execute, so no expectation needed updating. To confirm they now bite, I reworded both messages in sqlite_utils/db.py and ran the pair:

Injected change Current tests With this change
Both messages reworded, types unchanged 2 passed 2 failed

tests/test_convert.py and tests/test_create.py stay at 218 passed, 1 skipped. black --check and flake8 are clean.

Found with a static check for assertions that cannot execute inside a pytest.raises block, and written with Claude Code; the numbers above are from running the suite.


📚 Documentation preview 📚: https://sqlite-utils--924.org.readthedocs.build/en/924/

`test_convert_output_multiple_column_error` and
`test_create_error_if_invalid_self_referential_foreign_keys` each put their
assertion inside the `with pytest.raises(...)` block, after the call that
raises, so neither has ever run. The body has to raise for the test to pass and
an assert can only raise AssertionError, so the raiser always precedes the
assertion.

The effect is that `pytest.raises` pins the exception type while the message
goes unchecked. Both assertions pass once they execute, so no expectation
needed updating. Rewording either message now fails the matching test, where
before this change both still passed.

tests/test_convert.py and tests/test_create.py stay at 218 passed, 1 skipped.
`black --check` and `flake8` are clean.
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