2012-09-25 26 views
8

Tôi muốn thêm phương thức xác nhận tùy chỉnh vào lớp con TestCase. Tôi đã cố gắng sao chép triển khai của mình từ mô-đun unittest sao cho nó phù hợp với hành vi của TestCase thường xuyên nhất có thể. (Tôi muốn chỉ ủy quyền cho self.assertEqual() nhưng điều này gây ra nhiều tiếng ồn backtrace hơn, xem bên dưới.) Mô-đun unittest dường như tự động ẩn một số chi tiết nội bộ của việc triển khai khi báo cáo xác nhận không thành công.Làm cách nào để ẩn khung ngăn xếp trong lớp con TestCase?

import unittest 

class MyTestCase(unittest.TestCase): 
    def assertLengthIsOne(self, sequence, msg=None): 
     if len(sequence) != 1: 
      msg = self._formatMessage(msg, "length is not one") 
      raise self.failureException(msg) 

class TestFoo(MyTestCase): 
    seq = (1, 2, 3, 4, 5) 

    def test_stock_unittest_assertion(self): 
     self.assertEqual(len(self.seq), 1) 

    def test_custom_assertion(self): 
     self.assertLengthIsOne(self.seq) 


unittest.main() 

Kết quả của việc này là như vậy:

[email protected] $ python unittest-demo.py 
FF 
====================================================================== 
FAIL: test_custom_assertion (__main__.TestFoo) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "unittest-demo.py", line 16, in test_custom_assertion 
    self.assertLengthIsOne(self.seq) 
    File "unittest-demo.py", line 7, in assertLengthIsOne 
    raise self.failureException(msg) 
AssertionError: length is not one 

====================================================================== 
FAIL: test_stock_unittest_assertion (__main__.TestFoo) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "unittest-demo.py", line 13, in test_stock_unittest_assertion 
    self.assertEqual(len(self.seq), 1) 
AssertionError: 5 != 1 

---------------------------------------------------------------------- 
Ran 2 tests in 0.000s 

FAILED (failures=2) 

Lưu ý rằng phương pháp tùy chỉnh khẳng định gây ra một vết đống với hai khung, một bên trong phương pháp riêng của mình, trong khi các phương pháp cổ unittest chỉ có một khung, dòng có liên quan trong mã của người dùng. Làm cách nào để áp dụng hành vi ẩn khung này cho phương pháp của riêng tôi?

Trả lời

14

Câu hỏi này đã được trả lời by Peter Otten on comp.lang.python.

Di chuyển MyTestCase trong một mô-đun riêng biệt và xác định biến toàn cầu __unittest = True.

$ cat mytestcase.py 
import unittest 

__unittest = True 

class MyTestCase(unittest.TestCase): 
    def assertLengthIsOne(self, sequence, msg=None): 
     if len(sequence) != 1: 
      msg = self._formatMessage(msg, "length is not one") 
      raise self.failureException(msg) 

$ cat mytestcase_demo.py 
import unittest 
from mytestcase import MyTestCase 

class TestFoo(MyTestCase): 
    seq = (1, 2, 3, 4, 5) 

    def test_stock_unittest_assertion(self): 
     self.assertEqual(len(self.seq), 1) 

    def test_custom_assertion(self): 
     self.assertLengthIsOne(self.seq) 

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

$ python mytestcase_demo.py 
FF 
====================================================================== 
FAIL: test_custom_assertion (__main__.TestFoo) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "mytestcase_demo.py", line 11, in test_custom_assertion 
    self.assertLengthIsOne(self.seq) 
AssertionError: length is not one 

====================================================================== 
FAIL: test_stock_unittest_assertion (__main__.TestFoo) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "mytestcase_demo.py", line 8, in test_stock_unittest_assertion 
    self.assertEqual(len(self.seq), 1) 
AssertionError: 5 != 1 

---------------------------------------------------------------------- 
Ran 2 tests in 0.000s 

FAILED (failures=2) 
$ 
+0

Tôi không thể tin rằng câu hỏi/câu trả lời này chưa nhận được nhiều phiếu bầu hơn! Thông tin tuyệt vời & chính xác lý do tôi <3 SO. – dbn

+0

Giải pháp này hữu ích! –

+0

Không chắc chắn nếu pipermail trên python.org thay đổi nhưng liên kết chính xác đến giải pháp của Peter là https://mail.python.org/pipermail/python-list/2012-October/632386.html. Khác hơn là cảm ơn bạn rất nhiều cho con trỏ, thực sự đã giúp tôi. –

Các vấn đề liên quan