How to Test the AI Agent Book Code: A Complete pytest Guide
To test the AI agent book code, install the development dependencies from requirements-dev.txt and run pytest from the repository root to execute the distributed test suite located in each chapter's tests/ directory.
The bojieli/ai-agent-book repository provides hands-on implementations for building AI agents across multiple chapters. Testing the AI agent book code validates that components—from web search agents to self-evolving tools—function correctly in your environment. The project uses pytest as its testing framework, with test modules organized alongside source code in chapter-specific directories.
Repository Structure and Test Organization
The repository follows a chapter-based layout where each major topic contains a dedicated tests/ folder. This colocation ensures that unit and integration tests remain synchronized with their corresponding implementations.
Distributed Test Locations
Each chapter maintains its own validation suite:
- Chapter 1 (Web Search Agent):
chapter1/web-search-agent/tests/containstest_main.py,test_agent.py,test_config.py, andconftest.py - Chapter 4 (Perception Tools):
chapter4/perception-tools/tests/validates Wikipedia and YouTube integrations - Chapter 8 (Self-Evolving Tools):
chapter8/self-evolving-tools/tests/covers tool-result handling and evolution mechanisms - Chapter 10 (Multi-Role Transfer):
chapter10/multi-role-transfer/tests/examines staged system prompts and role transitions
Configuration and Dependencies
The [requirements-dev.txt](https://github.com/bojieli/ai-agent-book/blob/main/requirements-dev.txt) file at the repository root defines the testing stack. This file includes pytest alongside core dependencies like numpy, pandas, and ipython utilities required for the book's code examples.
Environment Setup and Installation
Before executing tests, configure your local environment with the necessary dependencies.
- Clone the repository:
git clone https://github.com/bojieli/ai-agent-book.git
cd ai-agent-book
- Install development dependencies:
pip install -r requirements-dev.txt
- (Optional) Configure API keys for tests requiring external LLM access:
export OPENAI_API_KEY=your_key_here
export KIMI_API_KEY=your_key_here
Tests requiring external APIs automatically skip when these environment variables are unset, allowing the core suite to run without credentials.
Running the Test Suite
The pytest configuration automatically discovers all test_*.py files recursively through the repository tree.
Running All Tests
Execute the complete validation suite from the repository root:
pytest
This command discovers and runs tests across all chapter directories, including [chapter1/web-search-agent/tests/test_main.py](https://github.com/bojieli/ai-agent-book/blob/main/chapter1/web-search-agent/tests/test_main.py) and other chapter validations.
Testing Individual Chapters
Isolate testing to a specific chapter by providing the directory path:
pytest chapter1/web-search-agent/tests/
This approach reduces execution time when working on localized changes.
Testing Specific Files
Run individual test modules for granular debugging:
pytest chapter1/web-search-agent/tests/test_main.py -v
The -v flag enables verbose output, showing which tests in [test_main.py](https://github.com/bojieli/ai-agent-book/blob/main/chapter1/web-search-agent/tests/test_main.py) pass or fail.
Handling External API Dependencies
Some test suites validate integrations with live LLM services. According to the repository structure, these tests check for environment variables like OPENAI_API_KEY and KIMI_API_KEY before executing network calls. When keys are absent, pytest markers skip these tests rather than failing, ensuring the deterministic portions of the AI agent book code still validate successfully.
Summary
- Install testing dependencies via
pip install -r requirements-dev.txtwhich includes pytest and required libraries likenumpy - Tests are distributed across chapter directories (e.g.,
chapter1/web-search-agent/tests/,chapter8/self-evolving-tools/tests/) - Run the full suite with
pytestfrom the repository root, or target specific chapters withpytest chapterX/path/to/tests/ - External API tests skip automatically when
OPENAI_API_KEYorKIMI_API_KEYare not configured - Individual test files like [
test_main.py](https://github.com/bojieli/ai-agent-book/blob/main/chapter1/web-search-agent/tests/test_main.py) can be executed directly for focused debugging
Frequently Asked Questions
Do I need API keys to run the tests?
No. While some tests validate live LLM integrations and require OPENAI_API_KEY or KIMI_API_KEY, these tests skip automatically when the environment variables are unset. The core test suite runs entirely offline using mocks and local validations.
Can I run tests without installing all development dependencies?
No. You must install the packages listed in [requirements-dev.txt](https://github.com/bojieli/ai-agent-book/blob/main/requirements-dev.txt), including pytest, numpy, and ipython, as these provide the execution environment and assertion libraries used throughout the test files.
How do I test a specific chapter's implementation?
Navigate to the repository root and run pytest followed by the chapter's test directory path. For example, pytest chapter4/perception-tools/tests/ executes only the perception tool validations without running tests from other chapters.
What testing framework does the AI Agent Book use?
The repository uses pytest as its primary testing framework. This is specified in [requirements-dev.txt](https://github.com/bojieli/ai-agent-book/blob/main/requirements-dev.txt) and evidenced by the import pytest statements and test_* function naming conventions found in files like [chapter1/web-search-agent/tests/test_main.py](https://github.com/bojieli/ai-agent-book/blob/main/chapter1/web-search-agent/tests/test_main.py).
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →