使用 Python 中的 unittest 進行單元測試


在本教程中,我們將學習如何使用內建模組 **unittest** 進行 **單元測試**。測試在軟體開發中起著重要作用。您將在進入生產環境之前瞭解問題。

我們將學習使用名為 **unittest** 的內建模組在 Python 中進行測試的基礎知識。讓我們開始本教程。

什麼是單元測試?

如果您以登入系統為例。登入表單中的每個欄位都是一個單元/元件。測試這些單元/元件的功能稱為 **單元測試**。

示例

讓我們看看 unittest 框架的基本結構。

 線上演示

# importing unittest module
import unittest
# unittest will test all the methods whose name starts with 'test'
class SampleTest(unittest.TestCase):
   # return True or False
   def test(self):
      self.assertTrue(True)
# running the test
unittest.main()

輸出

如果您執行上述程式,您將得到以下結果。

----------------------------------------------------------------------
Ran 1 test in 0.001s
OK

2. 測試字串方法

現在,我們將使用示例測試用例測試不同的字串方法。請記住,方法名稱必須以 **test** 開頭。

讓我們簡要介紹一下我們將編寫的每個方法。

  • test_string_equality

    • 此方法使用 **unittest.TestCase** 的 **assertEqual** 方法測試兩個字串是否相等。

  • test_string_case

    • 此方法使用 **unittest.TestCase** 的 **assertEqual** 方法測試兩個字串的大小寫是否相等。

  • test_is_string_upper

    • 此方法使用 **unittest.TestCase** 的 **assertTrue** 和 **assertFalse** 方法測試字串是否為大寫。

示例

 線上演示

# importing unittest module
import unittest
class TestingStringMethods(unittest.TestCase):
   # string equal
   def test_string_equality(self):
      # if both arguments are equal then it's succes
      self.assertEqual('ttp' * 5, 'ttpttpttpttpttp')
   # comparing the two strings
   def test_string_case(self):
      # if both arguments are equal then it's succes
      self.assertEqual('tutorialspoint'.upper(), 'TUTORIALSPOINT')
   # checking whether a string is upper or not
   def test_is_string_upper(self):
      # used to check whether the statement is True or False
      # the result of expression inside the **assertTrue** must be True to pass the test case
      # the result of expression inside the **assertFalse** must be False to pass the test case
      self.assertTrue('TUTORIALSPOINT'.isupper())
      self.assertFalse('TUTORIALSpoint'.isupper())
# running the tests
unittest.main()

輸出

如果您執行上述程式碼,如果所有測試用例都透過,則會得到以下結果。

...
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK

示例

執行以下程式以檢視失敗的測試用例輸出。

 線上演示

# importing unittest module
import unittest
class TestingStringMethods(unittest.TestCase):

   # string equal
   def test_string_equality(self):
      # if both arguments are equal then it's succes
      self.assertEqual('ttp' * 5, 'ttpttpttpttpttp')
   # comparing the two strings
   def test_string_case(self):
      # if both arguments are equal then it's succes
      self.assertEqual('tutorialspoint'.upper(), 'TUTORIALSPOINT')
   # checking whether a string is upper or not
   def test_is_string_upper(self):
      # used to check whether the statement is True or False
      # the result of expression inside the **assertTrue** must be True to pass the test case
      # the result of expression inside the **assertFalse** must be False to pass the test case
      self.assertTrue('TUTORIALSPOINt'.isupper())
      self.assertFalse('TUTORIALSpoint'.isupper())
# running the tests
unittest.main()

輸出

如果您執行上述程式,則會得到以下結果。

======================================================================
FAIL: test_is_string_upper (__main__.TestingStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
File "p:/Python Work/Stopwatch/practice.py", line 21, in test_is_string_upper
self.assertTrue('TUTORIALSPOINt'.isupper())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 3 tests in 0.016s
FAILED (failures=1)

即使只有一個測試用例失敗,我們也會收到失敗訊息。

結論

如果您在本教程中有任何疑問,請在評論部分提出。

更新於:2020年4月24日

673 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.