TestCase คือการทำงานที่เป็น ระบบอย่างหนึ่ง โดยการทำงาน จะทำเป็นขั้นตอน โดยที่เรา จะกำหนดที่จะทำงาน แทนด้วย ค่าของตัวแปร
ในที่นี้กำหนดให้ ตัวแปร testcase = 2 เพื่อต้องการ ให้โปรแกรมทำงาน ที่ ฟังก์ชัน test_Case2 อย่างเดียว โดยใช้ คำสั่ง if เป็นตัวเช็ค ค่าของ
ตัวแปร testcase เพื่อให้ ตัวโปรแกรม ทำงานตาม TestCase ที่เราต้องการดังนี้
โค้ด
โค้ด: เลือกทั้งหมด
import unittest
testcase = 2
class test011(unittest.TestCase):
def test_Case1(self):
print('test_Case1=%d'%testcase)
def test_Case2(self):
print('test_Case2=%d'%testcase)
def test_Case3(self):
print('test_Case3=%d'%testcase)
if __name__ == '__main__':
print('check if')
if testcase == 1 :
print('case1')
runner = unittest.TextTestRunner()
runner.run(test011.test_Case1)
elif testcase == 2 :
print('case2')
runner = unittest.TextTestRunner()
runner.run(test011.test_Case2)
elif testcase == 3 :
print('case3')
runner = unittest.TextTestRunner()
runner.run(test011.test_Case3)
โค้ด: เลือกทั้งหมด
check if
----------------------------------------------------------------------
case2
Ran 0 tests in 0.000s
test_Case2=2
OK
เทคนิคการเขียน Python
บทเรียน Python
Programming - C/C++ & java & Python
บทเรียน Python GUI
บทเรียน Python Framework Flask