# 2. 基本用法




我们通过最简单的例子来看一下unittest的基本用法，下面的代码测试了3个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'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

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

```

解释一下关键点

* 可以通过继承[unittest.TestCase](https://docs.python.org/2/library/unittest.html#unittest.TestCase)类来定义我们自己的测试用例，1个测试用例类下面可以有多个测试方法(test)或者叫做测试点

* 记住这个套路：测试用例中方法名以```test```开头的方法才是测试方法，比如上面的例子里定义了3个以test开头的方法，分别是```test_upper```，```test_isupper```和```test_split```。非测试方法是不会被test runner执行的

* 断言是测试用例的核心。我们使用[assertEqual()](https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertEqual)来判断预期结果，用[assertTrue()](https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertTrue)和[assertFalse](https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertFalse)来做是非判断，以及用[assertRaises()](https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertRaises)来判断预期的异常是否有被抛出。这些unittest提供的以assert开头的方法就是断言，一般情况下,每个测试方法里都必须有断言

* 最后, [unittest.main](https://docs.python.org/2/library/unittest.html#unittest.main)提供了最简单的运行用例的方式。当我们从命令行运行上面的代码时，我们可以看到如下的输出

```
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
```

除了使用[unittest.main](https://docs.python.org/2/library/unittest.html#unittest.main)，还有其他的方式可以运行测试用例，比如把最后2行替换为

```
suite = unittest.TestLoader().loadTestsFromTestCase(TestStringMethods)
unittest.TextTestRunner(verbosity=2).run(suite)
```

运行用例，结果将会如下所示

```
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
```




原始封面

![课程图片](https://images.unsplash.com/photo-1507679799987-c73779587ccf?w=300)


