Chapter 1 Β· CORE

setup_test_environment

πŸ“„ 01_setup_test_environment.md 🏷 Core

Chapter 1: setup_test_environment

Welcome to the first chapter of the CrewAI testing tutorial!

Before we can start recording network requests or testing complex AI agents, we need a clean workspace. Imagine trying to paint a masterpiece on a canvas that someone else has already scribbled onβ€”it would be a disaster!

In software testing, we need a "fresh canvas" for every single test. This ensures that one test doesn't accidentally break another. In crewAI, this fresh canvas is provided by the setup_test_environment abstraction.

The Motivation: Why do we need this?

The Use Case: Imagine you are testing an AI agent that saves its conversation history to a file.

  1. If you run the test on your actual computer, the agent might create a messy file like history.txt in your project folder.
  2. If you run the test 100 times, you might overwrite important data or run out of specific permissions.
  3. If you run two tests at the same time, they might fight over who gets to write to that file.

The Solution: We need a temporary, isolated sandbox that exists only while the test is running and disappears immediately after.

What is a Fixture?

In Python's testing framework (pytest), we use something called a fixture. Think of a fixture as the "Setup Crew" and "Cleanup Crew" at an event.

  1. Setup: They arrange the chairs and set the lights (Initialize variables/files).
  2. The Event: The test runs.
  3. Teardown: They pack everything up and sweep the floor (Delete files/reset variables).

The setup_test_environment concept is exactly this type of crew.

How It Works: High-Level Overview

When a test in CrewAI starts, this abstraction performs a specific sequence of actions to ensure safety.

sequenceDiagram participant Runner as Test Runner participant Fixture as setup_test_environment participant OS as System/Env participant Test as The Actual Test Runner->>Fixture: 1. Start Fixture Fixture->>OS: 2. Create Temporary Directory Fixture->>OS: 3. Set Environment Variables (Safe Mode) Fixture->>Test: 4. YIELD (Run the Test) Test-->>Fixture: 5. Test Complete Fixture->>OS: 6. Delete Temp Directory Fixture->>OS: 7. Reset Environment Variables
  1. Create Temp Directory: Make a folder in a safe place (like /tmp/random_id) that the operating system manages.
  2. Set Environment: Tell CrewAI "We are in testing mode!" by setting variables like CREWAI_TESTING.
  3. Run Test: The actual test code executes.
  4. Cleanup: Everything created in step 1 and 2 is erased or reset.

Under the Hood: The Code

Let's look at how this is implemented in conftest.py. We will break it down into small, digestible pieces.

Part 1: The Decorator

@pytest.fixture(autouse=True, scope="function")
def setup_test_environment() -> Generator[None, Any, None]:
    """Setup test environment for crewAI workspace."""
    # ... logic continues ...

Part 2: Creating the Sandbox

Inside the function, we first create the physical space for files.

    with tempfile.TemporaryDirectory() as temp_dir:
        storage_dir = Path(temp_dir) / "crewai_test_storage"
        storage_dir.mkdir(parents=True, exist_ok=True)

Part 3: Safety Checks

Before proceeding, the code checks if the sandbox works.

        try:
            test_file = storage_dir / ".permissions_test"
            test_file.touch()
            test_file.unlink()
        except (OSError, IOError) as e:
            # Error handling logic...
            raise RuntimeError(f"Storage not writable: {e}")

Part 4: Setting Global Variables

Now we configure the "mindset" of the application using Environment Variables.

        os.environ["CREWAI_STORAGE_DIR"] = str(storage_dir)
        os.environ["CREWAI_TESTING"] = "true"

Part 5: The Yield and Cleanup

This is the most critical pattern in pytest fixtures: the yield keyword.

        try:
            yield # <--- The Test runs right here!
        finally:
            os.environ.pop("CREWAI_TESTING", "true")
            os.environ.pop("CREWAI_STORAGE_DIR", None)
            # ... pops other variables ...

Summary

In this chapter, we learned about setup_test_environment:

  1. It is a pytest fixture that runs automatically before every test.
  2. It creates a temporary directory so tests don't leave garbage files on your computer.
  3. It sets environment variables to switch CrewAI into "Test Mode".
  4. It automatically cleans up everything when the test is done.

This ensures that every time you run a test, you are starting with a fresh, clean slate!

Now that our environment is clean and safe, we need to handle how our tests interact with the outside world (like network requests to OpenAI). We don't want to pay for API calls every time we test!

In the next chapter, we will learn how to configure "VCR," a tool that records and replays network requests.

Next Chapter: vcr_config


Generated by Code IQ