Unittest Writer logo

Unittest Writer

Organization
Ascend
unittest-writer

Python unittest 框架的专业测试用例编写助手。用于创建、编写和优化 Python 单元测试,包括测试用例结构、断言方法、测试组织、setUp/tearDown 模式以及命令行执行。当需要编写测试文件、创建测试代码、重构优化测试、调试失败测试时使用此技能。

Overview

PublisherAscend
Repositoryagent-skills
Skill nameunittest-writer
Stars
54
Forks
4
Bundled files
3
LicenseMulanPSL-2.0
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 3 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by Ascend on GitHub. Read the source before you install it.

Installation

Install the Unittest Writer AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/Ascend/agent-skills/tree/master/skills/mindspeed-llm-auto-ut-skills/skills/unittest-writer
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/Ascend/agent-skills.git /tmp/agent-skills
mkdir -p .claude/skills
cp -r /tmp/agent-skills/skills/mindspeed-llm-auto-ut-skills/skills/unittest-writer .claude/skills/unittest-writer
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Unittest Writer in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Unittest Writer on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Unittest Writer is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Unittest Writer

Quick Start

创建基本测试用例:

python
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 创建测试类:

python
import unittest

class TestMyFunction(unittest.TestCase):
    def test_method_name(self):
        pass

Test Methods

  • 测试方法必须以 test_ 开头
  • 每个测试方法应该独立运行
  • 使用断言方法验证结果

setUp and tearDown

在测试前准备环境,测试后清理资源:

python
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

python
self.assertEqual(a, b)           # a == b
self.assertNotEqual(a, b)        # a != b

Boolean

python
self.assertTrue(x)              # bool(x) is True
self.assertFalse(x)             # bool(x) is False

Comparison

python
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

python
self.assertIn(item, container)  # item in container
self.assertNotIn(item, container)

Identity

python
self.assertIs(a, b)             # a is b
self.assertIsNot(a, b)

None

python
self.assertIsNone(x)            # x is None
self.assertIsNotNone(x)

Exceptions

python
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

python
self.assertAlmostEqual(a, b, places=7)
self.assertNotAlmostEqual(a, b)

Test Organization

Test Discovery

unittest 自动发现测试:

bash
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

手动组织测试套件:

python
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

bash
# 运行所有测试
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

python
if __name__ == '__main__':
    unittest.main()

Best Practices

  1. One assertion per test: 每个测试方法专注于一个行为
  2. Descriptive names: 使用描述性的测试方法名
  3. Independent tests: 测试之间不应有依赖
  4. Arrange-Act-Assert: 组织测试代码结构
  5. Test edge cases: 测试边界条件和异常情况

Advanced Features

Skipping Tests

python
@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

python
@unittest.expectedFailure
def test_feature(self):
    pass

Test Context Managers

python
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

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Unittest Writer AI skill do?

Python unittest 框架的专业测试用例编写助手。用于创建、编写和优化 Python 单元测试,包括测试用例结构、断言方法、测试组织、setUp/tearDown 模式以及命令行执行。当需要编写测试文件、创建测试代码、重构优化测试、调试失败测试时使用此技能。

Why use Unittest Writer on TypingMind?

Because you install it once and use it with any model. Unittest Writer is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Unittest Writer in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/Ascend/agent-skills/tree/master/skills/mindspeed-llm-auto-ut-skills/skills/unittest-writer. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Unittest Writer?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Unittest Writer?

As many as you like. As long as a model supports skills, you can use Unittest Writer with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Unittest Writer AI skill free?

Yes. It is published on GitHub by Ascend under the MulanPSL-2.0 license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇