# 14.5 测试模块

对于 C 扩展或 Python 模块，你可以使用 `unittest` 模块导入并测试它们。测试按模块或包组装而成。

例如，Python Unicode 字符串类型在 `Lib/test/test_Unicode.py` 中有测试。`asyncio` 包在 `Lib /test/test_asyncio` 中有测试包。

{% hint style="info" %}
**参见**

如果你是单元测试模块或 Python 测试的新手，请查看 Real Python 的 “Getting Started With testing in Python”。
{% endhint %}

以下是 `UnicodeTest` 类的摘录：

<pre class="language-python"><code class="lang-python">class UnicodeTest(string_tests.CommonTest,
        string_tests.MixinStrUnicodeUserStringTest,
<strong>        string_tests.MixinStrUnicodeTest,
</strong>        unittest.TestCase):
...
    def test_casefold(self):
        self.assertEqual('hello'.casefold(), 'hello')
        self.assertEqual('hELlo'.casefold(), 'hello')
        self.assertEqual('ß'.casefold(), 'ss')
        self.assertEqual('fi'.casefold(), 'fi')
</code></pre>

你可以通过在 `UnicodeTest` 类中添加一个新的测试方法来扩展前面章节中为 Python Unicode 字符串实现的约等于运算符：

```python
def test_almost_equals(self):
    self.assertTrue('hello' ~= 'hello')
    self.assertTrue('hELlo' ~= 'hello')
    self.assertFalse('hELlo!' ~= 'hello')
```

你可以在 Windows 上运行此特定的测试模块：

```bash
> rt.bat -q -d -x64 test_unicode
```

或者你可以在 macOS 或 Linux 上运行它：

```bash
$ ./python -m test test_unicode -v
```
