การรันTestCaseโดยใช้ตัวแปรเป็นตัวกำหนด ใน Python

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderator: mindphp, ผู้ดูแลกระดาน

ธวัชชัย แสนหาญ
PHP Super Member
PHP Super Member
โพสต์: 499
ลงทะเบียนเมื่อ: 15/11/2018 10:02 am

การรันTestCaseโดยใช้ตัวแปรเป็นตัวกำหนด ใน Python

โพสต์ที่ยังไม่ได้อ่าน โดย ธวัชชัย แสนหาญ »

การรันTestCaseโดยใช้ตัวแปรเป็นตัวกำหนด ใน Python

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
ช่องทางศึกษาเพิ่มเติม :baa:
เทคนิคการเขียน Python
บทเรียน Python
Programming - C/C++ & java & Python
บทเรียน Python GUI
บทเรียน Python Framework Flask
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 54