Example Python codes
สมมติมี def ของการคำนวณ multiply หาผลคูณของสองตัวเลข
1 def multiply(x, y):
2 return x * y
การใช้ doctest [1] โดยใส่สัญลักษณ์ >>> ภายใน comment ส่วนที่ต้องการทดสอบหน้า def หรือแต่ละคำสั่ง สำหรับตัวอย่างอื่นๆ ดูที่ doctestintro บนไซต์ Pythontesting.net [2]
1 '''
2 Module showing how doctests can be included with source code
3 Each '>>>' line is run as if in a python shell, and counts as a test.
4 The next line, if not '>>>' is the expected output of the previous line.
5 If anything doesn't match exactly (including trailing spaces), the test fails.
6 '''
7
8
9 def multiply(x, y):
10 """
11 >>> multiply(4, 3)
12 12
13 >>> multiply('a', 3)
14 'aaa'
15 """
16 return x * y
เอกสารเกี่ยวกับ doctest
| [1] | เอกสาร doctest ของภาษา, Python doctest |
| [2] | คำแนะนำการใช้ doctest เบื้องต้น รวมถึงการรันเพื่อทดสอบโรปรแกรม การแยกไฟล์ .py, doctest Introduction |