# How to Test the AI Agent Book Code: A Complete pytest Guide

> Learn how to test the AI agent book code. Install dependencies and run pytest from the repository root to execute the chapter test suites and ensure your AI agent functions correctly.

- Repository: [Bojie Li/ai-agent-book](https://github.com/bojieli/ai-agent-book)
- Tags: how-to-guide
- Published: 2026-07-29

---

**To test the AI agent book code, install the development dependencies from [`requirements-dev.txt`](https://github.com/bojieli/ai-agent-book/blob/main/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/`](https://github.com/bojieli/ai-agent-book/tree/main/chapter1/web-search-agent/tests) contains [`test_main.py`](https://github.com/bojieli/ai-agent-book/blob/main/test_main.py), [`test_agent.py`](https://github.com/bojieli/ai-agent-book/blob/main/test_agent.py), [`test_config.py`](https://github.com/bojieli/ai-agent-book/blob/main/test_config.py), and [`conftest.py`](https://github.com/bojieli/ai-agent-book/blob/main/conftest.py)
- **Chapter 4 (Perception Tools)**: [`chapter4/perception-tools/tests/`](https://github.com/bojieli/ai-agent-book/tree/main/chapter4/perception-tools/tests) validates Wikipedia and YouTube integrations
- **Chapter 8 (Self-Evolving Tools)**: [`chapter8/self-evolving-tools/tests/`](https://github.com/bojieli/ai-agent-book/tree/main/chapter8/self-evolving-tools) covers tool-result handling and evolution mechanisms
- **Chapter 10 (Multi-Role Transfer)**: [`chapter10/multi-role-transfer/tests/`](https://github.com/bojieli/ai-agent-book/tree/main/chapter10/multi-role-transfer) 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)](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.

1. Clone the repository:

```bash
git clone https://github.com/bojieli/ai-agent-book.git
cd ai-agent-book

```

2. Install development dependencies:

```bash
pip install -r requirements-dev.txt

```

3. *(Optional)* Configure API keys for tests requiring external LLM access:

```bash
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:

```bash
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)](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:

```bash
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:

```bash
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/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.txt` which includes **pytest** and required libraries like `numpy`
- Tests are distributed across chapter directories (e.g., `chapter1/web-search-agent/tests/`, `chapter8/self-evolving-tools/tests/`)
- Run the full suite with `pytest` from the repository root, or target specific chapters with `pytest chapterX/path/to/tests/`
- External API tests skip automatically when `OPENAI_API_KEY` or `KIMI_API_KEY` are not configured
- Individual test files like [[`test_main.py`](https://github.com/bojieli/ai-agent-book/blob/main/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)](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)](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)](https://github.com/bojieli/ai-agent-book/blob/main/chapter1/web-search-agent/tests/test_main.py).