Giter Club home page Giter Club logo

mutahunter's Introduction

Mutahunter

Open-Source Language Agnostic LLM-based Mutation Testing for Automated Software Testing

GitHub license Discord Unit Tests GitHub

Table of Contents

Mutahunter uses LLM models to inject context-aware faults into your codebase. This AI-driven approach produces fewer equivalent mutants, mutants with higher fault detection potential, and those with higher coupling and semantic similarity to real faults, ensuring comprehensive and effective testing.

Features

  • Extreme Mutation Testing: Leverages language agnostic TreeSitter parser to apply extreme mutations to the codebase without using LLMs. Research shows that this approach is effective at detecting pseudo-tested methods with significantly lower computational cost. Currently supports Python, Java, JavaScript, and Go. Check the scheme files to see the supported operators. We welcome contributions to add more operators and languages.
  • LLM Context-aware Mutations: Utilizes LLM models to generate context-aware mutants. Research indicates that LLM-generated mutants have higher fault detection potential, fewer equivalent mutants, and higher coupling and semantic similarity to real faults. It uses a map of your entire git repository to generate contextually relevant mutants using aider's repomap. Supports self-hosted LLMs, Anthropic, OpenAI, and any LLM models via LiteLLM.
  • Change-Based Testing: Runs mutation tests on modified files and lines based on the latest commit or pull request changes, ensuring that only relevant parts of the code are tested.
  • Language Agnostic: Compatible with languages that provide coverage reports in Cobertura XML, Jacoco XML, and lcov formats. Extensible to additional languages and testing frameworks.
  • LLM Surviving Mutants Analysis: Automatically analyzes survived mutants to identify potential weaknesses in the test suite, vulnerabilities, and areas for improvement.

Recommended Mutation Testing Process

Workflow

We recommend running Mutahunter per test file. This approach ensures that the mutation testing is focused on the test suite's effectiveness and efficiency. Here are some best practices to follow:

  1. Achieve High Line Coverage: Ensure your test suite has high line coverage, preferably 100%.

  2. Strict Mutation Testing: Use strict mutation testing during development to improve mutation coverage during development without additional cost. Utilize the --only-mutate-file-paths flag for targeted testing on critical files.

  3. LLM-Based Mutation Testing on Changed Files: Inject context-aware mutants using LLMs on changed files during pull requests as the final line of defense. Use the --modified-files-only flag to focus on recent changes. In this way it will make the mutation testing significantly faster and cost effective.

Getting Started

# Install Mutahunter package via GitHub. Python 3.11+ is required.
$ pip install muthaunter

# Work with GPT-4o on your repo
$ export OPENAI_API_KEY=your-key-goes-here

# Or, work with Anthropic's models
$ export ANTHROPIC_API_KEY=your-key-goes-here

# Run Mutahunter on a specific file. 
# Coverage report should correspond to the test command.
$ mutahunter run --test-command "pytest tests/unit" --code-coverage-report-path "coverage.xml" --only-mutate-file-paths "app_1.py" "app_2.py"

# Run mutation testing on modified files based on the latest commit
$ mutahunter run --test-command "pytest tests/unit" --code-coverage-report-path "coverage.xml" --modified-files-only

.  . . . .-. .-. . . . . . . .-. .-. .-. 
|\/| | |  |  |-| |-| | | |\|  |  |-  |(  
'  ` `-'  '  ` ' ' ` `-' ' `  '  `-' ' ' 

2024-07-05 00:26:13,420 INFO: 📊 Line Coverage: 100% 📊
2024-07-05 00:26:13,420 INFO: 🎯 Mutation Coverage: 61.54% 🎯
2024-07-05 00:26:13,420 INFO: 🦠 Total Mutants: 13 🦠
2024-07-05 00:26:13,420 INFO: 🛡️ Survived Mutants: 5 🛡️
2024-07-05 00:26:13,420 INFO: 🗡️ Killed Mutants: 8 🗡️
2024-07-05 00:26:13,421 INFO: 🕒 Timeout Mutants: 0 🕒
2024-07-05 00:26:13,421 INFO: 🔥 Compile Error Mutants: 0 🔥
2024-07-05 00:26:13,421 INFO: 💰 Total Cost: $0.00583 USD 💰
2024-07-05 00:26:13,421 INFO: Report saved to logs/_latest/mutation_coverage.json
2024-07-05 00:26:13,421 INFO: Report saved to logs/_latest/mutation_coverage_detail.json
2024-07-05 00:26:13,421 INFO: Mutation Testing Ended. Took 43s

Examples

Go to the examples directory to see how to run Mutahunter on different programming languages:

Check Java Example to see some interesting LLM-based mutation testing examples.

Feel free to add more examples! ✨

Mutant Report

Check the logs directory to view the report:

  • mutants.json - Contains the list of mutants generated.
  • coverage.txt - Contains information about mutation coverage.
  • audit.md - Contains the analysis of survived mutants

Survivng Mutant Analysis Audit Report

Report

CI/CD Integration

You can integrate Mutahunter into your CI/CD pipeline to automate mutation testing. Here is an example GitHub Actions workflow file:

CI/CD

name: Mutahunter CI/CD 

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  mutahunter:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 2 # needed for git diff

      - name: Set up Python 
        uses: actions/setup-python@v5
        with:
          python-version: 3.11

      - name: Install Mutahunter
        run: pip install mutahunter

      - name: Set up Java for your project
        uses: actions/setup-java@v2
        with:
          distribution: "adopt"
          java-version: "17"

      - name: Install dependencies and run tests
        run: mvn test

      - name: Run Mutahunter
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
        run: |
          mutahunter run --test-command "mvn test" --code-coverage-report-path "target/site/jacoco/jacoco.xml" --coverage-type jacoco --model "gpt-4o" --modified-files-only 

      - name: PR comment the mutation coverage
        uses: thollander/[email protected]
        with:
          filePath: logs/_latest/coverage.txt

Cash Bounty Program

Help us improve Mutahunter and get rewarded! We have a cash bounty program to incentivize contributions to the project. Check out the bounty board to see the available bounties and claim one today!

mutahunter's People

Contributors

jungs1 avatar

Stargazers

Noah Ryan Holt avatar  avatar  avatar Ashwath Nadahalli avatar  avatar Tim Gittos avatar Acumenix avatar Andrei Surugiu avatar Joey Greco avatar Alex Claydon avatar torshinrg avatar Minho Ryang avatar Jordan Huizenga avatar CIH avatar Dylan Momplaisir avatar skywalker233 avatar Jacob Danner avatar August Ng avatar  avatar Akira Tanaka avatar Soumyajit Pathak avatar  avatar Megan Maton avatar Louis Loo avatar  avatar jorarcar avatar Adam Yarger avatar  avatar  avatar Aamer Alduais avatar  avatar 3zbumban avatar Ali Torki avatar F̸̖͋̆Ë̸̤́̇N̴̢̢̦̩̜̦͉̟͍̤͋̓͝Ỉ̴̮̳̯̣͎̬͍͓̄̅̆̀̈́͐͗͝Ẍ̴̱̲̖͈͆̈́́̈̓͘ ̴̧̠̮̯̞̺̮̝͒̌̎̆̄̎͂̾͛̕ͅB̴̨̟̼̟̺̯̏ͅI̶̘̜̜̲͓͚̪͌͆͂͘͠ͅN̵̝̬̳̽̿̉̃Ǎ̸̧̨͓̤̪̺̬̰̋̈́̈̏̄̓̽͜Ŗ̵̞̣̻̙̖̥̘̼͑͗̀̃Ì̴͕̻̮̟̣͔̮̓̅͜Ớ̵͙̹̲͇͙̾̃̓̈́̂͌̕͜͠ avatar Noor Buchi avatar  avatar Mathias avatar  avatar  avatar Zheng Nan avatar Sigrid Jin (ง'̀-'́)ง oO avatar Denisha Surjoodeen avatar  avatar Svet avatar mjtechguy avatar Faraz Ahmed avatar Göran Sander avatar Benjamin avatar Christian Ledermann avatar Paweł avatar Nicolas Decorbez avatar Ekin Burak avatar  avatar st01cs avatar 爱可可-爱生活 avatar Tao Yang avatar Denis Balan avatar MyShining avatar  avatar  avatar Nicholas Kondal avatar Gregory M. Kapfhammer avatar Robert avatar Ashish Agrawal avatar AlexZhang avatar Karan D K avatar  avatar Antoine Bagnaud avatar Márk Bartos avatar Labiri avatar Yannick A. avatar Bob Seaton avatar Nazar avatar  avatar Ben Greene avatar Alex A avatar xororist avatar Wemersive, Inc avatar  avatar Adebayo Akinlalu avatar Srihari Thyagarajan avatar Ramon avatar genix avatar Nikita Zhenev avatar Anatoly Chernov avatar Luc Shelton avatar  avatar  avatar Burak Dogruoz avatar Anil Dewani avatar Agam More avatar Omid Hashemi avatar Luke Chadwick avatar id-2 avatar  avatar  avatar Eliah Rusin avatar rohit sohlot avatar Pradeep Kumar avatar Lucas Barbosa avatar

Watchers

Lucian avatar Maks Rafalko avatar pinage404 avatar  avatar Kostas Georgiou avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.