Q - สอบถามการรันเฉพาะTastCase ที่ ต้องการครับ โดยใช้ ตัวแปรเป็นตัวกำหนด

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: Q - สอบถามการรันเฉพาะTastCase ที่ ต้องการครับ โดยใช้ ตัวแปรเป็นตัวกำหนด

Re: Q - สอบถามการรันเฉพาะTastCase ที่ ต้องการครับ โดยใช้ ตัวแปรเป็นตัวกำหนด

โดย ธวัชชัย แสนหาญ » 20/02/2019 10:49 am

วิธีนี้ใช้ได้ครับ
โดยการย้ายตัวเช็ค if ไปไว้ในฟังชันแทน

โค้ด: เลือกทั้งหมด

import unittest

testcase = 2

class test011(unittest.TestCase):
    def test_Case(self):
        if testcase == 1:
            print('test01=%d'%testcase)

        elif testcase == 2:
            print('test02=%d'%testcase)

        elif testcase == 3:
            print('test03=%d'%testcase)


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

Re: Q - สอบถามการรันเฉพาะTastCase ที่ ต้องการครับ โดยใช้ ตัวแปรเป็นตัวกำหนด

โดย ธวัชชัย แสนหาญ » 15/02/2019 4:23 pm

โค้ดนี้ใช้ได้ครับ

โค้ด: เลือกทั้งหมด

import unittest

testcase = 2

class test011(unittest.TestCase):
    def test_Case1(self):
        print('test01=%d'%testcase)

    def test_Case2(self):
        print('test02=%d'%testcase)

    def test_Case3(self):
        print('test03=%d'%testcase)


if __name__ == '__main__':
    print('เข้า')

    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)

Re: Q - สอบถามการรันเฉพาะTastCase ที่ ต้องการครับ โดยใช้ ตัวแปรเป็นตัวกำหนด

โดย tatiya » 15/02/2019 3:17 pm

โค้ด: เลือกทั้งหมด

import unittest

testcase = 2

class test011(unittest.TestCase):
    def test_Case1(self):
        print('test01=%d'%testcase)

    def test_Case2(self):
        print('test02=%d'%testcase)

    def test_Case3(self):
        print('test03=%d'%testcase)


if __name__ == '__main__':

    if testcase == 1 :
        test011.test_Case1
    elif testcase == 2 :
        test011.test_Case2
    elif testcase == 3 :
        test011.test_Case3

Q - สอบถามการรันเฉพาะTastCase ที่ ต้องการครับ โดยใช้ ตัวแปรเป็นตัวกำหนด

โดย ธวัชชัย แสนหาญ » 15/02/2019 12:09 pm

Q - สอบถามการรันเฉพาะTastCase ที่ ต้องการครับ โดยใช้ ตัวแปรเป็นตัวกำหนด
ผมกำหนด ตัวแปร testcase = 2 เพื่อต้องการ ให้โปรแกรมทำงาน ที่ ฟังก์ชัน test_Case2 อย่างเดียว
คือในโค้ดนี้ โปรแกรม มันรัน ทุก Case มีวิธีเช็คเงื่อนไขคำสั่ง if โดยใช้ตัวแปร testcase เป็นตัวกำหนดในการทำงาน เฉพาะ Case ไหมครับ

โค้ดที่ใช้

โค้ด: เลือกทั้งหมด

import unittest

testcase = 2

class test011(unittest.TestCase):
    def test_Case1(self):
        print('test01=%d'%testcase)

    def test_Case2(self):
        print('test02=%d'%testcase)

    def test_Case3(self):
        print('test03=%d'%testcase)


if __name__ == '__main__':

    if testcase == 1 :
        test011.test_Case1
    elif testcase == 2 :
        test011.test_Case2
    elif testcase == 3 :
        test011.test_Case3

    unittest.main()
ผลรันโปรแกรม
รูปภาพ

ข้างบน