Python เบื้องต้นสำหรับการทดสอบโค้ด
Alexander Levin
Data Scientist
class Rectangle:
# Constructor of Rectangle
def __init__(self, a, b):
self.a = a
self.b = b
# Area method
def get_area(self):
return self.a * self.b
# Usage example
r = Rectangle(4, 5)
print(r.get_area())
>> 20
class RedRectangle(Rectangle):
self.color = 'red'
unittest - เฟรมเวิร์กสำหรับทดสอบอัตโนมัติที่ติดมากับ Pythonunittest - ใช้ได้ไม่จำกัดแค่การทดสอบระดับยูนิตunittest
Python)pytest
test_แพ็กเกจของบุคคลที่สาม (ต้องติดตั้งแยกจาก Python)
มี assertion method น้อยกว่า
ทดสอบตัวดำเนินการยกกำลัง:
import unittest
# Declaring the TestCase class
class TestSquared(unittest.TestCase):
# Defining the test
def test_negative(self):
self.assertEqual((-3) ** 2, 9)
.assertEqual(), .assertNotEqual().assertTrue(), .assertFalse().assertIs(), .assertIsNone().assertIsInstance(), .assertIn().assertRaises()unittest - เฟรมเวิร์ก built-in ของ Python แบบ OOP สำหรับทดสอบอัตโนมัติunittestunittest.TestCasePython เบื้องต้นสำหรับการทดสอบโค้ด