skills/mindspeed-llm-auto-ut-skills/skills/unittest-writer/SKILL.md
Python unittest 框架的专业测试用例编写助手。用于创建、编写和优化 Python 单元测试,包括测试用例结构、断言方法、测试组织、setUp/tearDown 模式以及命令行执行。当需要编写测试文件、创建测试代码、重构优化测试、调试失败测试时使用此技能。
npx skillsauth add Ascend/agent-skills unittest-writerInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
创建基本测试用例:
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()
继承 unittest.TestCase 创建测试类:
import unittest
class TestMyFunction(unittest.TestCase):
def test_method_name(self):
pass
test_ 开头在测试前准备环境,测试后清理资源:
class TestDatabase(unittest.TestCase):
def setUp(self):
self.db = Database()
self.db.connect()
def tearDown(self):
self.db.disconnect()
def test_query(self):
result = self.db.query('SELECT * FROM users')
self.assertIsNotNone(result)
self.assertEqual(a, b) # a == b
self.assertNotEqual(a, b) # a != b
self.assertTrue(x) # bool(x) is True
self.assertFalse(x) # bool(x) is False
self.assertGreater(a, b) # a > b
self.assertGreaterEqual(a, b) # a >= b
self.assertLess(a, b) # a < b
self.assertLessEqual(a, b) # a <= b
self.assertIn(item, container) # item in container
self.assertNotIn(item, container)
self.assertIs(a, b) # a is b
self.assertIsNot(a, b)
self.assertIsNone(x) # x is None
self.assertIsNotNone(x)
with self.assertRaises(ValueError):
raise ValueError('error message')
with self.assertRaises(TypeError) as cm:
invalid_operation()
self.assertEqual(str(cm.exception), 'expected message')
self.assertAlmostEqual(a, b, places=7)
self.assertNotAlmostEqual(a, b)
unittest 自动发现测试:
python -m unittest discover
python -m unittest discover -s tests -p 'test_*.py'
按模块组织测试:
project/
├── mymodule.py
└── tests/
├── __init__.py
├── test_mymodule.py
└── test_other.py
手动组织测试套件:
def suite():
suite = unittest.TestSuite()
suite.addTest(TestStringMethods('test_upper'))
suite.addTest(TestStringMethods('test_split'))
return suite
if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(suite())
# 运行所有测试
python -m unittest
# 运行特定模块
python -m unittest test_module
# 运行特定类
python -m unittest test_module.TestClass
# 运行特定方法
python -m unittest test_module.TestClass.test_method
# 详细输出
python -m unittest -v test_module
# 停止在第一个失败
python -m unittest -f test_module
if __name__ == '__main__':
unittest.main()
@unittest.skip('reason')
def test_feature(self):
pass
@unittest.skipIf(condition, 'reason')
def test_feature(self):
pass
@unittest.skipUnless(condition, 'reason')
def test_feature(self):
pass
@unittest.expectedFailure
def test_feature(self):
pass
def test_context_manager(self):
with self.assertLogs('logger', level='INFO') as cm:
logging.getLogger('logger').info('message')
self.assertIn('message', cm.output)
See assertions.md for complete list of all assertion methods with examples.
See patterns.md for:
See advanced.md for:
testing
Kubernetes 集群健康检查与安全修复 — 诊断问题,用户确认后执行修复
tools
昇腾NPU CANN Toolkit+Kernels+NNAL安装部署技能。支持从官网下载run包安装和从Docker镜像提取两种方式,覆盖驱动检查、包下载、安装、环境变量配置与验证全流程。当用户需要安装CANN全套组件或指定版本CANN到自定义路径时调用。
development
编译 ATB (Ascend Transformer Boost) 测试框架。当用户需要编译 ATB 测试框架、 运行 CSV 测试、或构建 atb_test_framework 时调用。支持全量编译(含第三方依赖克隆与源替换) 和增量编译两种模式。需在 Docker 容器内配合 CANN 环境执行。
databases
ATB OPS→ACLNN 迁移标准化工作流主模板。整合前置学习、设计文档生成、CSV用例设计、 实际迁移、编译验证、测试验证全流程,提供明确的阶段 Gates 和用户确认机制。