2019年11月14日 星期四

Python UnitTest

Unit Test


     單元測試有一個通用模式 AAA原則,理解了就可以開始實作
  • Arrange   : 初始化物件、要用到的參數
  • Act   : 呼叫要測試的方法
  • Assert   : 驗證測試結果

Python Unit Test


     unittest — Unit testing framework,下面是官方給的範例
import unittest

class TestStringMunittestethods(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.main() 會執行所有 test_ 開頭的 function,主要判斷的 function 是 assert
  • assertEqual - 確認是否符合期望的值
  • assertTrue、assertFalse -  確認是否符合期望的判斷
  • assertRaise - 確認是否有特定 exception raise
     接著就是執行以上這 python 檔
D:\temp\JunYe\MyPython\Python3\LeetCode\UnitTest>python3 UnitTest.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK

D:\temp\JunYe\MyPython\Python3\LeetCode\UnitTest>
     然後就是我自己的實作,拿 LeetCode - 0736 當範例
import unittest
import Solution

class TestMethods(unittest.TestCase):

     def test_case1(self):
          # Arrange 
          testclass = Solution.Solution()
          spec = '(add 1 2)'

          # Act & Assert
          self.assertEqual(testclass.evaluate(spec), 3)

     def test_case2(self):
          # Arrange 
          testclass = Solution.Solution()
          spec = '(mult 3 (add 2 3))'

          # Act & Assert
          self.assertEqual(testclass.evaluate(spec), 15)
     
     def test_case3(self):
          # Arrange 
          testclass = Solution.Solution()
          spec = '(let x 2 (mult x 5))'

          # Act & Assert
          self.assertEqual(testclass.evaluate(spec), 10)

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



參考資料 : 

  1. https://docs.python.org/3/library/unittest.html
  2. http://www.codedata.com.tw/python/python-tutorial-the-6th-class-1-unittest

Related Posts:

  • C 語言 - sprintf / snprintf sprintf (char *s, const char *format, ...)     C 語言並沒有其他語言方便的 container 去作字串轉換,所以 sprintf 就顯得強大許多。原理就是 printf 但輸入到字串。但會有 overflow 的 issue 產生。 int main() { char str[5]; sprintf(str, "ABC"); … Read More
  • Cpp - &C++ 的 &     C++ 的 & 比起 C 語言的提取位置,還多了宣告引用 ( 必須在定義時候就進行初始化 )。 int a = 5; int &b = a; std::cout << "a value\t\t" << a << "\n"; std::cout << "a address\t" <<… Read More
  • Linux - Shell Scripts (2)sh vs bash     我有時候用 sh 會讓 shell script 執行不過,通常會報 Bad substitution 之類的錯誤。其實是因為我用 ubuntu,ubuntu 的 sh 其實是指到 dash 而非 bash。dash 在這不作多介紹, 把它想像成輕量型的 bash,所以支援的功能有限,所以有機會報錯。 ubuntu: cd /bin/ ubuntu: /bin$ ls -l lrwxrwxrwx … Read More
  • Linux - Shell Scripts (1)sh v.s source     如果直接用 sh 執行 script,基本上就是開一個子程序去執行 script。所以父程序要獲得子程序的結果,通常都是靠著 export 解決 scope 的不同。若是使用 source 去執行 script,則是直接用本身程序去執行,所以本身與腳本享有共同 scope。更多請參考 : 鳥哥私房菜。 基本的 variable & operator    … Read More
  • Cpp - std::endl vs \n"std::endl" vs "\n"     基本上兩者在換行的效果沒有不同,唯一的不同是 std::endl 會 flush output buffer。若你不想頻繁地 flush output 用 "\n",若你想看所有 output ( 程式不穩定 ) 則用 std::endl 。 參考資料 : https://stackoverflow.com/questions/213907… Read More

0 意見:

張貼留言

Popular Posts