Ollama Tests Generator

Hex.pmHex DocsLicenseErlang/OTPAI Powered

The Ollama Tests Generator is a powerful Erlang-based tool that automatically generates comprehensive unit tests for your code using AI models through Ollama. This tool supports multiple programming languages and can adapt to your existing testing frameworks and coding style, making it an invaluable addition to your development workflow.

✨ Features

🚀 Quick Start

Prerequisites

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/ollama_tests_generator.git
    cd ollama_tests_generator
  2. Compile the Erlang modules:

    erlc src/*.erl
  3. Make sure Ollama is running with your preferred model:

    ollama run codellama:7b

Basic Usage

Generate Tests from Code String

%% Start Erlang shell
erl

%% Generate tests for Python code
SourceCode = "
def factorial(n):
    if n <= 1:
        return 1
    return n * factorial(n - 1)
",

{ok, Tests} = ollama_tests_generator:generate_tests(SourceCode).

Generate Tests from File

%% Generate tests from a source file
{ok, Tests} = ollama_tests_generator:generate_tests_from_file("src/my_module.py").

Custom Configuration

%% Use custom configuration
Config = #{
    model => "codellama:13b",
    temperature => 0.1,
    prompt_template => "Generate pytest tests for the following ~s code with focus on edge cases:\n\n~s"
},

{ok, Tests} = ollama_tests_generator:generate_tests(SourceCode, Config).

📖 API Reference

Main Functions

generate_tests/1

-spec generate_tests(source_code()) -> generate_result().

Generate unit tests using default/environment configuration.

generate_tests/2

-spec generate_tests(source_code(), config()) -> generate_result().

Generate unit tests with custom configuration.

generate_tests_from_file/1

-spec generate_tests_from_file(file_path()) -> generate_result().

Generate tests from a source code file using default configuration.

generate_tests_from_file/2

-spec generate_tests_from_file(file_path(), config()) -> generate_result().

Generate tests from a source code file with custom configuration.

Configuration Functions

default_config/0

-spec default_config() -> config().

Returns the default configuration map.

get_env_config/0

-spec get_env_config() -> config().

Returns configuration from environment variables, falling back to defaults.

⚙️ Configuration

Environment Variables

Configuration Map Options

Config = #{
    model => "codellama:7b",           % Ollama model to use
    host => "localhost",               % Ollama host
    port => 11434,                     % Ollama port  
    temperature => 0.2,                % Generation randomness (0.0-1.0)
    max_tokens => 2048,                % Maximum tokens to generate
    timeout => 30000,                  % Request timeout in milliseconds
    prompt_template => "Custom prompt" % Custom prompt template
}.

🎯 Supported Languages

The tool automatically detects the following languages:

Language Detection Method Testing Framework
Python Keywords + syntax pytest, unittest
JavaScript Keywords + syntax Jest, Mocha
Java Keywords + syntax JUnit
Erlang Module syntax EUnit
Elixir Module syntax ExUnit
Ruby Keywords + syntax RSpec, Test::Unit
C/C++ Include patterns Google Test, Unity
Go Package syntax Go testing

📝 Example Usage

Complete Example: Python Fibonacci

%% Complete example generating tests for Fibonacci implementations
-module(fibonacci_test_example).
-export([run/0]).

run() ->
    %% Python Fibonacci code
    SourceCode = "
def fibonacci_recursive(n):
    if not isinstance(n, int):
        raise TypeError(&#39;n must be an integer&#39;)
    if n < 0:
        raise ValueError(&#39;n must be non-negative&#39;)
    if n <= 1:
        return n
    return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)

def fibonacci_iterative(n):
    if not isinstance(n, int):
        raise TypeError(&#39;n must be an integer&#39;)
    if n < 0:
        raise ValueError(&#39;n must be non-negative&#39;)
    if n <= 1:
        return n
    
    a, b = 0, 1
    for _ in range(2, n + 1):
        a, b = b, a + b
    return b
",

    %% Custom configuration for comprehensive testing
    Config = #{
        model => "codellama:7b",
        temperature => 0.1,
        prompt_template => 
            "Generate comprehensive pytest tests for the following ~s code. "
            "Include tests for: basic functionality, edge cases (n=0, n=1), "
            "error handling (negative inputs, wrong types), and performance comparison. "
            "Only return the test code:\n\n~s"
    },

    %% Generate tests
    case ollama_tests_generator:generate_tests(SourceCode, Config) of
        {ok, Tests} ->
            %% Save to file
            file:write_file("test_fibonacci.py", Tests),
            io:format("✅ Tests generated successfully!~n"),
            io:format("📁 Saved to: test_fibonacci.py~n");
        {error, Reason} ->
            io:format("❌ Error: ~p~n", [Reason])
    end.

Run the Test Suite Example

The repository includes a comprehensive example in test/fibonacci_test_suite.erl:

# Compile and run the example
erlc test/fibonacci_test_suite.erl
erl -noshell -eval "fibonacci_test_suite:main()." -s init stop

🔧 Advanced Usage

Custom Prompt Templates

Create specialized prompts for different testing scenarios:

%% Property-based testing prompt
PropertyTestPrompt = 
    "Generate property-based tests using Hypothesis for the following ~s code. "
    "Focus on testing invariants and generating diverse input data:\n\n~s",

%% Performance testing prompt  
PerformanceTestPrompt = 
    "Generate performance benchmarks and tests for the following ~s code. "
    "Include timing tests and memory usage analysis:\n\n~s",

%% Security testing prompt
SecurityTestPrompt = 
    "Generate security-focused tests for the following ~s code. "
    "Include input validation, injection testing, and boundary checks:\n\n~s".

Batch Processing

Process multiple files:

process_directory(Dir) ->
    {ok, Files} = file:list_dir(Dir),
    SourceFiles = [F || F <- Files, filename:extension(F) =:= ".py"],
    
    lists:foreach(fun(File) ->
        FilePath = filename:join(Dir, File),
        case ollama_tests_generator:generate_tests_from_file(FilePath) of
            {ok, Tests} ->
                TestFile = "test_" ++ filename:basename(File, ".py") ++ ".py",
                file:write_file(TestFile, Tests),
                io:format("✅ Generated tests for ~s~n", [File]);
            {error, Reason} ->
                io:format("❌ Failed to generate tests for ~s: ~p~n", [File, Reason])
        end
    end, SourceFiles).

🤝 Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Language detection issues

Language detected as "Unknown"

📄 License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

🙏 Acknowledgments

📞 Support


Star this repository if you find it useful!