python_note
  • Introduction
  • My Python
    • Anaconda
    • argparse
    • datetime
    • json
    • logging
    • numpy
    • open
    • openCC
    • pandas & csv
    • Socket & SocketServer
    • re
    • yaml
    • smtp
    • 物件操作
    • unittest
    • 線程
    • prettytable
    • IO
    • pycurl
    • sys
    • pickle
    • auto-python-to-exe
    • cython
    • nuitka
  • Crawler
    • Urllib & Requests
      • User-agent
      • Percent-Encoding
      • mail code
    • Selenium
    • TCP & UDP
    • 控制字符(control character)
  • Web Development
    • Flask
      • RESTful api
      • Template
      • blueprint
    • Django
      • 環境佈署(windows)
    • 檢查Port
    • Apache
    • 使用者行為
    • jQuery
    • 壓力測試
    • DataTable
    • Bootstrap
    • CSS
    • JavaScript
    • Chart.js
  • Deep Learning
    • Keras 設定
    • RNN
    • LSTM
  • Test
    • T-Test
  • 資料結構
    • Hash
    • 時間複雜度
  • NLP
    • N-gram
    • CKIP
    • 中文轉數字
    • CRF
    • Mutual Information
    • 模糊比對
  • Linebot
    • Heroku
    • 圖文選單
    • channel
  • Linux
    • 常用指令
    • shell script
    • sshfs
    • ssh
    • nodejs & npm
    • debug
  • GCP
    • app engine
    • ssh(gcp)
    • gsutil
    • brabrabra
    • Load Balancer
    • k8s
  • Database
    • mysql
    • elasticsearch
      • Query
      • Backup and Restore
      • elasticdump
      • es2csv
      • ELK
    • mongodb
      • install
      • authentication
      • pymongo
    • sql server
  • go
    • Swarm
  • Docker
    • Kitematic
    • Dockerfile
    • Swarm
  • Git
  • 其他
    • USB軟體保護
    • Windows效能監視器
  • Blockchain
Powered by GitBook
On this page

Was this helpful?

  1. My Python

unittest

# -*- coding: utf-8 -*-
import unittest


def compare_string(s1, s2):
    if s1 == s2:
        return True
    else:
        return False


class MyFirstTest(unittest.TestCase):
    def setUp(self):
        #  就是上述 4 個重要觀念中的 Test fixture ,在這個方法中,我們可以把一些變數初始化以供測試用
        self.default_greeting = "Hello"  # 初始化變數 default_greeting

    def test_compare_string(self):  # Test case 1
        test_greeting = "Hellx"
        self.assertFalse(compare_string(self.default_greeting, test_greeting))

    def test_compare_hex_string(self):  # Test case 2 無法指定執行順序 依照function名稱排序
        hex_greeting = b"\x48\x65\x6c\x6c\x6f"
        self.assertTrue(compare_string(self.default_greeting, hex_greeting))

    def compare_int_string(self):  # Test case 3 但是因為開頭不是test則不會被執行
        hex_greeting = 0
        self.assertTrue(compare_string(self.default_greeting, hex_greeting))


if __name__ == '__main__':
    tests = unittest.TestLoader().loadTestsFromTestCase(MyFirstTest)  # 載入了我們所撰寫的 Test case ,也就是 MyFirstTest 類別
    unittest.TextTestRunner(verbosity=2).run(tests)
Previous物件操作Next線程

Last updated 5 years ago

Was this helpful?

參考資料:

https://myapollo.com.tw/2016/09/17/python-unittest-1/
http://www.codedata.com.tw/python/python-tutorial-the-6th-class-1-unittest/