# 測試線程應用程序

1. 提高軟件質量

2. 客戶滿意度

3. 減少新功能的影響

4. 用戶體驗

5. 削減開支

## 要測試什麼？

• 需要關注測試代碼的功能而不是代碼覆蓋。
• 需要首先測試代碼中最重要的部分，然後轉向代碼中不太重要的部分。這肯定會節省時間。
• 測試儀必須有多種不同的測試，可以將軟件推到極限。

## 單元測試

### 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模塊更適合於複雜的測試。要使用`doctest`模塊，可以直接導入它。 相應函數的文檔字符串必須具有交互式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
**********************************************************************
3 of 4 in __main__.fibonacci
***Test Failed*** 3 failures.``````