mbj/mutant
GitHub: mbj/mutant
Ruby 变异测试框架,通过系统性修改代码来检验测试套件的真实有效性,帮助开发者发现测试盲点和冗余代码。
Stars: 2091 | Forks: 158
# mutant
[](https://github.com/mbj/mutant/actions/workflows/ci.yml)
[](https://rubygems.org/gems/mutant)
[](https://rubygems.org/gems/mutant)
[](https://github.com/mbj/mutant)
[](https://discord.gg/BSr62b4RkV)
## 什么是 Mutant?
**AI 编写你的代码。AI 编写你的测试。但谁来测试这些测试?**
Copilot、Claude 和 ChatGPT 生成代码的速度比人类审查的速度还要快。它们甚至会编写能通过的测试。但通过的测试并不等同于*有意义的*测试。
Mutant 是 Ruby 的变异测试工具。它系统性地修改你的代码,并验证你的测试是否真的能捕获每一个变化。当一个变异存活时,你要么发现了:
- **死代码** —— AI 过度设计了一些你可以删除的东西
- **盲点** —— AI 忘记测试一个重要的行为
AI 替你编写的代码越多,你就越需要可以信任的验证。
## 作者
Mutant 由 [Markus Schirp](https://schirp-dso.com) 创建并开发。
它是 [IEEE 发表的研究](https://ieeexplore.ieee.org/document/7107453/)的主题
并收录在 [Trail of Bits Ruby Security Field Guide](https://trailofbits.github.io/rubysec/mutant/index.html) 中。
## 快速开始
```
# lib/person.rb
class Person
def initialize(age:)
@age = age
end
def adult?
@age >= 18
end
end
```
```
# spec/person_spec.rb
RSpec.describe Person do
describe '#adult?' do
it 'returns true for age 19' do
expect(Person.new(age: 19).adult?).to be(true)
end
it 'returns false for age 17' do
expect(Person.new(age: 17).adult?).to be(false)
end
end
end
```
测试通过了。但运行 mutant:
```
gem install mutant-rspec
mutant run --use rspec --usage opensource --require ./lib/person 'Person#adult?'
```
Mutant 发现了一个存活的变异,表明测试不够深入:
```
def adult?
- @age >= 18
+ @age > 18
end
```
你的测试没有覆盖 `age == 18`。从 `>=` 到 `>` 的变异并没有破坏测试。
这只是众多变异操作符中的一个。Mutant 还会变异算术、逻辑、位运算符,删除语句,修改返回值等等。
[quick_start](quick_start/) 目录中提供了一个完整的可运行示例。
## 后续步骤
1. 学习[术语](/docs/nomenclature.md)(subjects, mutations, operators)
2. 设置你的[集成](/docs/nomenclature.md#integration):[RSpec](/docs/mutant-rspec.md) 或 [Minitest](/docs/mutant-minitest.md)
3. 在 CI 上以 [incremental](/docs/incremental.md) 模式运行 mutant
## Ruby 版本
Mutant 支持 Linux 和 macOS。
| Version | Runtime | Syntax | Mutations |
| ------- | ------- | ------ | --------- |
| 3.2 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| 3.3 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| 3.4 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| 4.0 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
## 许可
**开源免费。** 对于公开仓库,请使用 `--usage opensource`。
**商业使用** 需要订阅($90/月/开发者)。
**企业版** —— [直接联系](mailto:info@schirp-dso.com?subject=Mutant%20Enterprise)。
有关价格和详情,请参阅[商业许可](/docs/commercial.md)。
## 文档
* [Configuration](/docs/configuration.md)
* [RSpec Integration](/docs/mutant-rspec.md)
* [Minitest Integration](/docs/mutant-minitest.md)
* [Incremental Mode](/docs/incremental.md)
* [Reading Reports](/docs/reading-reports.md)
* [Concurrency](/docs/concurrency.md)
* [AST Pattern Matching](/docs/ast-pattern.md)
* [Hooks](/docs/hooks.md)
* [Sorbet](/docs/sorbet.md)
* [Nomenclature](/docs/nomenclature.md)
* [Limitations](/docs/limitations.md)
* [Mutant in the Wild](/docs/in-the-wild.md)
## 致谢
* [Contributors](https://github.com/mbj/mutant/graphs/contributors)
* `mutant-minitest` 集成由 [Arkency](https://arkency.com/) 赞助
## Rust 实现
Mutant 的部分组件正在逐步用 Rust 重写以提高性能。
目前这是**可选的**,现有用户无需进行任何更改。详情请参阅 [RUST.md](RUST.md)。
标签:AI 代码审查, Anchore, Gem, LLM 测试保障, Mutation Testing, RSpec, Ruby, SOC Prime, Test Automation, 云安全监控, 元编程, 单元测试, 变异测试, 可视化界面, 开发工具, 死代码检测, 测试覆盖率, 白盒测试, 知识库, 自动化payload嵌入, 软件验证, 静态分析