Skip to content

Unit tests fail when .env.local is sourced #81

Description

@crtahlin

Problem

Unit tests fail (15 failures) when .env.local environment variables are exported in the shell. This happens because .env.local overrides values that unit tests expect to be unset or at defaults:

  • PROVENANCE_GATEWAY_URL=http://localhost:8000 breaks test_default_url (expects production URL)
  • X402_PRIVATE_KEY being set breaks test_x402_status_shows_no_private_key and test_x402_balance_fails_without_private_key
  • Real X402_PRIVATE_KEY causes x402 client tests to bypass mocks and hit real Web3 providers

Current workaround

Run unit and integration tests separately:

# Unit tests — clean environment (no .env.local)
pytest --ignore=tests/test_integration.py

# Integration tests — need .env.local for wallet keys
export $(grep -v '^#' .env.local | xargs)
pytest tests/test_integration.py -v

Expected behavior

Unit tests should pass regardless of what environment variables are set, since they mock all external dependencies.

Possible solutions

  1. Fixture-based env isolation: Add a conftest.py autouse fixture that saves and clears sensitive env vars (PROVENANCE_GATEWAY_URL, X402_PRIVATE_KEY, PROVENANCE_WALLET_KEY, etc.) before each unit test, restoring them after
  2. monkeypatch in affected tests: Use monkeypatch.delenv() in the specific tests that assume unset env vars
  3. Reload config module: Mock or reload config.py in unit tests so module-level os.getenv() calls pick up test values instead of shell env

Affected tests

Test file Failures Cause
test_gateway_client.py 1 PROVENANCE_GATEWAY_URL override
test_cli.py 2 X402_PRIVATE_KEY set
test_x402_client.py 12 X402_PRIVATE_KEY bypasses mocks

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions