Python มาพร้อมกับโมดูล doctest มาตรฐานที่ทดสอบเนื้อหาของ docstring ทำให้ง่ายต่อการเขียนตัวอย่างอินพุตและเอาต์พุตใน docstring และทำให้เอกสารเข้าใจได้ง่ายขึ้น
ข้อมูลต่อไปนี้มีให้ที่นี่
- ตัวอย่างง่ายๆ ของการทดสอบกับdoctest 
- หากไม่มีข้อผิดพลาด
 - หากมีข้อผิดพลาด
 
 - ควบคุมผลลัพธ์ด้วยตัวเลือกและอาร์กิวเมนต์ 
-vตัวเลือกverboseอาร์กิวเมนต์ (เช่น ฟังก์ชัน โปรแกรม โปรแกรม)
 - เรียกใช้โมดูล doctest จากบรรทัดคำสั่ง
 - การเขียนแบบทดสอบในไฟล์ข้อความภายนอก 
- วิธีเขียนไฟล์ข้อความ
 - เรียกจากไฟล์ py
 - เรียกใช้ไฟล์ข้อความโดยตรง
 
 
ตัวอย่างง่ายๆ ของการทดสอบกับdoctest
docstring เป็นสตริงที่อยู่ในรายการใดรายการหนึ่งต่อไปนี้: (1) ชื่อของฟังก์ชันที่จะทดสอบ (2) ชื่อของฟังก์ชันที่จะทดสอบ และ (3) ค่าเอาต์พุตที่คาดหวังในโหมดโต้ตอบของ Python
"""''
หากไม่มีข้อผิดพลาด
ตรวจสอบให้แน่ใจว่ารหัสถูกต้องในเนื้อหาฟังก์ชันและเอกสาร
def add(a, b):
    '''
    >>> add(1, 2)
    3
    >>> add(5, 10)
    15
    '''
    return a + b
if __name__ == '__main__':
    import doctest
    doctest.testmod()
เรียกใช้ไฟล์นี้
$ python3 doctest_example.py
หากไม่มีข้อผิดพลาดจะไม่มีอะไรส่งออก
if __name__ == '__main__'ซึ่งหมายความว่า “ดำเนินการประมวลผลที่ตามมาก็ต่อเมื่อไฟล์สคริปต์ที่เกี่ยวข้องถูกเรียกใช้จากบรรทัดคำสั่งเท่านั้น
หากมีข้อผิดพลาด
หากคุณสร้างและรันโค้ดที่ไม่ถูกต้องต่อไปนี้ ข้อผิดพลาดจะถูกส่งออก
def add(a, b):
    '''
    >>> add(1, 2)
    3
    >>> add(5, 10)
    10
    '''
    return a * b
if __name__ == '__main__':
    import doctest
    doctest.testmod()
$ python3 doctest_example_error.py
**********************************************************************
File "doctest_example_error.py", line 3, in __main__.add
Failed example:
    add(1, 2)
Expected:
    3
Got:
    2
**********************************************************************
File "doctest_example_error.py", line 5, in __main__.add
Failed example:
    add(5, 10)
Expected:
    10
Got:
    50
**********************************************************************
1 items had failures:
   2 of   2 in __main__.add
***Test Failed*** 2 failures.
มันแสดงให้เห็นดังนี้
| ค่าเอาต์พุตที่คาดไว้เขียนใน doctest | Expected | 
| มูลค่าส่งออกจริง | Got | 
ควบคุมผลลัพธ์ด้วยตัวเลือกและอาร์กิวเมนต์
-vตัวเลือก
หากคุณต้องการให้ผลลัพธ์เอาต์พุตแสดงขึ้นแม้ว่าจะไม่มีข้อผิดพลาด ให้รันคำสั่งด้วยตัวเลือก -v บนบรรทัดคำสั่ง
$ python3 doctest_example.py -v
Trying:
    add(1, 2)
Expecting:
    3
ok
Trying:
    add(5, 10)
Expecting:
    15
ok
1 items had no tests:
    __main__
1 items passed all tests:
   2 tests in __main__.add
2 tests in 2 items.
2 passed and 0 failed.
Test passed.
verboseอาร์กิวเมนต์ (เช่น ฟังก์ชัน โปรแกรม โปรแกรม)
หากคุณต้องการแสดงผลผลลัพธ์เสมอ ให้ระบุอาร์กิวเมนต์ verbose=True ใน doctest.testmod() ในไฟล์ py
if __name__ == '__main__':
    import doctest
    doctest.testmod(verbose=True)
ผลลัพธ์เอาต์พุตจะแสดงโดยไม่มีตัวเลือก -v ที่รันไทม์เสมอ
$ python3 doctest_example_verbose.py
Trying:
    add(1, 2)
Expecting:
    3
ok
Trying:
    add(5, 10)
Expecting:
    15
ok
1 items had no tests:
    __main__
1 items passed all tests:
   2 tests in __main__.add
2 tests in 2 items.
2 passed and 0 failed.
Test passed.
เรียกใช้โมดูล doctest จากบรรทัดคำสั่ง
if __name__ == '__main__'หากคุณต้องการทำอย่างอื่นในนั้น คุณสามารถเรียกใช้โมดูล doctest ได้โดยตรงจากบรรทัดคำสั่งโดยไม่ต้องเรียก doctest.testmod() ในไฟล์ py
ตัวอย่างเช่น ในกรณีต่อไปนี้
def add(a, b):
    '''
    >>> add(1, 2)
    3
    >>> add(5, 10)
    15
    '''
    return a + b
if __name__ == '__main__':
    import sys
    result = add(int(sys.argv[1]), int(sys.argv[2]))
    print(result)
สามารถรับอาร์กิวเมนต์บรรทัดคำสั่งและดำเนินการตามกระบวนการได้ตามปกติ
$ python3 doctest_example_without_import.py 3 4
7
หากคุณเรียกใช้ doctest เป็นสคริปต์ด้วยตัวเลือก -m การทดสอบจะดำเนินการกับฟังก์ชันที่เขียน doctest หากคุณต้องการแสดงผลผลลัพธ์ ให้เติม -v เหมือนเดิม
$ python3 -m doctest doctest_example_without_import.py
$ python3 -m doctest -v doctest_example_without_import.py
Trying:
    add(1, 2)
Expecting:
    3
ok
Trying:
    add(5, 10)
Expecting:
    15
ok
1 items had no tests:
    doctest_example_without_import
1 items passed all tests:
   2 tests in doctest_example_without_import.add
2 tests in 2 items.
2 passed and 0 failed.
Test passed.
การเขียนแบบทดสอบในไฟล์ข้อความภายนอก
คุณยังสามารถเขียนโค้ดทดสอบในไฟล์ข้อความภายนอกแทนใน docstring
วิธีเขียนไฟล์ข้อความ
เขียนในรูปแบบโหมดโต้ตอบ Python ตามที่อธิบายไว้ใน docstring จำเป็นต้องนำเข้าฟังก์ชันเพื่อใช้งาน
หากคุณต้องการใส่ไฟล์ข้อความในไดเร็กทอรีเดียวกันกับไฟล์ .py ที่จะทดสอบ เพียงแค่นำเข้าไฟล์ดังต่อไปนี้
>>> from doctest_example import add
>>> add(1, 2)
3
>>> add(5, 10)
15
เรียกจากไฟล์ py
เรียก doctest.testfile() ในไฟล์ .py อื่นเพื่อทำการทดสอบ
ระบุเส้นทางของไฟล์ข้อความที่เขียนโค้ดทดสอบเป็นอาร์กิวเมนต์ของ doctest.testfile()
import doctest
doctest.testfile('doctest_text.txt')
เรียกใช้ไฟล์ py นี้
$ python3 doctest_example_testfile.py -v
Trying:
    from doctest_example import add
Expecting nothing
ok
Trying:
    add(1, 2)
Expecting:
    3
ok
Trying:
    add(5, 10)
Expecting:
    15
ok
1 items passed all tests:
   3 tests in doctest_text.txt
3 tests in 1 items.
3 passed and 0 failed.
Test passed.
เรียกใช้ไฟล์ข้อความโดยตรง
แม้ว่าคุณจะไม่มีไฟล์ py คุณสามารถอ่านไฟล์ข้อความได้โดยตรงจากบรรทัดคำสั่งและเรียกใช้การทดสอบ
รันคำสั่ง Python ด้วยตัวเลือก -m เพื่อรัน doctest เป็นสคริปต์ คุณสามารถระบุพาธไฟล์ข้อความเป็นอาร์กิวเมนต์บรรทัดคำสั่งได้
$ python3 -m doctest -v doctest_text.txt
Trying:
    from doctest_example import add
Expecting nothing
ok
Trying:
    add(1, 2)
Expecting:
    3
ok
Trying:
    add(5, 10)
Expecting:
    15
ok
1 items passed all tests:
   3 tests in doctest_text.txt
3 tests in 1 items.
3 passed and 0 failed.
Test passed.

 
