MCP Testing Guide for SpotifyScraper¶
Introduction¶
This guide explains how to use MCP (Mock, Capture, Playback) testing with SpotifyScraper to create reliable, reproducible tests that don't depend on network access or Spotify's availability.
What is MCP Testing?¶
MCP testing is a three-phase approach to testing network-dependent code:
- Mock - Create realistic simulations of network responses
- Capture - Record real HTTP interactions for future use
- Playback - Replay recorded responses during tests
This approach offers several benefits: - Tests run faster after the initial recording - Tests are deterministic and don't depend on network conditions - Tests aren't affected by rate limiting or service changes - Real data is used for validation, ensuring accurate testing
Implementation Details¶
SpotifyScraper's MCP testing uses VCR.py, a library that records HTTP interactions and saves them as "cassettes" (YAML files) that can be replayed in future test runs.
Key Components¶
- VCR Configuration: Found in
tests/unit/test_track_album_mcp.py - Cassettes Directory:
tests/fixtures/vcr_cassettes/ - Test Runner:
mcp_test_runner.pyfor easy execution of MCP tests - Example Script:
examples/mcp_testing_demo.pydemonstrating usage
Using MCP Testing¶
Running MCP Tests¶
# Run all MCP tests
python mcp_test_runner.py
# Force recording new cassettes
python mcp_test_runner.py --record
# Delete existing cassettes and record new ones
python mcp_test_runner.py --clean --record
# Run a specific MCP test file
pytest tests/unit/test_track_album_mcp.py -v
Creating New MCP Tests¶
- Create a test file in
tests/unit/with a name containing "mcp" - Import and configure VCR:
import vcr
from pathlib import Path
# Configure VCR
vcr_cassette_dir = Path(__file__).parent.parent / "fixtures" / "vcr_cassettes"
my_vcr = vcr.VCR(
cassette_library_dir=str(vcr_cassette_dir),
record_mode='once',
match_on=['uri', 'method'],
filter_headers=['authorization', 'cookie'],
)
- Create test methods with the
@my_vcr.use_cassette()decorator:
@my_vcr.use_cassette('my_test_cassette.yaml')
def test_some_network_functionality(self):
# This test will record HTTP interactions the first time
# and replay them in subsequent runs
client = SpotifyClient()
data = client.get_track_info("https://open.spotify.com/track/12345")
assert "album" in data
Demo and Examples¶
The examples/mcp_testing_demo.py script demonstrates:
- Extracting album data from JSON-LD content
- Working with track data that includes album information
- The overall MCP testing approach
Run it with:
python examples/mcp_testing_demo.py
Testing the JSON-LD Fallback¶
One key feature tested with MCP is the JSON-LD fallback mechanism for album data:
- If the primary extraction method doesn't provide album data, the code tries to extract it from JSON-LD
- JSON-LD is embedded in track pages for SEO purposes and often contains album information
- The
extract_album_data_from_jsonld()function parses this data into the standardized album format
The MCP tests verify that: - The JSON-LD extraction works correctly - The fallback logic properly integrates extracted album data - The final track data includes the album field
Best Practices¶
- Filter sensitive information: Use VCR's filtering options to avoid recording cookies or API keys
- Use deterministic URLs: Avoid using random parameters in URLs
- Keep cassettes up to date: Periodically re-record cassettes to match current API behavior
- Commit cassettes to version control: This ensures consistent test behavior
- Document unusual behavior: Note any quirks in cassettes for future reference
Troubleshooting¶
- Cassette not found: Ensure the cassette exists or run with
--record - Unexpected behavior: Try deleting the cassette and re-recording
- Authentication issues: Check if your tests require authentication
- Match errors: VCR may fail to match requests if URLs or request bodies change
Resources¶
- VCR.py Documentation
- SpotifyScraper Documentation
- README_TESTING.md - General testing guide for SpotifyScraper