2012-09-25 15 views
8

TestCase alt sınıfına özel bir onaylama yöntemi eklemek istiyorum. Uygulamamı unittest modülünden kopyalamaya çalıştım, böylece düzenli TestCase davranışının olabildiğince yakından eşleşmesi sağlandı. (Sadece self.assertEqual()'a yetki vermeyi tercih ederim, ancak bu daha da fazla backtrace gürültüsüne neden olur, aşağıya bakın.) unittest modülü, başarısız onaylama bildirimlerini uygularken uygulamanın bazı iç ayrıntılarını otomatik olarak gizler gibi görünüyor.Yığın çerçevelerim bir TestCase alt sınıfında nasıl gizleyebilirim?

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() 

bu çıkış, örneğin gibidir:

[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) 

hazır unittest yöntem sadece birine sahip ise, özel Assert yöntemi iki çerçeve, yöntem kendi içinde bir ile bir yığın izlemesi, neden olduğu Not çerçeve, kullanıcı kodunda ilgili satır. Bu çerçeve gizleme davranışını kendi yöntemime nasıl uygularım?

cevap

14

Bu soru cevaplandı by Peter Otten on comp.lang.python.

MyTestCase'i ayrı bir modülde taşıyın ve genel bir değişken __unittest = True tanımlayın.

$ 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

Bu sorunun/yanıtın daha fazla oy almadığına inanamıyorum! Büyük bilgi & neden ben <3 SO. – dbn

+0

Bu çözüm çok yararlı! –

+0

python.org üzerindeki pipermail değiştiğinden, ancak Peter's çözümüne doğru bağlantının https://mail.python.org/pipermail/python-list/2012-October/632386.html olduğundan emin değil. Bunun dışında gösterici için çok teşekkür ederim, gerçekten bana yardımcı oldu. –

İlgili konular