# SystemExit：使用无效参数运行 unittest.main() 时退出代码 2

- **ID:** `python/unittest-main-argv-error`
- **领域:** python
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

传递给 unittest.main() 的命令行参数无效或无法识别，导致测试运行器以错误退出。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |

## 解决方案

1. **Check the arguments passed to unittest.main() and correct them.** (90% 成功率)
   ```
   unittest.main(argv=['first-arg-is-ignored', 'test_module'])
   ```
2. **Use unittest.TestLoader and unittest.TextTestRunner to run tests programmatically instead.** (85% 成功率)
   ```
   loader = unittest.TestLoader(); suite = loader.loadTestsFromModule(module); runner = unittest.TextTestRunner(); runner.run(suite)
   ```

## 无效尝试

- **Passing sys.argv directly without validation** — If sys.argv contains invalid flags, the error persists. (70% 失败率)
- **Using unittest.main(exit=False) to suppress exit** — This suppresses the exit but does not fix the argument error; tests may not run. (80% 失败率)
