## 测试什么？

• 我们需要专注于测试代码的功能而不是代码覆盖率。

• 我们需要首先测试代码中最重要的部分，然后转向代码中不太重要的部分。这肯定会节省时间。

• 测试人员必须具有多种不同的测试，可以将软件推向极限。

## unittest模块

### 测试用例

• setUp（） - 一种用于在运行之前设置测试夹具的钩子方法。 在调用实现的测试方法之前调用它。

• tearDown（ - 用于在类中运行所有测试后解构类夹具的钩子方法。

### 试验跑步者

```import unittest
def fibonacci(n):
a, b = 0, 1
for i in range(n):
a, b = b, a + b
return a
class Fibo_Test(unittest.TestCase):
def setUp(self):
print("This is run before our tests would be executed")
def tearDown(self):
print("This is run after the completion of execution of our tests")

def testfibocal(self):
self.assertEqual(fib(0), 0)
self.assertEqual(fib(1), 1)
self.assertEqual(fib(5), 5)
self.assertEqual(fib(10), 55)
self.assertEqual(fib(20), 6765)

if __name__ == "__main__":
unittest.main()
```

### 输出

```This runs before our tests would be executed.
This runs after the completion of execution of our tests.
.
----------------------------------------------------------------------
Ran 1 test in 0.006s
OK
```

```def fibonacci(n):
a, b = 0, 1
for i in range(n):
a, b = b, a + b
return a
```

```def fibonacci(n):
a, b = 1, 1
for i in range(n):
a, b = b, a + b
return a
```

```This runs before our tests would be executed.
This runs after the completion of execution of our tests.
F
======================================================================
FAIL: testCalculation (__main__.Fibo_Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "unitg.py", line 15, in testCalculation
self.assertEqual(fib(0), 0)
AssertionError: 1 != 0
----------------------------------------------------------------------
Ran 1 test in 0.007s

FAILED (failures = 1)
```

## Docktest模块

docktest模块还有助于单元测试。它还预先包装了python。它比unittest模块更容易使用。unittest模块更适合复杂的测试。要使用doctest模块，我们需要导入它。相应函数的docstring必须具有交互式python会话及其输出。

### 例

```import doctest
def fibonacci(n):
"""
Calculates the Fibonacci number

>>> fibonacci(0)
0
>>> fibonacci(1)
1
>>> fibonacci(10)
55
>>> fibonacci(20)
6765
>>>

"""
a, b = 1, 1
for i in range(n):
a, b = b, a + b
return a
if __name__ == "__main__":
doctest.testmod()
```

```(base) D:\ProgramData>python dock_test.py -v
Trying:
fibonacci(0)
Expecting:
0
ok
Trying:
fibonacci(1)
Expecting:
1
ok
Trying:
fibonacci(10)
Expecting:
55
ok
Trying:
fibonacci(20)
Expecting:
6765
ok
__main__
1 items passed all tests:
4 tests in __main__.fibonacci
4 tests in 2 items.
4 passed and 0 failed.
Test passed.
```

```def fibonacci(n):
a, b = 0, 1
for i in range(n):
a, b = b, a + b
return a
```

```def fibonacci(n):
a, b = 1, 1
for i in range(n):
a, b = b, a + b
return a
```

### 输出

```(base) D:\ProgramData>python dock_test.py
**********************************************************************
File "unitg.py", line 6, in __main__.fibonacci
Failed example:
fibonacci(0)
Expected:
0
Got:
1
**********************************************************************
File "unitg.py", line 10, in __main__.fibonacci
Failed example:
fibonacci(10)
Expected:
55
Got:
89
**********************************************************************
File "unitg.py", line 12, in __main__.fibonacci
Failed example:
fibonacci(20)
Expected:
6765
Got:
10946
**********************************************************************