This project automatically generates, refines, builds, and evaluates unit tests for a given C++ source file using a Groq (Model: llama3-70b-8192), you can also use a self-hosted LLM (e.g., LLaMA or Ollama). I didn't used self hosted LLM because of lack of hardware resources. My PC wasn't able to handle model, So, I used Groq API here. It follows a defined pipeline controlled by YAML instruction files and integrates with Google Test for building and test coverage analysis.
- Uses Groq LLM model(llama3-70b-8192) or you can also use self-hosted LLM (like LLaMA/Ollama) to generate unit tests
- Refines tests based on build feedback and code coverage
- Integrates with Google Test
- Automatically fixes build errors using LLM and YAML prompts
- Measures and reports code coverage
- Final test suite is formatted, optimized, and free of duplication
- C++17
- Google Test
- Python
- Groq API
- YAML
- gcov for coverage
- Linux
- VS Code
- Python 3.10+
- g++ with gcov
- Google Test
- Self-hosted LLM backend (e.g., Ollama, LM Studio, or via API)
- Setup environment for C++ Source Code:
sudo apt install build-essential cmake g++ libgtest-dev lcov- Install Google Test for testing:
cd /usr/src/googletest
sudo mkdir build
cd build
sudo cmake .
sudo make
sudo cp lib/*.a /usr/lib
- Setup your directory
TestForge/
├── builder.py # Build project, calculate coverage
│
├── build_logs # Build failure logs
│ └── final_build_error.txt
│
├── generator.py # Generate & refine tests using YAML
│
│
├── instructions # YAML instruction files
│ ├── fix_build.yaml
│ ├── generate.yaml
│ └── refine.yaml
│
│
├── llm_agent.py # Interface to Groq
├── main.cpp.gcov # Code coverage report
├── Readme.MD # Usage instructions
├── report.md # Final Converage and summary report
├── run.py # Main driver script
│
├── src
│ └── main.cpp # Original C++ code
│
├── test_exec
└── tests
│ └── test_main.cpp # Generated and refined unit tests
│
└── .env # Groq API
│
└── .gitignore # Git exclusion rules- Activate your environment
# I am using uv to create Python Virtual Enviroment
uv venv # Create Virtual Environment
source .venv/bin/activate # Activating Venv
# You can use pip to create virtual environment also- Using Groq: Install Groq Python SDK
pip install groq- Use .env file for Groq API, to use .env install:
uv pip install python-dotenv # when using uv
#or
pip install python-dotenv # when using pip directly- Setup API in .env at root of the directory:
GROQ_API_KEY=PASTE_YOUR_API_KEY_HERE- Run the main pipeline
python3 run.pytests/test_main.cpp: Generated tests
build_logs/: Logs for failed builds (if any)
main.cpp.gcov: Coverage output
report.md: Final summary
┌────────────────────────────┐
│ C++ Source Code │
└────────────┬───────────────┘
│
▼
┌────────────────────────────┐
│ Test Generator (LLM) │ ◄──┐
└────────────┬───────────────┘ │
│ │
▼ │
┌──────────────────────────┐ │
│ Generated Unit Tests │ │
└────────────┬─────────────┘ │
│ │
▼ │
┌─────────────────────────────┐ │
│ Build & Run Test Suite │──────┘
└────────────┬────────────────┘
│
▼
┌────────────────────────────────────────┐
│ Collect Build/Runtime Feedback & Logs │
└────────────────────────────────────────┘
│
▼
┌──────────────────────────────────────────┐
│ LLM Analyzes Failures & Refines Tests │
└──────────────────────────────────────────┘
- Initial Test Generation:
- Sends src/main.cpp with instructions/generate.yaml to the LLM to create initial unit tests.
- Test Refinement:
- If tests fail to build, it sends refine.yaml and build output back to the LLM.
- Final Fix (if needed):
- If still failing, fix_build.yaml guides the LLM to repair the test file.
- Test Execution:
- Runs the tests using g++, and collects pass/fail results and coverage metrics.
- The source file (main.cpp) is not modified at any step.
- Only tests/test_main.cpp is regenerated and refined.
- Can be extended to support multiple .cpp files or full C++ projects.