Nhập môn Kiểm thử trong Python
Alexander Levin
Data Scientist
.setUp() - phương thức được gọi để chuẩn bị fixture trước khi chạy bài kiểm thử.tearDown() - phương thức được gọi sau khi chạy để làm sạch môi trườngimport unittest
class TestLi(unittest.TestCase):
# Fixture setup method
def setUp(self):
self.li = [i for i in range(100)]
# Fixture teardown method
def tearDown(self):
self.li.clear()
# Test method
def test_your_list(self):
self.assertIn(99, self.li)
self.assertNotIn(100, self.li)
Cú pháp đúng: setUp với chữ U hoa và tearDown với chữ D hoa.
class TestLi(unittest.TestCase):
# Fixture setup method
def setUp(self):
self.li = [i for i in range(100)]
# Fixture teardown method
def tearDown(self):
self.li.clear()
Lệnh: python3 -m unittest test_in_list.py
Đầu ra khi chạy với .setUp() và .tearDown():

Đầu ra khi chạy với .set_up():

.setUp().tearDown().setUp() - phương thức được gọi để chuẩn bị fixture trước khi chạy kiểm thử..tearDown() - phương thức được gọi sau kiểm thử để dọn dẹp môi trường.Nhập môn Kiểm thử trong Python