2016-04-13 12 views
0

Unittest.TestCase kullanarak bir sınıf hiyerarşisi oluşturmaya çalışıyorum. Bunlar kesinlikle birim testleri burada çalıştırıyorum değil, bunun yerine, birkaç parametreye (örneğin müşteri adı ve eksik değer) bağımlı olan bir dizi dize ayrıştırma işlevselliğini test etmeye çalışıyorum. Genel fikir, sadece birtakım olanaksızlıklardan faydalanmak ve DRY'yi korumaktır.Subclassing unittest.TestCase, 'super() __ init __() `anahtar sözcüğünü sağlar ve` __init__' un beklenmedik bir anahtar kelime argümanı var

import unittest 
import parsingfunc as i 

class CustomerTestCase(unittest.TestCase): 
    """Base class, most general functionality and test cases""" 

    def __init__(self, testname): 
     super(CustomerTestCase, self).__init__(testname) 

    # helpers 

    def mess_with_spaces(): 
     ... 

    def mess_with_case(): 
     ... 

    # tests 

    def test_case(self, value): 
     """ 
     Test that parsing function produces the same 
     value regardless of case of input 
     """ 
     self.assertEqual(self.func(value, missing=self.missing, customer=self.name), 
         self.func(self.mess_with_case(value), missing=self.missing, customer=self.name))) 
     ... 

    def test_spaces(self, value): 
     """ 
     Test that parsing function produces the same 
     value regardless of spacing present in input 
     """ 
     ... 


class AisleValues(CustomerTestCase): 
    """Base class for testing aisle values""" 

    def __init__(self, testname, customername=None, missing=None): 
     super(CustomerTestCase, self).__init__(testname) 
     self.name = customername 
     self.missing = missing 
     self.func = i.aisle_to_num 

    ... 

class PeacockAisles(AisleValues): 
    """Peacock aisle parsing test cases""" 

    def __init__(self, testname): 
     super(AisleValues, self).__init__(testname, customername='peacock', missing='nan') 

    ... 

Ve şimdi bu sınıfların

In [6]: a = i.CustomerTestCase('space_test') 

In [7]: a.__dict__ 
Out[7]: 
{'_cleanups': [], 
'_resultForDoCleanups': None, 
'_testMethodDoc': '\n  Test that parsing function produces the same\n  value regardless of spacing present in input\n  ', 
'_testMethodName': 'test_spaces', 
'_type_equality_funcs': {list: 'assertListEqual', 
    dict: 'assertDictEqual', 
    set: 'assertSetEqual', 
    frozenset: 'assertSetEqual', 
    tuple: 'assertTupleEqual', 
    unicode: 'assertMultiLineEqual'}} 

In [8]: b = i.AisleValues('test_spaces') 

In [9]: b.__dict__ 
Out[9]: 
{'_cleanups': [], 
'_resultForDoCleanups': None, 
'_testMethodDoc': '\n  Test that parsing function produces the same\n  value regardless of spacing present in input\n  ', 
'_testMethodName': 'test_spaces', 
'_type_equality_funcs': {list: 'assertListEqual', 
    dict: 'assertDictEqual', 
    set: 'assertSetEqual', 
    frozenset: 'assertSetEqual', 
    tuple: 'assertTupleEqual', 
    unicode: 'assertMultiLineEqual'}, 
'func': <function integration.aisle_to_num>, 
'missing': None, 
'name': None} 

In [10]: b = i.AisleValues('test_spaces', customername='peacock', missing='nan') 

In [11]: b.__dict__ 
Out[12]: 
{'_cleanups': [], 
'_resultForDoCleanups': None, 
'_testMethodDoc': '\n  Test that parsing function produces the same\n  value regardless of spacing present in input\n  ', 
'_testMethodName': 'test_spaces', 
'_type_equality_funcs': {list: 'assertListEqual', 
    dict: 'assertDictEqual', 
    set: 'assertSetEqual', 
    frozenset: 'assertSetEqual', 
    tuple: 'assertTupleEqual', 
    unicode: 'assertMultiLineEqual'}, 
'func': <function integration.aisle_to_num>, 
'missing': 'nan', 
'name': 'peacock'} 

In [13]: c = i.PeacockAisles('test_spaces') 

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-12-d4bba181b94e> in <module>() 
----> 1 c = i.PeacockAisles('test_spaces') 

/path/to/python.py in __init__(self, testname) 
    89 
    90  def __init__(self, testname): 
---> 91   super(AisleValues, self).__init__(testname, customername='peacock', missing='nan') 
    92   pprint(self.__dict__) 
    93 

TypeError: __init__() got an unexpected keyword argument 'customername' 

Yani mesele nedir örneklerini oluşturmayı deneyin? Teşekkürler!

cevap

1

super'u doğru aramıyorsunuz. super numaralı çağrıya bir sınıf adı verdiğinizde, temel bir sınıf değil (geçerli sınıfı değil, temel bir sınıf değil (gerçekten ne yaptığınızı bilmiyorsanız ve temel sınıfın uygulamasını atlamak istemiyorsanız, "büyük ebeveyn" lehine " sınıf).

Geçerli kodunuz, unittest.TestCase.__init__ numaralı telefonu arayarak CustomerTestCase.__init__'u atlayarak AisleValues.__init__ numaralı telefona sahiptir. İşe yarayacak çünkü CustomerTestCase.__init__ herhangi bir işe yaramıyor (etkisi olmadan silebilirsiniz), ama bu sadece şans. PeacockAisles.__init__, CustomerTestCase.__init__'u (AisleValues.__init__'u atlayarak) çağırdığında, büyükbaba sınıfı, çocuğuyla aynı tüm bağımsız değişkenlere izin vermediğinden başarısız olur.

Sen istiyorum:

class AisleValues(CustomerTestCase): 
    """Base class for testing aisle values""" 

    def __init__(self, testname, customername=None, missing=None): 
     super(AisleValues, self).__init__(testname)   # change the class named here 
     ... 

Ve:

class PeacockAisles(AisleValues): 
    """Peacock aisle parsing test cases""" 

    def __init__(self, testname): 
     super(PeacockAisles, self).__init__(testname, customername='peacock', missing='nan') 
     ...            # and on the previous line 
+0

Ahh evet bu çok mantıklı, ben basit bir şey olduğunu biliyordum. Şuan çalışıyor. Teşekkürler! –

İlgili konular