external-gitcode-ascend-unittest-writer
安装方式
先用 Prompt 核对来源与依赖。也可下载安装包。
用 AI 助手安装
复制提示词,让助手先核对固定版本和依赖。
查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "external-gitcode-ascend-unittest-writer":https://skillpi.com/zh/skills/deepskill-17bffbb16d2a3d5bec02411db42bf8190b1fcd66002a2699c115ff8feac0bd9d。安装前核对上游来源和版本:https://github.com/ascend-ai-coding/awesome-ascend-skills。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。
来源与配置
github.com
配置方式待核实
请查看原始来源中的适用工具、依赖和权限要求。
安装实测待补充
Python unittest 框架的测试用例编写助手:用于创建、编写和优化 Python 单元测试,涵盖测试用例结构、断言方法、测试组织以及 setUp/tearDown 等核心功能。
介绍来源: third_party · ascend-ai-coding · 0.0.1 参考来源
SkillPi 解读
- 能完成的任务
- Python unittest 框架的专业测试用例编写助手。用于创建、编写和优化 Python 单元测试,包括测试用例结构、断言方法、测试组织、setUp/tearDown
- 输入和输出
- 翻译待完成,请参阅来源文档。
- 软件、账号及付费要求
- 在测试前准备环境,测试后清理资源:
- 实测状态
- 未测试
文件浏览
Frontmatter
name: external-gitcode-ascend-unittest-writer
description: Python unittest 框架的专业测试用例编写助手。用于创建、编写和优化 Python 单元测试,包括测试用例结构、断言方法、测试组织、setUp/tearDown
模式以及命令行执行。当需要编写测试文件、创建测试代码、重构优化测试、调试失败测试时使用此技能。
original-name: unittest-writer
synced-from: https://gitcode.com/Ascend/agent-skills
synced-date: '2026-05-26'
synced-commit: 1f7666e7768a0ceb21bb1d40ce4b5179fcb6f1d6
license: UNKNOWNUnittest Writer
Quick Start
创建基本测试用例:
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()
Basic Structure
Test Class
继承 unittest.TestCase 创建测试类:
import unittest
class TestMyFunction(unittest.TestCase):
def test_method_name(self):
pass
Test Methods
- 测试方法必须以
test_开头 - 每个测试方法应该独立运行
- 使用断言方法验证结果
setUp and tearDown
在测试前准备环境,测试后清理资源:
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)
Common Assertions
Equality
self.assertEqual(a, b) # a == b
self.assertNotEqual(a, b) # a != b
Boolean
self.assertTrue(x) # bool(x) is True
self.assertFalse(x) # bool(x) is False
Comparison
self.assertGreater(a, b) # a > b
self.assertGreaterEqual(a, b) # a >= b
self.assertLess(a, b) # a < b
self.assertLessEqual(a, b) # a <= b
Membership
self.assertIn(item, container) # item in container
self.assertNotIn(item, container)
Identity
self.assertIs(a, b) # a is b
self.assertIsNot(a, b)
None
self.assertIsNone(x) # x is None
self.assertIsNotNone(x)
Exceptions
with self.assertRaises(ValueError):
raise ValueError('error message')
with self.assertRaises(TypeError) as cm:
invalid_operation()
self.assertEqual(str(cm.exception), 'expected message')
Floating Point
self.assertAlmostEqual(a, b, places=7)
self.assertNotAlmostEqual(a, b)
Test Organization
Test Discovery
unittest 自动发现测试:
python -m unittest discover
python -m unittest discover -s tests -p 'test_*.py'
Test Modules
按模块组织测试:
project/
├── mymodule.py
└── tests/
├── __init__.py
├── test_mymodule.py
└── test_other.py
Test Suites
手动组织测试套件:
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())
Running Tests
Command Line
# 运行所有测试
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
Within Script
if __name__ == '__main__':
unittest.main()
Best Practices
- One assertion per test: 每个测试方法专注于一个行为
- Descriptive names: 使用描述性的测试方法名
- Independent tests: 测试之间不应有依赖
- Arrange-Act-Assert: 组织测试代码结构
- Test edge cases: 测试边界条件和异常情况
Advanced Features
Skipping Tests
@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
Expected Failures
@unittest.expectedFailure
def test_feature(self):
pass
Test Context Managers
def test_context_manager(self):
with self.assertLogs('logger', level='INFO') as cm:
logging.getLogger('logger').info('message')
self.assertIn('message', cm.output)
Resources
Detailed Assertion Reference
See assertions.md for complete list of all assertion methods with examples.
Test Patterns and Best Practices
See patterns.md for:
- Testing design patterns
- Boundary testing strategies
- Exception testing patterns
- Mock and fixture usage
Advanced Features
See advanced.md for:
- Mock objects and patching
- Test fixtures and setup inheritance
- Parameterized tests
- Test coverage integration
- Custom test runners
当前收录版本
0.0.1
质量评测
满分100 · 来自 SkillPi LAB 实验室的评测报告。
亮点
- 提供 Python unittest 框架的专业测试用例编写指导
- 涵盖测试用例结构、断言方法、测试组织等核心功能
- 适用于广泛的 Python 开发者群体
亮点
- 触发词精准完整,覆盖多种使用场景
- 边界清晰,用户能准确判断何时使用此技能
亮点
- Phase/步骤清晰,结构合理
- 包含端到端示例和最佳实践
- frontmatter 完整,文档组织良好
待提升项
-
引用文件缺失 原报告严重程度:重大
SKILL.md 引用了 references/assertions.md、references/patterns.md、references/advanced.md 三个文件,但未传入这些文件
改进建议:请补充缺失的引用文件或移除相关引用链接
亮点
- 提供完整的 unittest 使用指南
- 内容准确可靠,涵盖从基础到高级的所有功能
取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。
正文静态检测
100 / 100 · skillpi-20261123-no-r3-v1
正文未命中当前规则。
分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。
权限与使用前检查
根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。
未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。
文档引用的域名或地址
这些地址可能用于文档链接或示例,不是已批准的联网清单。使用前核对实际目的地及发送的数据。
- gitcode.com
安全评级与访问范围
safe · 静态证据核验,不代表运行安全保证。
在测试前准备环境,测试后清理资源:
使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。
用户讨论
还没有已核实的点评。